-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.tpl
561 lines (539 loc) · 20.3 KB
/
template.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
___TERMS_OF_SERVICE___
By creating or modifying this file you agree to Google Tag Manager's Community
Template Gallery Developer Terms of Service available at
https://developers.google.com/tag-manager/gallery-tos (or such other URL as
Google may provide), as modified from time to time.
___INFO___
{
"type": "MACRO",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "GA4 Ecommerce to Enhanced Ecommerce Converter",
"description": "Convert GA4 Ecommerce to Universal Analytics Enhanced Ecommerce. It supports all EEC Actions, and also Product Scoped Dimensions/Metrics.",
"categories": [
"ANALYTICS",
"UTILITY",
"TAG_MANAGEMENT"
],
"containerContexts": [
"WEB"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "LABEL",
"name": "description",
"displayName": "This Variable makes it possible to convert GA4 Ecommerce Object \u0026 Events to Enhanced Ecommerce. You can choose GA4 Ecommerce Object, do Automatically or Manual Mapping, create Checkout, and map Product Scoped Dimensions/Metrics."
},
{
"type": "GROUP",
"name": "ecomInputGroup",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "RADIO",
"name": "ecomInputSelector",
"displayName": "Select GA4 Ecommerce Object",
"radioItems": [
{
"value": "ecomAuto",
"displayValue": "GA4 Ecommerce Object"
},
{
"value": "ecomGTMVariable",
"displayValue": "GTM Variable"
}
],
"simpleValueType": true
},
{
"type": "SELECT",
"name": "GTMVariable",
"displayName": "GTM Variable",
"macrosInSelect": true,
"selectItems": [],
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "ecomInputSelector",
"paramValue": "ecomGTMVariable",
"type": "EQUALS"
}
]
}
],
"help": "Choose if Enhanced Ecommerce Object should be created from \u003cb\u003eGA4 Ecommerce Object from DataLayer\u003c/b\u003e, or from a \u003cb\u003eGTM Variable\u003c/b\u003e. Use the last option if you for example have rewritten your GA4 Ecommerce data using a Custom Javascript Variable.",
"displayName": "GA4 Ecommerce Object"
},
{
"type": "RADIO",
"name": "mapEcomEvents",
"displayName": "Map GA4 Ecommerce to Enhanced Ecommerce Actions",
"radioItems": [
{
"value": "mapAuto",
"displayValue": "Map GA4 Ecommerce to Enhanced Ecommerce Actions Automatically"
},
{
"value": "mapMan",
"displayValue": "Map Manually"
}
],
"simpleValueType": true,
"help": "If your GA4 Ecommerce implementation is implemented with the \u003cb\u003ecorrect GA4 Events\u003c/b\u003e, choose Automatically mapping. If not, map manually.\nThis mapping does not include \u003cb\u003eEnhanced Checkout\u003c/b\u003e, that is done in the \u003cb\u003eCheckout Setup\u003c/b\u003e mapping."
},
{
"type": "GROUP",
"name": "mapEventsManually",
"displayName": "Map Variables Manually to Enhanced Ecommerce Actions",
"groupStyle": "ZIPPY_OPEN_ON_PARAM",
"subParams": [
{
"type": "SIMPLE_TABLE",
"name": "mapManVariables",
"displayName": "Map Variables to Enhanced Ecommerce Object",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Enhanced Ecommerce Action",
"name": "mapGA4Events",
"type": "SELECT",
"selectItems": [
{
"value": "view_item_list",
"displayValue": "Product/Item List Views/Impressions"
},
{
"value": "select_item",
"displayValue": "Product/Item List Clicks"
},
{
"value": "view_item",
"displayValue": "Product/Item Detail Views"
},
{
"value": "add_to_cart",
"displayValue": "Add to Cart"
},
{
"value": "view_cart",
"displayValue": "View Cart"
},
{
"value": "remove_from_cart",
"displayValue": "Remove from Cart"
},
{
"value": "purchase",
"displayValue": "Purchase"
},
{
"value": "refund",
"displayValue": "Refund"
},
{
"value": "view_promotion",
"displayValue": "Promotion Views/Impressions"
},
{
"value": "select_promotion",
"displayValue": "Promotion Clicks"
}
]
},
{
"defaultValue": "",
"displayName": "GTM Variable",
"name": "mapVariable",
"type": "TEXT",
"valueHint": "{{Event}}"
},
{
"defaultValue": "",
"displayName": "GTM Variable Value",
"name": "mapVariableValue",
"type": "TEXT",
"valueHint": "addToCart"
}
],
"newRowButtonText": "Add GTM Variable to Map to Enhanced Ecommerce Object",
"help": "If don\u0027t have correct \u003cb\u003eGA4 Ecom Events\u003c/b\u003e implemented with the GA4 Ecommerce Object, but you have a \u003cb\u003eGTM Variable\u003c/b\u003e connected to the GA4 Ecommerce Object, use this mapping.\n\n\u003cb\u003eCheckout\u003c/b\u003e has it\u0027s own mapping, and doesn\u0027t use this setting."
}
],
"enablingConditions": [
{
"paramName": "mapEcomEvents",
"paramValue": "mapMan",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "checkoutObject",
"displayName": "Checkout Setup",
"groupStyle": "ZIPPY_OPEN_ON_PARAM",
"subParams": [
{
"type": "LABEL",
"name": "checkoutDescription",
"displayName": "Map \u003cb\u003eEEC Checkout Actions, Steps \u0026 Options\u003c/b\u003e to your existing \u003cb\u003eGA4 Ecommerce implementation\u003c/b\u003e. Map using \u003cb\u003eGTM Events\u003c/b\u003e, \u003cb\u003eGTM Variables\u003c/b\u003e and \u003cb\u003eGTM Variable Values\u003c/b\u003e."
},
{
"type": "PARAM_TABLE",
"name": "checkoutSettings",
"displayName": "Checkout Settings",
"paramTableColumns": [
{
"param": {
"type": "SELECT",
"name": "checkoutAction",
"displayName": "EEC Checkout Action",
"selectItems": [
{
"value": "checkout",
"displayValue": "Checkout"
},
{
"value": "checkout_option",
"displayValue": "Checkout Option"
}
],
"simpleValueType": true,
"help": "\u003cb\u003eEnhanced Ecommerce Checkout Action\u003c/b\u003e is either a Checkout (\u003cb\u003echeckout\u003c/b\u003e) or a Checkout Option (\u003cb\u003echeckout_option\u003c/b\u003e)."
},
"isUnique": false
},
{
"param": {
"type": "SELECT",
"name": "checkoutStep",
"displayName": "EEC Checkout Step",
"selectItems": [
{
"value": 1,
"displayValue": "1"
},
{
"value": 2,
"displayValue": "2"
},
{
"value": 3,
"displayValue": "3"
},
{
"value": 4,
"displayValue": "4"
},
{
"value": 5,
"displayValue": "5"
},
{
"value": 6,
"displayValue": "6"
},
{
"value": 7,
"displayValue": "7"
},
{
"value": 8,
"displayValue": "8"
}
],
"simpleValueType": true,
"help": "Choose \u003cb\u003eCheckout Step Number\u003c/b\u003e."
},
"isUnique": false
},
{
"param": {
"type": "TEXT",
"name": "checkoutOption",
"displayName": "EEC Checkout Option",
"simpleValueType": true,
"help": "If you have a Variable/Parameter that contains \u003cb\u003eCheckout Option info\u003c/b\u003e (ex. \u003cb\u003epayment_type\u003c/b\u003e, add that Variable here. \nThis Value \u003cb\u003ecan\u003c/b\u003e be empty.",
"canBeEmptyString": true,
"notSetText": "N/A"
},
"isUnique": false
},
{
"param": {
"type": "TEXT",
"name": "GTMEvent",
"displayName": "GTM Event",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"valueHint": "begin_checkout",
"help": "Insert \u003cb\u003eGTM Event\u003c/b\u003e that would trigger this Checkout Step, ex. \u003ci\u003ebegin_checkout\u003c/i\u003e. If the GTM Event isn\u0027t enough to classify this step, fill out the \u003cb\u003eGTM Variable\u003c/b\u003e and \u003cb\u003eGTM Variable Value\u003c/b\u003e fields as well.\nThis Value \u003cb\u003ecan\u0027t\u003c/b\u003e be empty."
},
"isUnique": false
},
{
"param": {
"type": "TEXT",
"name": "GTMVariable",
"displayName": "GTM Variable",
"simpleValueType": true,
"help": "If the \u003cb\u003eGA4 Event\u003c/b\u003e alone isn\u0027t enough to classify the \u003cb\u003eEEC Checkout Step\u003c/b\u003e, maybe you have a \u003cb\u003eParameter (Dimension)\u003c/b\u003e, or can create a \u003cb\u003eGTM Variable\u003c/b\u003e that can help you with the classification. Insert the \u003cb\u003eGTM Variable\u003c/b\u003e into this field.\nThis Value \u003cb\u003ecan\u003c/b\u003e be empty.",
"canBeEmptyString": true,
"notSetText": "N/A"
},
"isUnique": false
},
{
"param": {
"type": "TEXT",
"name": "GTMVariableValue",
"displayName": "GTM Variable Value",
"simpleValueType": true,
"canBeEmptyString": true,
"help": "Insert a \u003cb\u003eGTM Variable Value\u003c/b\u003e that matches the output from the \u003cb\u003eGTM Variable\u003c/b\u003e and will help you classify this step.\nThis Value \u003cb\u003ecan\u003c/b\u003e be empty.",
"notSetText": "N/A"
},
"isUnique": false
}
],
"newRowButtonText": "Add Checkout Action \u0026 Checkout Step"
}
]
},
{
"type": "GROUP",
"name": "mapItemParameters",
"displayName": "Map Item Parameters to Product Scoped Dimensions or Metrics",
"groupStyle": "ZIPPY_OPEN_ON_PARAM",
"subParams": [
{
"type": "LABEL",
"name": "mapItemParametersDescription",
"displayName": "If your \u003cb\u003eGA4 Ecommerce Items\u003c/b\u003e contains \u003cb\u003eItem Parameters\u003c/b\u003e similiar to \u003cb\u003eEnhanced Ecommerce Product Scoped Dimensions/Metrics\u003c/b\u003e, use this table to map Item Parameter to corresponding Dimension/Metric."
},
{
"type": "SIMPLE_TABLE",
"name": "itemParamMapTable",
"displayName": "Map Item Parameters to Product Scoped Dimensions/Metrics",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Item Parameter Name",
"name": "itemParam",
"type": "TEXT",
"valueHint": "Ex. item_rating",
"isUnique": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"defaultValue": "",
"displayName": "Product Scoped Dimension/Metric",
"name": "paramIndex",
"type": "TEXT",
"isUnique": true,
"valueHint": "Ex. dimension1",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
}
],
"help": "Input \u003cstrong\u003eItem Parameters\u003c/strong\u003e and map them to \u003cstrong\u003eProduct Scoped Custom Dimensions\u003cstrong\u003e (ex. \u003ci\u003edimension1\u003c/i\u003e) or Metrics (ex. \u003ci\u003emetric1\u003c/i\u003e).\nSince GA4 doesn\u0027t have the Dimension/Metric concept, use this table to map \u003cstrong\u003eboth\u003c/strong\u003e Dimensions \u0026 Metrics.",
"newRowButtonText": "Map Item Parameter to Product Scoped Dimension/Metric"
}
]
}
]
___SANDBOXED_JS_FOR_WEB_TEMPLATE___
const dataLayer = require('copyFromDataLayer');
const JSON = require('JSON');
const makeTableMap = require('makeTableMap');
// Input settings
let ecom = data.GTMVariable ? data.GTMVariable : dataLayer('ecommerce', 1); // Data Layer Version 1
if(ecom) {
ecom = JSON.stringify(ecom).indexOf('ecommerce')>0 ? ecom.ecommerce : ecom;
let event = dataLayer('event',2);
const itemParamMapTable = data.itemParamMapTable ? makeTableMap(data.itemParamMapTable, 'itemParam', 'paramIndex'): undefined;
const checkoutEventTable = data.checkoutEventTable;
// Manual mapping of Variables if GA4 Ecom Events are Custom Events
if (data.mapManVariables && data.mapEcomEvents === 'mapMan') {
data.mapManVariables.forEach((eventArray) => {
if (eventArray.mapVariable === eventArray.mapVariableValue) {
event = eventArray.mapGA4Events;}
});
}
// EEC Ecom Action Definition
const eecEventMapping = {
'view_item_list': 'impressions',
'select_item': 'click',
'view_item': 'detail',
'add_to_cart': 'add',
'remove_from_cart': 'remove',
'purchase': 'purchase',
'refund': 'refund',
'select_promotion': 'promoClick',
'view_promotion': 'promoView'
};
let eecAction = eecEventMapping[event];
// Checkout Mapping
let checkoutOption;
let checkoutStep;
if (data.checkoutSettings) {
data.checkoutSettings.forEach((checkoutArray) => {
if (checkoutArray.GTMEvent === event && checkoutArray.GTMVariable === checkoutArray.GTMVariableValue) {
eecAction = checkoutArray.checkoutAction;
checkoutStep = checkoutArray.checkoutStep;
checkoutOption = checkoutArray.checkoutOption;
}
});
}
const eecItems = ecom.items ? ecom.items : undefined;
let currency = ecom.currency;
let affiliation = ecom.affiliation;
let item_list_name = ecom.item_list_name;
if (eecAction) {
if (eecAction === 'promoClick' || eecAction === 'promoView') {
// EEC Promotions
let promo;
const promotion_id = ecom.promotion_id ? ecom.promotion_id : undefined;
const promotion_name = ecom.promotion_name ? ecom.promotion_name : undefined;
const creative_name = ecom.creative_name ? ecom.creative_name : undefined;
const creative_slot = ecom.creative_slot ? ecom.creative_slot : undefined;
if (eecItems && !(promotion_id||promotion_name)) {
promo = [];
for(let i =0; i < eecItems.length && (eecItems[i].promotion_id||eecItems[i].promotion_name); i++){
promo.push({
'id': eecItems[i].promotion_id,
'name': eecItems[i].promotion_name,
'creative': eecItems[i].creative_name,
'position': eecItems[i].creative_slot
});
}
}
promo = promo ? promo : [{id:promotion_id, name:promotion_name, creative: creative_name, position: creative_slot}];
if (eecAction === 'promoClick') {
ecom = {ecommerce: {promoClick: {promotions: promo}}};
}
if (eecAction === 'promoView') {
ecom = {ecommerce: {promoView: {promotions: promo}}};
}
} else {
// EEC Products
currency = eecItems && eecItems.length > 0 ? eecItems[0].currency : currency;
affiliation = eecItems.length && !affiliation ? eecItems[0].affiliation : affiliation;
let items = [];
if(eecItems) {
for(let i =0; i < eecItems.length; i++){
let category;
const item = eecItems[i];
if (item.item_category) {category = item.item_category.toString().replace('/', '_');}
if (item.item_category2) {category = category + '/' + item.item_category2.toString().replace('/', '_');}
if (item.item_category3) {category = category + '/' + item.item_category3.toString().replace('/', '_');}
if (item.item_category4) {category = category + '/' + item.item_category4.toString().replace('/', '_');}
if (item.item_category5) {category = category + '/' + item.item_category5.toString().replace('/', '_');}
items.push({
'id': item.item_id,
'name': item.item_name,
'variant': item.item_variant,
'brand': item.item_brand,
'price': item.price,
'quantity': item.quantity,
'coupon': item.item_coupon,
'category': category,
'list': item.item_list_name ? item.item_list_name : item_list_name,
'position': item.index ? item.index : undefined
});
if (itemParamMapTable) {
for (let param in item) {
if (item.hasOwnProperty(param)) {
if (itemParamMapTable[param]) {
items[i][itemParamMapTable[param]] = item[param];
}
}
}
}
}
}
item_list_name = items && items.length > 0 ? items[0].list : item_list_name;
if (eecAction === 'impressions') {
ecom = {ecommerce: {currencyCode: currency, impressions: items}};
}
if (eecAction === 'click') {
ecom = {ecommerce: {currencyCode: currency, click: {actionField: {list: item_list_name}, products: items}}};
}
if (eecAction === 'detail') {
ecom = {ecommerce: {currencyCode: currency, detail: {actionField: {list: item_list_name}, products: items}}};
}
if (eecAction === 'add') {
ecom = {ecommerce: {currencyCode: currency, add: {actionField: {list: item_list_name}, products: items}}};
}
if (eecAction === 'remove') {
ecom = {ecommerce: {currencyCode: currency, remove: {products: items}}};
}
if (eecAction === 'purchase') {
const transaction_id = ecom.transaction_id, value = ecom.value, tax = ecom.tax, shipping = ecom.shipping, coupon = ecom.coupon;
ecom = {ecommerce: {currencyCode: currency, purchase: {actionField: {id: transaction_id, affiliation: affiliation, revenue: value, tax: tax, shipping: shipping, coupon: coupon}, products: items}}};
}
if (eecAction === 'refund') {
const transaction_id = ecom.transaction_id;
ecom = {ecommerce: {currencyCode: currency, refund: {actionField: {id: transaction_id}, products: items}}};
}
if (eecAction === 'checkout') {
ecom = {ecommerce: {ccurrencyCode: currency, checkout: {actionField: {step: checkoutStep, option: checkoutOption}, products: items}}};
}
if (eecAction === 'checkout_option') {
ecom = {ecommerce: {currencyCode: currency, checkout_option: {actionField: {step: checkoutStep, option: checkoutOption}}}};
}
}
return ecom;
}
}
___WEB_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_data_layer",
"versionId": "1"
},
"param": [
{
"key": "keyPatterns",
"value": {
"type": 2,
"listItem": [
{
"type": 1,
"string": "ecommerce.*"
},
{
"type": 1,
"string": "event"
}
]
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios: []
___NOTES___
Created on 10/19/2020, 2:11:38 PM