@mparticle/web-google-analytics-4-client-kit 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,14 +4,29 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
// Google requires event and user attribute strings to have specific limits
|
|
6
6
|
// in place when sending to data layer.
|
|
7
|
-
// https://support.google.com/analytics/answer/
|
|
7
|
+
// https://support.google.com/analytics/answer/11202874?sjid=7958830619827381593-NA
|
|
8
8
|
|
|
9
9
|
var EVENT_NAME_MAX_LENGTH = 40;
|
|
10
10
|
var EVENT_ATTRIBUTE_KEY_MAX_LENGTH = 40;
|
|
11
11
|
var EVENT_ATTRIBUTE_VAL_MAX_LENGTH = 100;
|
|
12
|
+
var EVENT_ATTRIBUTE_MAX_NUMBER = 100;
|
|
13
|
+
|
|
12
14
|
var USER_ATTRIBUTE_KEY_MAX_LENGTH = 24;
|
|
13
15
|
var USER_ATTRIBUTE_VALUE_MAX_LENGTH = 36;
|
|
14
16
|
|
|
17
|
+
var PRODUCT_ATTRIBUTE_MAX_NUMBER = 10;
|
|
18
|
+
|
|
19
|
+
var RESERVED_PRODUCT_KEYS = [
|
|
20
|
+
'item_category',
|
|
21
|
+
'item_category2',
|
|
22
|
+
'item_category3',
|
|
23
|
+
'item_category4',
|
|
24
|
+
'item_category5',
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
var FORBIDDEN_PREFIXES = ['google_', 'firebase_', 'ga_'];
|
|
28
|
+
var FORBIDDEN_CHARACTERS_REGEX = /[^a-zA-Z0-9_]/g;
|
|
29
|
+
|
|
15
30
|
function truncateString(string, limit) {
|
|
16
31
|
return !!string && string.length > limit
|
|
17
32
|
? string.substring(0, limit)
|
|
@@ -56,6 +71,49 @@ Common.prototype.truncateAttributes = function (
|
|
|
56
71
|
return truncatedAttributes;
|
|
57
72
|
};
|
|
58
73
|
|
|
74
|
+
Common.prototype.limitAttributes = function (attributes, limitNumber) {
|
|
75
|
+
if (isEmpty(attributes)) {
|
|
76
|
+
return {};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
var attributeKeys = Object.keys(attributes);
|
|
80
|
+
|
|
81
|
+
attributeKeys.sort();
|
|
82
|
+
|
|
83
|
+
var limitedAttributes = attributeKeys
|
|
84
|
+
.slice(0, limitNumber)
|
|
85
|
+
.reduce(function (obj, key) {
|
|
86
|
+
obj[key] = attributes[key];
|
|
87
|
+
return obj;
|
|
88
|
+
}, {});
|
|
89
|
+
|
|
90
|
+
return limitedAttributes;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
Common.prototype.limitEventAttributes = function (attributes) {
|
|
94
|
+
return this.limitAttributes(attributes, EVENT_ATTRIBUTE_MAX_NUMBER);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
Common.prototype.limitProductAttributes = function (attributes) {
|
|
98
|
+
var productAttributes = {};
|
|
99
|
+
var reservedAttributes = {};
|
|
100
|
+
|
|
101
|
+
for (var key in attributes) {
|
|
102
|
+
if (RESERVED_PRODUCT_KEYS.indexOf(key) >= 0) {
|
|
103
|
+
reservedAttributes[key] = attributes[key];
|
|
104
|
+
} else {
|
|
105
|
+
productAttributes[key] = attributes[key];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
var limitedProductAttributes = this.limitAttributes(
|
|
110
|
+
productAttributes,
|
|
111
|
+
PRODUCT_ATTRIBUTE_MAX_NUMBER
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return this.mergeObjects(limitedProductAttributes, reservedAttributes);
|
|
115
|
+
};
|
|
116
|
+
|
|
59
117
|
Common.prototype.truncateEventName = function (eventName) {
|
|
60
118
|
return truncateString(eventName, EVENT_NAME_MAX_LENGTH);
|
|
61
119
|
};
|
|
@@ -68,6 +126,83 @@ Common.prototype.truncateEventAttributes = function (eventAttributes) {
|
|
|
68
126
|
);
|
|
69
127
|
};
|
|
70
128
|
|
|
129
|
+
Common.prototype.standardizeParameters = function (parameters) {
|
|
130
|
+
var standardizedParameters = {};
|
|
131
|
+
for (var key in parameters) {
|
|
132
|
+
var standardizedKey = this.standardizeName(key);
|
|
133
|
+
standardizedParameters[standardizedKey] = parameters[key];
|
|
134
|
+
}
|
|
135
|
+
return standardizedParameters;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
Common.prototype.standardizeName = function (name) {
|
|
139
|
+
// names of events and parameters have the following requirements:
|
|
140
|
+
// 1. They must only contain letters, numbers, and underscores
|
|
141
|
+
function removeForbiddenCharacters(name) {
|
|
142
|
+
return name.replace(FORBIDDEN_CHARACTERS_REGEX, '_');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 2. They must start with a letter
|
|
146
|
+
function doesNameStartsWithLetter(name) {
|
|
147
|
+
return !isEmpty(name) && /^[a-zA-Z]/.test(name.charAt(0));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 3. They must not start with certain prefixes
|
|
151
|
+
function doesNameStartWithForbiddenPrefix(name) {
|
|
152
|
+
var hasPrefix = false;
|
|
153
|
+
if (!isEmpty(name)) {
|
|
154
|
+
for (var i = 0; i < FORBIDDEN_PREFIXES.length; i++) {
|
|
155
|
+
var prefix = FORBIDDEN_PREFIXES[i];
|
|
156
|
+
if (name.indexOf(prefix) === 0) {
|
|
157
|
+
hasPrefix = true;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return hasPrefix;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function removeNonAlphabetCharacterFromStart(name) {
|
|
167
|
+
var str = name.slice();
|
|
168
|
+
while (!isEmpty(str) && str.charAt(0).match(/[^a-zA-Z]/i)) {
|
|
169
|
+
str = str.substring(1);
|
|
170
|
+
}
|
|
171
|
+
return str;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function removeForbiddenPrefix(name) {
|
|
175
|
+
var str = name.slice();
|
|
176
|
+
|
|
177
|
+
FORBIDDEN_PREFIXES.forEach(function (prefix) {
|
|
178
|
+
if (str.indexOf(prefix) === 0) {
|
|
179
|
+
str = str.replace(prefix, '');
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
return str;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
var standardizedName = removeForbiddenCharacters(name);
|
|
187
|
+
|
|
188
|
+
// While loops is required because there is a chance that once certain sanitization
|
|
189
|
+
// occurs, that the resulting string will end up violating a different criteria.
|
|
190
|
+
// An example is 123___google_$$google_test_event. If letters, are removed and
|
|
191
|
+
// prefix is removed once, the remaining string will be __google_test_event which violates
|
|
192
|
+
// a string starting with a letter. We have to repeat the sanitizations repeatedly
|
|
193
|
+
// until all criteria checks pass.
|
|
194
|
+
while (
|
|
195
|
+
!doesNameStartsWithLetter(standardizedName) ||
|
|
196
|
+
doesNameStartWithForbiddenPrefix(standardizedName)
|
|
197
|
+
) {
|
|
198
|
+
standardizedName =
|
|
199
|
+
removeNonAlphabetCharacterFromStart(standardizedName);
|
|
200
|
+
standardizedName = removeForbiddenPrefix(standardizedName);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return standardizedName;
|
|
204
|
+
};
|
|
205
|
+
|
|
71
206
|
Common.prototype.truncateUserAttributes = function (userAttributes) {
|
|
72
207
|
return this.truncateAttributes(
|
|
73
208
|
userAttributes,
|
|
@@ -165,8 +300,11 @@ Common.prototype.getUserId = function (
|
|
|
165
300
|
|
|
166
301
|
var common = Common;
|
|
167
302
|
|
|
303
|
+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
304
|
+
|
|
305
|
+
var self$1 = commonjsGlobal;
|
|
168
306
|
function CommerceHandler(common) {
|
|
169
|
-
this.common = common || {};
|
|
307
|
+
self$1.common = this.common = common || {};
|
|
170
308
|
}
|
|
171
309
|
|
|
172
310
|
var ProductActionTypes = {
|
|
@@ -200,8 +338,14 @@ var ADD_SHIPPING_INFO = 'add_shipping_info',
|
|
|
200
338
|
CommerceHandler.prototype.logCommerceEvent = function (event) {
|
|
201
339
|
var needsCurrency = true,
|
|
202
340
|
needsValue = true,
|
|
203
|
-
ga4CommerceEventParameters,
|
|
204
|
-
isViewCartEvent = false
|
|
341
|
+
ga4CommerceEventParameters = {},
|
|
342
|
+
isViewCartEvent = false,
|
|
343
|
+
customEventAttributes = event.EventAttributes || {},
|
|
344
|
+
// affiliation potentially lives on any commerce event with items
|
|
345
|
+
affiliation =
|
|
346
|
+
event && event.ProductAction
|
|
347
|
+
? event.ProductAction.Affiliation
|
|
348
|
+
: null;
|
|
205
349
|
|
|
206
350
|
// GA4 has a view_cart event which MP does not support via a ProductActionType
|
|
207
351
|
// In order to log a view_cart event, pass ProductActionType.Unknown along with
|
|
@@ -214,12 +358,12 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
|
|
|
214
358
|
event.CustomFlags[GA4_COMMERCE_EVENT_TYPE] === VIEW_CART
|
|
215
359
|
) {
|
|
216
360
|
isViewCartEvent = true;
|
|
217
|
-
return logViewCart(event);
|
|
361
|
+
return logViewCart(event, affiliation);
|
|
218
362
|
}
|
|
219
363
|
}
|
|
220
364
|
// Handle Impressions
|
|
221
365
|
if (event.EventCategory === ProductActionTypes.Impression) {
|
|
222
|
-
return logImpressionEvent(event);
|
|
366
|
+
return logImpressionEvent(event, affiliation);
|
|
223
367
|
// Handle Promotions
|
|
224
368
|
} else if (
|
|
225
369
|
event.EventCategory === PromotionActionTypes.PromotionClick ||
|
|
@@ -228,43 +372,38 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
|
|
|
228
372
|
return logPromotionEvent(event);
|
|
229
373
|
}
|
|
230
374
|
|
|
231
|
-
ga4CommerceEventParameters = buildParameters(event);
|
|
232
|
-
if (event.EventAttributes) {
|
|
233
|
-
ga4CommerceEventParameters = this.common.mergeObjects(
|
|
234
|
-
ga4CommerceEventParameters,
|
|
235
|
-
event.EventAttributes
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
375
|
switch (event.EventCategory) {
|
|
240
376
|
case ProductActionTypes.AddToCart:
|
|
241
377
|
case ProductActionTypes.RemoveFromCart:
|
|
242
|
-
ga4CommerceEventParameters = buildAddOrRemoveCartItem(
|
|
378
|
+
ga4CommerceEventParameters = buildAddOrRemoveCartItem(
|
|
379
|
+
event,
|
|
380
|
+
affiliation
|
|
381
|
+
);
|
|
243
382
|
break;
|
|
244
383
|
case ProductActionTypes.Checkout:
|
|
245
|
-
ga4CommerceEventParameters = buildCheckout(event);
|
|
384
|
+
ga4CommerceEventParameters = buildCheckout(event, affiliation);
|
|
246
385
|
break;
|
|
247
386
|
case ProductActionTypes.Click:
|
|
248
|
-
ga4CommerceEventParameters = buildSelectItem(event);
|
|
387
|
+
ga4CommerceEventParameters = buildSelectItem(event, affiliation);
|
|
249
388
|
|
|
250
389
|
needsCurrency = false;
|
|
251
390
|
needsValue = false;
|
|
252
391
|
break;
|
|
253
392
|
case ProductActionTypes.Purchase:
|
|
254
|
-
ga4CommerceEventParameters = buildPurchase(event);
|
|
393
|
+
ga4CommerceEventParameters = buildPurchase(event, affiliation);
|
|
255
394
|
break;
|
|
256
395
|
case ProductActionTypes.Refund:
|
|
257
|
-
ga4CommerceEventParameters = buildRefund(event);
|
|
396
|
+
ga4CommerceEventParameters = buildRefund(event, affiliation);
|
|
258
397
|
break;
|
|
259
398
|
case ProductActionTypes.ViewDetail:
|
|
260
|
-
ga4CommerceEventParameters = buildViewItem(event);
|
|
399
|
+
ga4CommerceEventParameters = buildViewItem(event, affiliation);
|
|
261
400
|
break;
|
|
262
401
|
case ProductActionTypes.AddToWishlist:
|
|
263
|
-
ga4CommerceEventParameters = buildAddToWishlist(event);
|
|
402
|
+
ga4CommerceEventParameters = buildAddToWishlist(event, affiliation);
|
|
264
403
|
break;
|
|
265
404
|
|
|
266
405
|
case ProductActionTypes.CheckoutOption:
|
|
267
|
-
return logCheckoutOptionEvent(event);
|
|
406
|
+
return logCheckoutOptionEvent(event, affiliation);
|
|
268
407
|
|
|
269
408
|
default:
|
|
270
409
|
// a view cart event is handled at the beginning of this function
|
|
@@ -277,6 +416,18 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
|
|
|
277
416
|
}
|
|
278
417
|
}
|
|
279
418
|
|
|
419
|
+
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5714
|
|
420
|
+
if (this.common.forwarderSettings.enableDataCleansing) {
|
|
421
|
+
customEventAttributes = this.common.standardizeParameters(
|
|
422
|
+
customEventAttributes
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
ga4CommerceEventParameters = this.common.mergeObjects(
|
|
427
|
+
ga4CommerceEventParameters,
|
|
428
|
+
this.common.limitEventAttributes(customEventAttributes)
|
|
429
|
+
);
|
|
430
|
+
|
|
280
431
|
// CheckoutOption, Promotions, and Impressions will not make it to this code
|
|
281
432
|
if (needsCurrency) {
|
|
282
433
|
ga4CommerceEventParameters.currency = event.CurrencyCode;
|
|
@@ -293,29 +444,25 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
|
|
|
293
444
|
return true;
|
|
294
445
|
};
|
|
295
446
|
|
|
296
|
-
function
|
|
447
|
+
function buildAddOrRemoveCartItem(event, affiliation) {
|
|
297
448
|
return {
|
|
298
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
299
|
-
coupon: event.ProductAction ? event.ProductAction.CouponCode : null,
|
|
449
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
300
450
|
};
|
|
301
451
|
}
|
|
302
452
|
|
|
303
|
-
function
|
|
453
|
+
function buildCheckout(event, affiliation) {
|
|
304
454
|
return {
|
|
305
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function buildCheckout(event) {
|
|
310
|
-
return {
|
|
311
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
455
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
312
456
|
coupon: event.ProductAction ? event.ProductAction.CouponCode : null,
|
|
313
457
|
};
|
|
314
458
|
}
|
|
315
459
|
|
|
316
|
-
function buildCheckoutOptions(event) {
|
|
460
|
+
function buildCheckoutOptions(event, affiliation) {
|
|
317
461
|
var parameters = event.EventAttributes;
|
|
318
|
-
parameters.items = buildProductsList(
|
|
462
|
+
parameters.items = buildProductsList(
|
|
463
|
+
event.ProductAction.ProductList,
|
|
464
|
+
affiliation
|
|
465
|
+
);
|
|
319
466
|
parameters.coupon = event.ProductAction
|
|
320
467
|
? event.ProductAction.CouponCode
|
|
321
468
|
: null;
|
|
@@ -323,23 +470,23 @@ function buildCheckoutOptions(event) {
|
|
|
323
470
|
return parameters;
|
|
324
471
|
}
|
|
325
472
|
|
|
326
|
-
function parseImpression(impression) {
|
|
473
|
+
function parseImpression(impression, affiliation) {
|
|
327
474
|
return {
|
|
328
475
|
item_list_id: impression.ProductImpressionList,
|
|
329
476
|
item_list_name: impression.ProductImpressionList,
|
|
330
|
-
items: buildProductsList(impression.ProductList),
|
|
477
|
+
items: buildProductsList(impression.ProductList, affiliation),
|
|
331
478
|
};
|
|
332
479
|
}
|
|
333
480
|
|
|
334
|
-
function buildSelectItem(event) {
|
|
481
|
+
function buildSelectItem(event, affiliation) {
|
|
335
482
|
return {
|
|
336
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
483
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
337
484
|
};
|
|
338
485
|
}
|
|
339
486
|
|
|
340
|
-
function buildViewItem(event) {
|
|
487
|
+
function buildViewItem(event, affiliation) {
|
|
341
488
|
return {
|
|
342
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
489
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
343
490
|
};
|
|
344
491
|
}
|
|
345
492
|
|
|
@@ -347,36 +494,34 @@ function buildPromotion(promotion) {
|
|
|
347
494
|
return parsePromotion(promotion);
|
|
348
495
|
}
|
|
349
496
|
|
|
350
|
-
function buildPurchase(event) {
|
|
497
|
+
function buildPurchase(event, affiliation) {
|
|
351
498
|
return {
|
|
352
499
|
transaction_id: event.ProductAction.TransactionId,
|
|
353
500
|
value: event.ProductAction.TotalAmount,
|
|
354
|
-
affiliation: event.ProductAction.Affiliation,
|
|
355
501
|
coupon: event.ProductAction.CouponCode,
|
|
356
502
|
shipping: event.ProductAction.ShippingAmount,
|
|
357
503
|
tax: event.ProductAction.TaxAmount,
|
|
358
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
504
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
359
505
|
};
|
|
360
506
|
}
|
|
361
|
-
function buildRefund(event) {
|
|
507
|
+
function buildRefund(event, affiliation) {
|
|
362
508
|
return {
|
|
363
509
|
transaction_id: event.ProductAction.TransactionId,
|
|
364
510
|
value: event.ProductAction.TotalAmount,
|
|
365
|
-
affiliation: event.ProductAction.Affiliation,
|
|
366
511
|
coupon: event.ProductAction.CouponCode,
|
|
367
512
|
shipping: event.ProductAction.ShippingAmount,
|
|
368
513
|
tax: event.ProductAction.TaxAmount,
|
|
369
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
514
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
370
515
|
};
|
|
371
516
|
}
|
|
372
|
-
function buildAddToWishlist(event) {
|
|
517
|
+
function buildAddToWishlist(event, affiliation) {
|
|
373
518
|
return {
|
|
374
519
|
value: event.ProductAction.TotalAmount,
|
|
375
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
520
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
376
521
|
};
|
|
377
522
|
}
|
|
378
523
|
|
|
379
|
-
function buildAddShippingInfo(event) {
|
|
524
|
+
function buildAddShippingInfo(event, affiliation) {
|
|
380
525
|
return {
|
|
381
526
|
coupon:
|
|
382
527
|
event.ProductAction && event.ProductAction.CouponCode
|
|
@@ -386,11 +531,11 @@ function buildAddShippingInfo(event) {
|
|
|
386
531
|
event.CustomFlags && event.CustomFlags[GA4_SHIPPING_TIER]
|
|
387
532
|
? event.CustomFlags[GA4_SHIPPING_TIER]
|
|
388
533
|
: null,
|
|
389
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
534
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
390
535
|
};
|
|
391
536
|
}
|
|
392
537
|
|
|
393
|
-
function buildAddPaymentInfo(event) {
|
|
538
|
+
function buildAddPaymentInfo(event, affiliation) {
|
|
394
539
|
return {
|
|
395
540
|
coupon:
|
|
396
541
|
event.ProductAction && event.ProductAction.CouponCode
|
|
@@ -400,7 +545,7 @@ function buildAddPaymentInfo(event) {
|
|
|
400
545
|
event.CustomFlags && event.CustomFlags[GA4_PAYMENT_TYPE]
|
|
401
546
|
? event.CustomFlags[GA4_PAYMENT_TYPE]
|
|
402
547
|
: null,
|
|
403
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
548
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
404
549
|
};
|
|
405
550
|
}
|
|
406
551
|
|
|
@@ -412,32 +557,55 @@ function toUnderscore(string) {
|
|
|
412
557
|
.toLowerCase();
|
|
413
558
|
}
|
|
414
559
|
|
|
415
|
-
function parseProduct(
|
|
416
|
-
|
|
560
|
+
function parseProduct(product, affiliation) {
|
|
561
|
+
// 1. Move key/value pairs from product.Attributes to be at the same level
|
|
562
|
+
// as all keys in product, limiting them to 10 in the process.
|
|
563
|
+
|
|
564
|
+
var productWithAllAttributes = {};
|
|
417
565
|
|
|
418
|
-
|
|
566
|
+
if (affiliation) {
|
|
567
|
+
productWithAllAttributes.affiliation = affiliation;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
productWithAllAttributes = self$1.common.mergeObjects(
|
|
571
|
+
productWithAllAttributes,
|
|
572
|
+
self$1.common.limitProductAttributes(product.Attributes)
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
// 2. Copy key/value pairs in product
|
|
576
|
+
for (var key in product) {
|
|
419
577
|
switch (key) {
|
|
420
578
|
case 'Sku':
|
|
421
|
-
|
|
579
|
+
productWithAllAttributes.item_id = product.Sku;
|
|
422
580
|
break;
|
|
423
581
|
case 'Name':
|
|
424
|
-
|
|
582
|
+
productWithAllAttributes.item_name = product.Name;
|
|
425
583
|
break;
|
|
426
584
|
case 'Brand':
|
|
427
|
-
|
|
585
|
+
productWithAllAttributes.item_brand = product.Brand;
|
|
428
586
|
break;
|
|
429
587
|
case 'Category':
|
|
430
|
-
|
|
588
|
+
productWithAllAttributes.item_category = product.Category;
|
|
589
|
+
break;
|
|
590
|
+
case 'CouponCode':
|
|
591
|
+
productWithAllAttributes.coupon = product.CouponCode;
|
|
431
592
|
break;
|
|
432
593
|
case 'Variant':
|
|
433
|
-
|
|
594
|
+
productWithAllAttributes.item_variant = product.Variant;
|
|
595
|
+
break;
|
|
596
|
+
case 'Attributes':
|
|
434
597
|
break;
|
|
435
598
|
default:
|
|
436
|
-
|
|
599
|
+
productWithAllAttributes[toUnderscore(key)] = product[key];
|
|
437
600
|
}
|
|
438
601
|
}
|
|
439
602
|
|
|
440
|
-
|
|
603
|
+
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5716
|
|
604
|
+
if (self$1.common.forwarderSettings.enableDataCleansing) {
|
|
605
|
+
return self$1.common.standardizeParameters(productWithAllAttributes);
|
|
606
|
+
} else {
|
|
607
|
+
return productWithAllAttributes;
|
|
608
|
+
}
|
|
441
609
|
}
|
|
442
610
|
|
|
443
611
|
function parsePromotion(_promotion) {
|
|
@@ -462,14 +630,18 @@ function parsePromotion(_promotion) {
|
|
|
462
630
|
}
|
|
463
631
|
}
|
|
464
632
|
|
|
465
|
-
|
|
633
|
+
if (self$1.common.forwarderSettings.enableDataCleansing) {
|
|
634
|
+
return self$1.common.standardizeParameters(promotion);
|
|
635
|
+
} else {
|
|
636
|
+
return promotion;
|
|
637
|
+
}
|
|
466
638
|
}
|
|
467
639
|
|
|
468
|
-
function buildProductsList(products) {
|
|
640
|
+
function buildProductsList(products, affiliation) {
|
|
469
641
|
var productsList = [];
|
|
470
642
|
|
|
471
643
|
products.forEach(function (product) {
|
|
472
|
-
productsList.push(parseProduct(product));
|
|
644
|
+
productsList.push(parseProduct(product, affiliation));
|
|
473
645
|
});
|
|
474
646
|
|
|
475
647
|
return productsList;
|
|
@@ -529,7 +701,7 @@ function getCheckoutOptionEventName(customFlags) {
|
|
|
529
701
|
// Google previously had a CheckoutOption event, and now this has been split into 2 GA4 events - add_shipping_info and add_payment_info
|
|
530
702
|
// Since MP still uses CheckoutOption, we must map this to the 2 events using custom flags. To prevent any data loss from customers
|
|
531
703
|
// migrating from UA to GA4, we will set a default `set_checkout_option` which matches Firebase's data model.
|
|
532
|
-
function logCheckoutOptionEvent(event) {
|
|
704
|
+
function logCheckoutOptionEvent(event, affiliation) {
|
|
533
705
|
try {
|
|
534
706
|
var customFlags = event.CustomFlags,
|
|
535
707
|
GA4CommerceEventType = customFlags[GA4_COMMERCE_EVENT_TYPE],
|
|
@@ -544,13 +716,22 @@ function logCheckoutOptionEvent(event) {
|
|
|
544
716
|
|
|
545
717
|
switch (GA4CommerceEventType) {
|
|
546
718
|
case ADD_SHIPPING_INFO:
|
|
547
|
-
ga4CommerceEventParameters = buildAddShippingInfo(
|
|
719
|
+
ga4CommerceEventParameters = buildAddShippingInfo(
|
|
720
|
+
event,
|
|
721
|
+
affiliation
|
|
722
|
+
);
|
|
548
723
|
break;
|
|
549
724
|
case ADD_PAYMENT_INFO:
|
|
550
|
-
ga4CommerceEventParameters = buildAddPaymentInfo(
|
|
725
|
+
ga4CommerceEventParameters = buildAddPaymentInfo(
|
|
726
|
+
event,
|
|
727
|
+
affiliation
|
|
728
|
+
);
|
|
551
729
|
break;
|
|
552
730
|
default:
|
|
553
|
-
ga4CommerceEventParameters = buildCheckoutOptions(
|
|
731
|
+
ga4CommerceEventParameters = buildCheckoutOptions(
|
|
732
|
+
event,
|
|
733
|
+
affiliation
|
|
734
|
+
);
|
|
554
735
|
break;
|
|
555
736
|
}
|
|
556
737
|
} catch (error) {
|
|
@@ -587,11 +768,14 @@ function logPromotionEvent(event) {
|
|
|
587
768
|
return false;
|
|
588
769
|
}
|
|
589
770
|
|
|
590
|
-
function logImpressionEvent(event) {
|
|
771
|
+
function logImpressionEvent(event, affiliation) {
|
|
591
772
|
try {
|
|
592
773
|
var ga4CommerceEventParameters;
|
|
593
774
|
event.ProductImpressions.forEach(function (impression) {
|
|
594
|
-
ga4CommerceEventParameters = parseImpression(
|
|
775
|
+
ga4CommerceEventParameters = parseImpression(
|
|
776
|
+
impression,
|
|
777
|
+
affiliation
|
|
778
|
+
);
|
|
595
779
|
|
|
596
780
|
gtag(
|
|
597
781
|
'event',
|
|
@@ -609,8 +793,12 @@ function logImpressionEvent(event) {
|
|
|
609
793
|
return true;
|
|
610
794
|
}
|
|
611
795
|
|
|
612
|
-
function logViewCart(event) {
|
|
613
|
-
var ga4CommerceEventParameters = buildViewCart(event);
|
|
796
|
+
function logViewCart(event, affiliation) {
|
|
797
|
+
var ga4CommerceEventParameters = buildViewCart(event, affiliation);
|
|
798
|
+
ga4CommerceEventParameters = self$1.common.mergeObjects(
|
|
799
|
+
ga4CommerceEventParameters,
|
|
800
|
+
self$1.common.limitEventAttributes(event.EventAttributes)
|
|
801
|
+
);
|
|
614
802
|
ga4CommerceEventParameters.currency = event.CurrencyCode;
|
|
615
803
|
|
|
616
804
|
ga4CommerceEventParameters.value =
|
|
@@ -621,9 +809,9 @@ function logViewCart(event) {
|
|
|
621
809
|
return true;
|
|
622
810
|
}
|
|
623
811
|
|
|
624
|
-
function buildViewCart(event) {
|
|
812
|
+
function buildViewCart(event, affiliation) {
|
|
625
813
|
return {
|
|
626
|
-
items: buildProductsList(event.ProductAction.ProductList),
|
|
814
|
+
items: buildProductsList(event.ProductAction.ProductList, affiliation),
|
|
627
815
|
};
|
|
628
816
|
}
|
|
629
817
|
|
|
@@ -633,11 +821,27 @@ function EventHandler(common) {
|
|
|
633
821
|
this.common = common || {};
|
|
634
822
|
}
|
|
635
823
|
|
|
824
|
+
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5715
|
|
636
825
|
EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
|
|
826
|
+
var standardizedEventName;
|
|
827
|
+
var standardizedAttributes;
|
|
828
|
+
if (this.common.forwarderSettings.enableDataCleansing) {
|
|
829
|
+
standardizedEventName = this.common.standardizeName(eventName);
|
|
830
|
+
standardizedAttributes =
|
|
831
|
+
this.common.standardizeParameters(eventAttributes);
|
|
832
|
+
} else {
|
|
833
|
+
standardizedEventName = eventName;
|
|
834
|
+
standardizedAttributes = eventAttributes;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
standardizedAttributes = this.common.limitEventAttributes(
|
|
838
|
+
standardizedAttributes
|
|
839
|
+
);
|
|
840
|
+
|
|
637
841
|
gtag(
|
|
638
842
|
'event',
|
|
639
|
-
this.common.truncateEventName(
|
|
640
|
-
this.common.truncateEventAttributes(
|
|
843
|
+
this.common.truncateEventName(standardizedEventName),
|
|
844
|
+
this.common.truncateEventAttributes(standardizedAttributes)
|
|
641
845
|
);
|
|
642
846
|
};
|
|
643
847
|
|
|
@@ -777,11 +981,14 @@ var initialization = {
|
|
|
777
981
|
mParticle._setIntegrationDelay(this.moduleId, true);
|
|
778
982
|
|
|
779
983
|
common.forwarderSettings = forwarderSettings;
|
|
984
|
+
common.forwarderSettings.enableDataCleansing =
|
|
985
|
+
common.forwarderSettings.enableDataCleansing === 'True';
|
|
780
986
|
var measurementId = forwarderSettings.measurementId;
|
|
781
987
|
var userIdType = forwarderSettings.externalUserIdentityType;
|
|
782
988
|
var hashUserId = forwarderSettings.hashUserId;
|
|
989
|
+
|
|
783
990
|
var configSettings = {
|
|
784
|
-
send_page_view: forwarderSettings.enablePageView === 'True'
|
|
991
|
+
send_page_view: forwarderSettings.enablePageView === 'True'
|
|
785
992
|
};
|
|
786
993
|
window.dataLayer = window.dataLayer || [];
|
|
787
994
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mparticle/web-google-analytics-4-client-kit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
|
|
5
5
|
"description": "mParticle integration sdk for Google Analytics",
|
|
6
6
|
"main": "dist/GoogleAnalytics4EventForwarderClientSide-Kit.common.js",
|