@mparticle/web-google-analytics-4-client-kit 1.0.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.
- package/.eslintrc +30 -0
- package/README.md +28 -0
- package/dist/.keep +0 -0
- package/dist/GoogleAnalytics4EventForwarderClientSide-Kit.common.js +1315 -0
- package/dist/GoogleAnalytics4EventForwarderClientSide-Kit.iife.js +1318 -0
- package/package.json +46 -0
- package/rollup.config.js +52 -0
- package/src/commerce-handler.js +463 -0
- package/src/common.js +104 -0
- package/src/event-handler.js +36 -0
- package/src/identity-handler.js +68 -0
- package/src/initialization.js +75 -0
- package/src/session-handler.js +10 -0
- package/src/user-attribute-handler.js +27 -0
- package/test/boilerplate/karma.config.js +19 -0
- package/test/boilerplate/mockhttprequest.js +472 -0
- package/test/boilerplate/test-index.js +8 -0
- package/test/boilerplate/test-karma.js +8 -0
- package/test/end-to-end-testapp/build/.keep +0 -0
- package/test/end-to-end-testapp/settings.js +10 -0
- package/test/index.html +32 -0
- package/test/settings.json +12 -0
- package/test/tests.js +1433 -0
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mparticle/web-google-analytics-4-client-kit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
|
|
5
|
+
"description": "mParticle integration sdk for Google Analytics",
|
|
6
|
+
"main": "dist/GoogleAnalytics4EventForwarder-Kit.common.js",
|
|
7
|
+
"browser": "dist/GoogleAnalytics4EventForwarder-Kit.iife.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "node test/boilerplate/test-karma.js",
|
|
10
|
+
"build": "ENVIRONMENT=production rollup --config rollup.config.js",
|
|
11
|
+
"watch": "ENVIRONMENT=production rollup --config rollup.config.js -w",
|
|
12
|
+
"testEndToEnd": "ENVIRONMENT=testEndToEnd rollup --config rollup.config.js && open http://localhost:8082/node_modules/@mparticle/web-kit-wrapper/end-to-end-testapp/index.html && node node_modules/@mparticle/web-kit-wrapper/end-to-end-testapp/server"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-google-analytics-4/tree/master/packages/GA4Client",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@mparticle/web-kit-wrapper": "^1.0.5",
|
|
20
|
+
"mocha": "^5.2.0",
|
|
21
|
+
"chai": "^4.2.0",
|
|
22
|
+
"karma": "^3.1.1",
|
|
23
|
+
"karma-chai": "^0.1.0",
|
|
24
|
+
"karma-chrome-launcher": "^2.2.0",
|
|
25
|
+
"karma-edge-launcher": "^0.4.2",
|
|
26
|
+
"karma-firefox-launcher": "^1.1.0",
|
|
27
|
+
"karma-ie-launcher": "^1.0.0",
|
|
28
|
+
"karma-junit-reporter": "^1.2.0",
|
|
29
|
+
"karma-mocha": "^1.3.0",
|
|
30
|
+
"karma-safari-launcher": "^1.0.0",
|
|
31
|
+
"rollup": "^1.15.6",
|
|
32
|
+
"rollup-plugin-commonjs": "^10.0.0",
|
|
33
|
+
"rollup-plugin-node-resolve": "^5.0.3",
|
|
34
|
+
"shelljs": "0.8.3",
|
|
35
|
+
"eslint": "^7.25.0",
|
|
36
|
+
"eslint-config-prettier": "8.3.0",
|
|
37
|
+
"eslint-plugin-prettier": "3.4.1",
|
|
38
|
+
"should": "13.2.3",
|
|
39
|
+
"prettier": "^2.4.1",
|
|
40
|
+
"watchify": "^3.11.0"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@mparticle/web-sdk": "^2.12.2"
|
|
44
|
+
},
|
|
45
|
+
"license": "Apache-2.0"
|
|
46
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// This file is all boilerplate. Do not edit unless you know what you're doing.
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
production,
|
|
5
|
+
testEndToEnd,
|
|
6
|
+
} from './node_modules/@mparticle/web-kit-wrapper/rollup.base';
|
|
7
|
+
|
|
8
|
+
const { ENVIRONMENT } = process.env;
|
|
9
|
+
const initialization = require('./src/initialization');
|
|
10
|
+
|
|
11
|
+
const productionBuilds = {
|
|
12
|
+
iife: {
|
|
13
|
+
input: production.input,
|
|
14
|
+
output: {
|
|
15
|
+
...production.output,
|
|
16
|
+
format: 'iife',
|
|
17
|
+
file: `dist/${initialization.name}ClientSide-Kit.iife.js`,
|
|
18
|
+
name: `${initialization.name}Kit`,
|
|
19
|
+
},
|
|
20
|
+
plugins: [...production.plugins],
|
|
21
|
+
},
|
|
22
|
+
cjs: {
|
|
23
|
+
input: production.input,
|
|
24
|
+
output: {
|
|
25
|
+
...production.output,
|
|
26
|
+
format: 'cjs',
|
|
27
|
+
file: `dist/${initialization.name}ClientSide-Kit.common.js`,
|
|
28
|
+
name: `${initialization.name}Kit`,
|
|
29
|
+
},
|
|
30
|
+
plugins: [...production.plugins],
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const testEndToEndBuild = {
|
|
35
|
+
input: testEndToEnd.input,
|
|
36
|
+
output: {
|
|
37
|
+
...testEndToEnd.output,
|
|
38
|
+
format: 'iife',
|
|
39
|
+
file: 'test/end-to-end-testapp/build/compilation.js',
|
|
40
|
+
name: `${initialization.name}Kit`,
|
|
41
|
+
},
|
|
42
|
+
plugins: [...testEndToEnd.plugins],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
let selectedBuilds = [];
|
|
46
|
+
if (ENVIRONMENT === 'production') {
|
|
47
|
+
selectedBuilds.push(productionBuilds.iife, productionBuilds.cjs);
|
|
48
|
+
} else if (ENVIRONMENT === 'testEndToEnd') {
|
|
49
|
+
selectedBuilds.push(testEndToEndBuild);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default selectedBuilds;
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
function CommerceHandler(common) {
|
|
2
|
+
this.common = common || {};
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
var ProductActionTypes = {
|
|
6
|
+
Unknown: 0,
|
|
7
|
+
AddToCart: 10,
|
|
8
|
+
Click: 14,
|
|
9
|
+
Checkout: 12,
|
|
10
|
+
CheckoutOption: 13,
|
|
11
|
+
Impression: 22,
|
|
12
|
+
Purchase: 16,
|
|
13
|
+
Refund: 17,
|
|
14
|
+
RemoveFromCart: 11,
|
|
15
|
+
ViewDetail: 15,
|
|
16
|
+
AddToWishlist: 20,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
var PromotionActionTypes = {
|
|
20
|
+
PromotionClick: 19,
|
|
21
|
+
PromotionView: 18,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Custom Flags
|
|
25
|
+
var GA4_COMMERCE_EVENT_TYPE = 'GA4.CommerceEventType',
|
|
26
|
+
GA4_SHIPPING_TIER = 'GA4.ShippingTier',
|
|
27
|
+
GA4_PAYMENT_TYPE = 'GA4.PaymentType';
|
|
28
|
+
|
|
29
|
+
var ADD_SHIPPING_INFO = 'add_shipping_info',
|
|
30
|
+
ADD_PAYMENT_INFO = 'add_payment_info',
|
|
31
|
+
VIEW_CART = 'view_cart';
|
|
32
|
+
|
|
33
|
+
CommerceHandler.prototype.logCommerceEvent = function (event) {
|
|
34
|
+
var needsCurrency = true,
|
|
35
|
+
needsValue = true,
|
|
36
|
+
ga4CommerceEventParameters,
|
|
37
|
+
isViewCartEvent = false;
|
|
38
|
+
|
|
39
|
+
// GA4 has a view_cart event which MP does not support via a ProductActionType
|
|
40
|
+
// In order to log a view_cart event, pass ProductActionType.Unknown along with
|
|
41
|
+
// the proper custom flag
|
|
42
|
+
// Unknown ProductActionTypes without a custom flag will reach the switch statement
|
|
43
|
+
// below and throw an error to the customer
|
|
44
|
+
if (event.EventCategory === ProductActionTypes.Unknown) {
|
|
45
|
+
if (
|
|
46
|
+
event.CustomFlags &&
|
|
47
|
+
event.CustomFlags[GA4_COMMERCE_EVENT_TYPE] === VIEW_CART
|
|
48
|
+
) {
|
|
49
|
+
isViewCartEvent = true;
|
|
50
|
+
return logViewCart(event);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Handle Impressions
|
|
54
|
+
if (event.EventCategory === ProductActionTypes.Impression) {
|
|
55
|
+
return logImpressionEvent(event);
|
|
56
|
+
// Handle Promotions
|
|
57
|
+
} else if (
|
|
58
|
+
event.EventCategory === PromotionActionTypes.PromotionClick ||
|
|
59
|
+
event.EventCategory === PromotionActionTypes.PromotionView
|
|
60
|
+
) {
|
|
61
|
+
return logPromotionEvent(event);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
ga4CommerceEventParameters = buildParameters(event);
|
|
65
|
+
if (event.EventAttributes) {
|
|
66
|
+
ga4CommerceEventParameters = this.common.mergeObjects(
|
|
67
|
+
ga4CommerceEventParameters,
|
|
68
|
+
event.EventAttributes
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
switch (event.EventCategory) {
|
|
73
|
+
case ProductActionTypes.AddToCart:
|
|
74
|
+
case ProductActionTypes.RemoveFromCart:
|
|
75
|
+
ga4CommerceEventParameters = buildAddOrRemoveCartItem(event);
|
|
76
|
+
break;
|
|
77
|
+
case ProductActionTypes.Checkout:
|
|
78
|
+
ga4CommerceEventParameters = buildCheckout(event);
|
|
79
|
+
break;
|
|
80
|
+
case ProductActionTypes.Click:
|
|
81
|
+
ga4CommerceEventParameters = buildSelectItem(event);
|
|
82
|
+
|
|
83
|
+
needsCurrency = false;
|
|
84
|
+
needsValue = false;
|
|
85
|
+
break;
|
|
86
|
+
case ProductActionTypes.Purchase:
|
|
87
|
+
ga4CommerceEventParameters = buildPurchase(event);
|
|
88
|
+
break;
|
|
89
|
+
case ProductActionTypes.Refund:
|
|
90
|
+
ga4CommerceEventParameters = buildRefund(event);
|
|
91
|
+
break;
|
|
92
|
+
case ProductActionTypes.ViewDetail:
|
|
93
|
+
ga4CommerceEventParameters = buildViewItem(event);
|
|
94
|
+
break;
|
|
95
|
+
case ProductActionTypes.AddToWishlist:
|
|
96
|
+
ga4CommerceEventParameters = buildAddToWishlist(event);
|
|
97
|
+
break;
|
|
98
|
+
|
|
99
|
+
case ProductActionTypes.CheckoutOption:
|
|
100
|
+
return logCheckoutOptionEvent(event);
|
|
101
|
+
|
|
102
|
+
default:
|
|
103
|
+
// a view cart event is handled at the beginning of this function
|
|
104
|
+
if (!isViewCartEvent) {
|
|
105
|
+
console.error(
|
|
106
|
+
'Unsupported Commerce Event. Event not sent.',
|
|
107
|
+
event
|
|
108
|
+
);
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// CheckoutOption, Promotions, and Impressions will not make it to this code
|
|
114
|
+
if (needsCurrency) {
|
|
115
|
+
ga4CommerceEventParameters.currency = event.CurrencyCode;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (needsValue) {
|
|
119
|
+
ga4CommerceEventParameters.value =
|
|
120
|
+
(event.CustomFlags && event.CustomFlags['GA4.Value']) ||
|
|
121
|
+
event.ProductAction.TotalAmount ||
|
|
122
|
+
null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
|
|
126
|
+
return true;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
function buildParameters(event) {
|
|
130
|
+
return {
|
|
131
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
132
|
+
coupon: event.ProductAction ? event.ProductAction.CouponCode : null,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function buildAddOrRemoveCartItem(event) {
|
|
137
|
+
return {
|
|
138
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function buildCheckout(event) {
|
|
143
|
+
return {
|
|
144
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
145
|
+
coupon: event.ProductAction ? event.ProductAction.CouponCode : null,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function buildCheckoutOptions(event) {
|
|
150
|
+
var parameters = event.EventAttributes;
|
|
151
|
+
parameters.items = buildProductsList(event.ProductAction.ProductList);
|
|
152
|
+
parameters.coupon = event.ProductAction
|
|
153
|
+
? event.ProductAction.CouponCode
|
|
154
|
+
: null;
|
|
155
|
+
|
|
156
|
+
return parameters;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function parseImpression(impression) {
|
|
160
|
+
return {
|
|
161
|
+
item_list_id: impression.ProductImpressionList,
|
|
162
|
+
item_list_name: impression.ProductImpressionList,
|
|
163
|
+
items: buildProductsList(impression.ProductList),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function buildSelectItem(event) {
|
|
168
|
+
return {
|
|
169
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function buildViewItem(event) {
|
|
174
|
+
return {
|
|
175
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function buildPromotion(promotion) {
|
|
180
|
+
return parsePromotion(promotion);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function buildPurchase(event) {
|
|
184
|
+
return {
|
|
185
|
+
transaction_id: event.ProductAction.TransactionId,
|
|
186
|
+
value: event.ProductAction.TotalAmount,
|
|
187
|
+
affiliation: event.ProductAction.Affiliation,
|
|
188
|
+
coupon: event.ProductAction.CouponCode,
|
|
189
|
+
shipping: event.ProductAction.ShippingAmount,
|
|
190
|
+
tax: event.ProductAction.TaxAmount,
|
|
191
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function buildRefund(event) {
|
|
195
|
+
return {
|
|
196
|
+
transaction_id: event.ProductAction.TransactionId,
|
|
197
|
+
value: event.ProductAction.TotalAmount,
|
|
198
|
+
affiliation: event.ProductAction.Affiliation,
|
|
199
|
+
coupon: event.ProductAction.CouponCode,
|
|
200
|
+
shipping: event.ProductAction.ShippingAmount,
|
|
201
|
+
tax: event.ProductAction.TaxAmount,
|
|
202
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function buildAddToWishlist(event) {
|
|
206
|
+
return {
|
|
207
|
+
value: event.ProductAction.TotalAmount,
|
|
208
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function buildAddShippingInfo(event) {
|
|
213
|
+
return {
|
|
214
|
+
coupon:
|
|
215
|
+
event.ProductAction && event.ProductAction.CouponCode
|
|
216
|
+
? event.ProductAction.CouponCode
|
|
217
|
+
: null,
|
|
218
|
+
shipping_tier:
|
|
219
|
+
event.CustomFlags && event.CustomFlags[GA4_SHIPPING_TIER]
|
|
220
|
+
? event.CustomFlags[GA4_SHIPPING_TIER]
|
|
221
|
+
: null,
|
|
222
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function buildAddPaymentInfo(event) {
|
|
227
|
+
return {
|
|
228
|
+
coupon:
|
|
229
|
+
event.ProductAction && event.ProductAction.CouponCode
|
|
230
|
+
? event.ProductAction.CouponCode
|
|
231
|
+
: null,
|
|
232
|
+
payment_type:
|
|
233
|
+
event.CustomFlags && event.CustomFlags[GA4_PAYMENT_TYPE]
|
|
234
|
+
? event.CustomFlags[GA4_PAYMENT_TYPE]
|
|
235
|
+
: null,
|
|
236
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Utility function
|
|
241
|
+
function toUnderscore(string) {
|
|
242
|
+
return string
|
|
243
|
+
.split(/(?=[A-Z])/)
|
|
244
|
+
.join('_')
|
|
245
|
+
.toLowerCase();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function parseProduct(_product) {
|
|
249
|
+
var product = {};
|
|
250
|
+
|
|
251
|
+
for (var key in _product) {
|
|
252
|
+
switch (key) {
|
|
253
|
+
case 'Sku':
|
|
254
|
+
product.item_id = _product.Sku;
|
|
255
|
+
break;
|
|
256
|
+
case 'Name':
|
|
257
|
+
product.item_name = _product.Name;
|
|
258
|
+
break;
|
|
259
|
+
case 'Brand':
|
|
260
|
+
product.item_brand = _product.Brand;
|
|
261
|
+
break;
|
|
262
|
+
case 'Category':
|
|
263
|
+
product.item_category = _product.Category;
|
|
264
|
+
break;
|
|
265
|
+
case 'Variant':
|
|
266
|
+
product.item_variant = _product.Variant;
|
|
267
|
+
break;
|
|
268
|
+
default:
|
|
269
|
+
product[toUnderscore(key)] = _product[key];
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return product;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function parsePromotion(_promotion) {
|
|
277
|
+
var promotion = {};
|
|
278
|
+
|
|
279
|
+
for (var key in _promotion) {
|
|
280
|
+
switch (key) {
|
|
281
|
+
case 'Id':
|
|
282
|
+
promotion.promotion_id = _promotion.Id;
|
|
283
|
+
break;
|
|
284
|
+
case 'Creative':
|
|
285
|
+
promotion.creative_name = _promotion.Creative;
|
|
286
|
+
break;
|
|
287
|
+
case 'Name':
|
|
288
|
+
promotion.promotion_name = _promotion.Name;
|
|
289
|
+
break;
|
|
290
|
+
case 'Position':
|
|
291
|
+
promotion.creative_slot = _promotion.Position;
|
|
292
|
+
break;
|
|
293
|
+
default:
|
|
294
|
+
promotion[toUnderscore(key)] = _promotion[key];
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return promotion;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function buildProductsList(products) {
|
|
302
|
+
var productsList = [];
|
|
303
|
+
|
|
304
|
+
products.forEach(function (product) {
|
|
305
|
+
productsList.push(parseProduct(product));
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
return productsList;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function mapGA4EcommerceEventName(event) {
|
|
312
|
+
switch (event.EventCategory) {
|
|
313
|
+
case ProductActionTypes.AddToCart:
|
|
314
|
+
return 'add_to_cart';
|
|
315
|
+
case ProductActionTypes.AddToWishlist:
|
|
316
|
+
return 'add_to_wishlist';
|
|
317
|
+
case ProductActionTypes.RemoveFromCart:
|
|
318
|
+
return 'remove_from_cart';
|
|
319
|
+
case ProductActionTypes.Purchase:
|
|
320
|
+
return 'purchase';
|
|
321
|
+
case ProductActionTypes.Checkout:
|
|
322
|
+
return 'begin_checkout';
|
|
323
|
+
case ProductActionTypes.CheckoutOption:
|
|
324
|
+
return getCheckoutOptionEventName(event.CustomFlags);
|
|
325
|
+
case ProductActionTypes.Click:
|
|
326
|
+
return 'select_item';
|
|
327
|
+
case ProductActionTypes.Impression:
|
|
328
|
+
return 'view_item_list';
|
|
329
|
+
case ProductActionTypes.Refund:
|
|
330
|
+
return 'refund';
|
|
331
|
+
case ProductActionTypes.ViewDetail:
|
|
332
|
+
return 'view_item';
|
|
333
|
+
case PromotionActionTypes.PromotionClick:
|
|
334
|
+
return 'select_promotion';
|
|
335
|
+
case PromotionActionTypes.PromotionView:
|
|
336
|
+
return 'view_promotion';
|
|
337
|
+
case ProductActionTypes.Unknown:
|
|
338
|
+
if (
|
|
339
|
+
event.CustomFlags &&
|
|
340
|
+
event.CustomFlags[GA4_COMMERCE_EVENT_TYPE] === VIEW_CART
|
|
341
|
+
) {
|
|
342
|
+
return 'view_cart';
|
|
343
|
+
}
|
|
344
|
+
break;
|
|
345
|
+
default:
|
|
346
|
+
console.error('Product Action Type not supported');
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function getCheckoutOptionEventName(customFlags) {
|
|
352
|
+
switch (customFlags[GA4_COMMERCE_EVENT_TYPE]) {
|
|
353
|
+
case ADD_SHIPPING_INFO:
|
|
354
|
+
return ADD_SHIPPING_INFO;
|
|
355
|
+
case ADD_PAYMENT_INFO:
|
|
356
|
+
return ADD_PAYMENT_INFO;
|
|
357
|
+
default:
|
|
358
|
+
return 'set_checkout_option';
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Google previously had a CheckoutOption event, and now this has been split into 2 GA4 events - add_shipping_info and add_payment_info
|
|
363
|
+
// Since MP still uses CheckoutOption, we must map this to the 2 events using custom flags. To prevent any data loss from customers
|
|
364
|
+
// migrating from UA to GA4, we will set a default `set_checkout_option` which matches Firebase's data model.
|
|
365
|
+
function logCheckoutOptionEvent(event) {
|
|
366
|
+
try {
|
|
367
|
+
var customFlags = event.CustomFlags,
|
|
368
|
+
GA4CommerceEventType = customFlags[GA4_COMMERCE_EVENT_TYPE],
|
|
369
|
+
ga4CommerceEventParameters;
|
|
370
|
+
|
|
371
|
+
// if no custom flags exist or there is no GA4CommerceEventType, the user has not updated their code from using legacy GA which still supports checkout_option
|
|
372
|
+
if (!customFlags || !GA4CommerceEventType) {
|
|
373
|
+
console.error(
|
|
374
|
+
'Your checkout option event for the Google Analytics 4 integration is missing a custom flag for "GA4.CommerceEventType". The event was sent using the deprecated set_checkout_option event. Review mParticle docs for GA4 for the custom flags to add.'
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
switch (GA4CommerceEventType) {
|
|
379
|
+
case ADD_SHIPPING_INFO:
|
|
380
|
+
ga4CommerceEventParameters = buildAddShippingInfo(event);
|
|
381
|
+
break;
|
|
382
|
+
case ADD_PAYMENT_INFO:
|
|
383
|
+
ga4CommerceEventParameters = buildAddPaymentInfo(event);
|
|
384
|
+
break;
|
|
385
|
+
default:
|
|
386
|
+
ga4CommerceEventParameters = buildCheckoutOptions(event);
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
} catch (error) {
|
|
390
|
+
console.error(
|
|
391
|
+
'There is an issue with the custom flags in your CheckoutOption event. The event was not sent. Plrease review the docs and fix.',
|
|
392
|
+
error
|
|
393
|
+
);
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
|
|
398
|
+
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function logPromotionEvent(event) {
|
|
403
|
+
try {
|
|
404
|
+
var ga4CommerceEventParameters;
|
|
405
|
+
event.PromotionAction.PromotionList.forEach(function (promotion) {
|
|
406
|
+
ga4CommerceEventParameters = buildPromotion(promotion);
|
|
407
|
+
gtag(
|
|
408
|
+
'event',
|
|
409
|
+
mapGA4EcommerceEventName(event),
|
|
410
|
+
ga4CommerceEventParameters
|
|
411
|
+
);
|
|
412
|
+
});
|
|
413
|
+
return true;
|
|
414
|
+
} catch (error) {
|
|
415
|
+
console.error(
|
|
416
|
+
'Error logging Promotions to GA4. Promotions not logged.',
|
|
417
|
+
error
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function logImpressionEvent(event) {
|
|
424
|
+
try {
|
|
425
|
+
var ga4CommerceEventParameters;
|
|
426
|
+
event.ProductImpressions.forEach(function (impression) {
|
|
427
|
+
ga4CommerceEventParameters = parseImpression(impression);
|
|
428
|
+
|
|
429
|
+
gtag(
|
|
430
|
+
'event',
|
|
431
|
+
mapGA4EcommerceEventName(event),
|
|
432
|
+
ga4CommerceEventParameters
|
|
433
|
+
);
|
|
434
|
+
});
|
|
435
|
+
} catch (error) {
|
|
436
|
+
console.log(
|
|
437
|
+
'Error logging Impressions to GA4. Impressions not logged',
|
|
438
|
+
error
|
|
439
|
+
);
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function logViewCart(event) {
|
|
446
|
+
var ga4CommerceEventParameters = buildViewCart(event);
|
|
447
|
+
ga4CommerceEventParameters.currency = event.CurrencyCode;
|
|
448
|
+
|
|
449
|
+
ga4CommerceEventParameters.value =
|
|
450
|
+
(event.CustomFlags && event.CustomFlags['GA4.Value']) ||
|
|
451
|
+
event.ProductAction.TotalAmount ||
|
|
452
|
+
null;
|
|
453
|
+
gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function buildViewCart(event) {
|
|
458
|
+
return {
|
|
459
|
+
items: buildProductsList(event.ProductAction.ProductList),
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
module.exports = CommerceHandler;
|
package/src/common.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
function Common() {}
|
|
2
|
+
|
|
3
|
+
Common.prototype.forwarderSettings = null;
|
|
4
|
+
|
|
5
|
+
Common.prototype.mergeObjects = function () {
|
|
6
|
+
var resObj = {};
|
|
7
|
+
for (var i = 0; i < arguments.length; i += 1) {
|
|
8
|
+
var obj = arguments[i],
|
|
9
|
+
keys = Object.keys(obj);
|
|
10
|
+
for (var j = 0; j < keys.length; j += 1) {
|
|
11
|
+
resObj[keys[j]] = obj[keys[j]];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return resObj;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
Common.prototype.getUserId = function (
|
|
18
|
+
user,
|
|
19
|
+
externalUserIdentityType,
|
|
20
|
+
hashUserId
|
|
21
|
+
) {
|
|
22
|
+
if (!user) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var externalUserIdentityTypes = {
|
|
27
|
+
none: 'None',
|
|
28
|
+
customerId: 'CustomerId',
|
|
29
|
+
other: 'Other',
|
|
30
|
+
other2: 'Other2',
|
|
31
|
+
other3: 'Other3',
|
|
32
|
+
other4: 'Other4',
|
|
33
|
+
other5: 'Other5',
|
|
34
|
+
other6: 'Other6',
|
|
35
|
+
other7: 'Other7',
|
|
36
|
+
other8: 'Other8',
|
|
37
|
+
other9: 'Other9',
|
|
38
|
+
other10: 'Other10',
|
|
39
|
+
mpid: 'mpid',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
var userId,
|
|
43
|
+
userIdentities = user.getUserIdentities().userIdentities;
|
|
44
|
+
if (externalUserIdentityType !== externalUserIdentityTypes.none) {
|
|
45
|
+
switch (externalUserIdentityType) {
|
|
46
|
+
case 'CustomerId':
|
|
47
|
+
userId = userIdentities.customerid;
|
|
48
|
+
break;
|
|
49
|
+
case externalUserIdentityTypes.other:
|
|
50
|
+
userId = userIdentities.other;
|
|
51
|
+
break;
|
|
52
|
+
case externalUserIdentityTypes.other2:
|
|
53
|
+
userId = userIdentities.other2;
|
|
54
|
+
break;
|
|
55
|
+
case externalUserIdentityTypes.other3:
|
|
56
|
+
userId = userIdentities.other3;
|
|
57
|
+
break;
|
|
58
|
+
case externalUserIdentityTypes.other4:
|
|
59
|
+
userId = userIdentities.other4;
|
|
60
|
+
break;
|
|
61
|
+
case externalUserIdentityTypes.other5:
|
|
62
|
+
userId = userIdentities.other5;
|
|
63
|
+
break;
|
|
64
|
+
case externalUserIdentityTypes.other6:
|
|
65
|
+
userId = userIdentities.other6;
|
|
66
|
+
break;
|
|
67
|
+
case externalUserIdentityTypes.other7:
|
|
68
|
+
userId = userIdentities.other7;
|
|
69
|
+
break;
|
|
70
|
+
case externalUserIdentityTypes.other8:
|
|
71
|
+
userId = userIdentities.other8;
|
|
72
|
+
break;
|
|
73
|
+
case externalUserIdentityTypes.other9:
|
|
74
|
+
userId = userIdentities.other9;
|
|
75
|
+
break;
|
|
76
|
+
case externalUserIdentityTypes.other10:
|
|
77
|
+
userId = userIdentities.other10;
|
|
78
|
+
break;
|
|
79
|
+
case externalUserIdentityTypes.mpid:
|
|
80
|
+
userId = user.getMPID();
|
|
81
|
+
break;
|
|
82
|
+
default:
|
|
83
|
+
console.warn(
|
|
84
|
+
'External identity type not found for setting identity on ' +
|
|
85
|
+
'GA4' +
|
|
86
|
+
'. User not set. Please double check your implementation.'
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (userId) {
|
|
91
|
+
if (hashUserId == 'True') {
|
|
92
|
+
userId = window.mParticle.generateHash(userId);
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
console.warn(
|
|
96
|
+
'External identity type of ' +
|
|
97
|
+
externalUserIdentityType +
|
|
98
|
+
' not set on the user'
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return userId;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
module.exports = Common;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
function EventHandler(common) {
|
|
2
|
+
this.common = common || {};
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
EventHandler.prototype.logEvent = function (event) {
|
|
6
|
+
gtag('event', event.EventName, event.EventAttributes || {});
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
EventHandler.prototype.logError = function () {
|
|
10
|
+
console.warn('Google Analytics 4 does not support error events.');
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
EventHandler.prototype.logPageView = function (event) {
|
|
14
|
+
var TITLE = 'Google.Title';
|
|
15
|
+
var LOCATION = 'Google.Location';
|
|
16
|
+
var pageTitle, pageLocation;
|
|
17
|
+
if (event.CustomFlags.hasOwnProperty(TITLE)) {
|
|
18
|
+
pageTitle = event.CustomFlags[TITLE];
|
|
19
|
+
} else {
|
|
20
|
+
pageTitle = document.title;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (event.CustomFlags.hasOwnProperty(LOCATION)) {
|
|
24
|
+
pageLocation = event.CustomFlags[LOCATION];
|
|
25
|
+
} else {
|
|
26
|
+
pageLocation = location.href;
|
|
27
|
+
}
|
|
28
|
+
gtag('event', 'page_view', {
|
|
29
|
+
page_title: pageTitle,
|
|
30
|
+
page_location: pageLocation,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return true;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
module.exports = EventHandler;
|