-
Notifications
You must be signed in to change notification settings - Fork 5
/
template.tpl
583 lines (529 loc) · 15.5 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
___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",
"__wm": "VGVtcGxhdGUtQXV0aG9yX2VlYy1wcm9kdWN0cy1nYTQtaXRlbXMtU2ltby1BaGF2YQ\u003d\u003d",
"categories": [
"UTILITY",
"TAG_MANAGEMENT"
],
"version": 1,
"securityGroups": [],
"displayName": "EEC Products -\u003e GA4 Items",
"description": "Converts Universal Analytics Enhanced Ecommerce products/impressions/promotions dataLayers to the format required by GA4\u0027s items array.",
"containerContexts": [
"WEB"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "RADIO",
"name": "option",
"displayName": "Map Source",
"radioItems": [
{
"value": "auto",
"displayValue": "Map ecommerce object automatically"
},
{
"value": "products",
"displayValue": "Map products array",
"subParams": [
{
"type": "SELECT",
"name": "productsVar",
"displayName": "",
"macrosInSelect": true,
"selectItems": [],
"simpleValueType": true,
"notSetText": "Select variable that returns a \"products\" array"
}
]
},
{
"value": "impressions",
"displayValue": "Map impressions array",
"subParams": [
{
"type": "SELECT",
"name": "impressionsVar",
"macrosInSelect": true,
"selectItems": [],
"simpleValueType": true,
"notSetText": "Select variable that returns an \"impressions\" array"
}
]
},
{
"value": "promotions",
"displayValue": "Map promotions array",
"subParams": [
{
"type": "SELECT",
"name": "promotionsVar",
"macrosInSelect": true,
"selectItems": [],
"simpleValueType": true,
"notSetText": "Select variable that returns a \"promotions\" array"
}
]
}
],
"simpleValueType": true,
"help": "Choose \u003cstrong\u003eMap ecommerce object automatically\u003c/strong\u003e to map the most recently pushed \u003cstrong\u003eecommerce\u003c/strong\u003e object\u0027s item array into the format required by Google Analytics 4. Select another option to reference a specific type of array directly."
},
{
"type": "GROUP",
"name": "mapGroup",
"displayName": "Map Custom Definitions To Item Parameters",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "LABEL",
"name": "customdeflabel",
"displayName": "If your Enhanced Ecommerce object contains product-scoped custom dimensions/metrics, you can use this table to map those into GA4 item parameter names. Input the index number (if using the prescribed \u003cstrong\u003edimensionXX\u003c/strong\u003e and \u003cstrong\u003emetricXX\u003c/strong\u003e syntax), or the name of the custom property (e.g. \u003cstrong\u003eproductSize\u003c/strong\u003e or \u003cstrong\u003estockCount\u003c/strong\u003e) in the first field. Input the parameter name with which the value should be sent to GA4 in the second."
},
{
"type": "SIMPLE_TABLE",
"name": "customDims",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Custom Dimension Index Or Parameter Name",
"name": "cdindex",
"type": "TEXT",
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"isUnique": true
},
{
"defaultValue": "",
"displayName": "Item Parameter Name",
"name": "cdparam",
"type": "TEXT",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
}
],
"newRowButtonText": "Add custom dimension map"
},
{
"type": "SIMPLE_TABLE",
"name": "customMets",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Custom Metric Index Or Parameter Name",
"name": "cmindex",
"type": "TEXT",
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"isUnique": true
},
{
"defaultValue": "",
"displayName": "Item Parameter Name",
"name": "cmparam",
"type": "TEXT",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
}
],
"newRowButtonText": "Add custom metric map"
}
]
}
]
___SANDBOXED_JS_FOR_WEB_TEMPLATE___
const copyFromDataLayer = require('copyFromDataLayer');
const getType = require('getType');
const makeNumber = require('makeNumber');
const makeTableMap = require('makeTableMap');
const math = require('Math');
const customDimMap = data.customDims ? makeTableMap(data.customDims, 'cdindex', 'cdparam') : {};
const customMetMap = data.customMets ? makeTableMap(data.customMets, 'cmindex', 'cmparam') : {};
const mapProductData = i => {
const category = i.category ? i.category.split('/') : [];
const itemObj = {
item_id: i.id,
item_name: i.name,
price: i.price,
item_brand: i.brand,
item_variant: i.variant,
quantity: i.quantity ? math.round(i.quantity) : i.quantity
};
category.forEach((c, i) => {
if (i === 0) itemObj.item_category = c;
else itemObj['item_category' + (i + 1)] = c;
});
for (let prop in i) {
if (customDimMap[prop]) { itemObj[customDimMap[prop]] = i[prop]; }
if (customMetMap[prop]) { itemObj[customMetMap[prop]] = i[prop]; }
if (prop.indexOf('dimension') === 0) {
let paramName = customDimMap[prop.split('dimension')[1]];
itemObj[paramName || prop] = i[prop];
}
if (prop.indexOf('metric') === 0) {
let paramName = customMetMap[prop.split('metric')[1]];
itemObj[paramName || prop] = makeNumber(i[prop]) || 0;
}
}
return itemObj;
};
const mapImpressionData = i => {
const impression = mapProductData(i);
impression.item_list_name = i.list;
impression.index = i.position;
return impression;
};
const mapPromotionData = i => {
return {
promotion_name: i.name,
promotion_id: i.id,
creative_name: i.creative,
creative_slot: i.position
};
};
if (data.option === 'auto') {
const eec = copyFromDataLayer('ecommerce', 1) || {};
if (eec.hasOwnProperty('click') && getType(eec.click.products) === 'array') {
return eec.click.products.map(i => {
const product = mapProductData(i);
product.item_list_name = eec.click.actionField ? eec.click.actionField.list : undefined;
product.index = i.position;
return product;
});
}
if (eec.hasOwnProperty('detail') && getType(eec.detail.products) === 'array') {
return eec.detail.products.map(i => {
const product = mapProductData(i);
product.item_list_name = eec.detail.actionField ? eec.detail.actionField.list : undefined;
return product;
});
}
if (eec.hasOwnProperty('add') && getType(eec.add.products) === 'array') {
return eec.add.products.map(i => {
const product = mapProductData(i);
product.item_list_name = eec.add.actionField ? eec.add.actionField.list : undefined;
return product;
});
}
if (eec.hasOwnProperty('remove') && getType(eec.remove.products) === 'array') {
return eec.remove.products.map(i => {
const product = mapProductData(i);
product.item_list_name = eec.remove.actionField ? eec.remove.actionField.list : undefined;
return product;
});
}
if (eec.hasOwnProperty('checkout') && getType(eec.checkout.products) === 'array') {
return eec.checkout.products.map(i => {
const product = mapProductData(i);
product.item_list_name = eec.checkout.actionField ? eec.checkout.actionField.list : undefined;
return product;
});
}
if (eec.hasOwnProperty('purchase') && getType(eec.purchase.products) === 'array') {
return eec.purchase.products.map(i => {
const product = mapProductData(i);
product.item_list_name = eec.purchase.actionField ? eec.purchase.actionField.list : undefined;
return product;
});
}
if (eec.hasOwnProperty('refund') && getType(eec.refund.products) === 'array') {
return eec.refund.products.map(i => {
const product = mapProductData(i);
product.item_list_name = eec.refund.actionField ? eec.refund.actionField.list : undefined;
return product;
});
}
if (eec.hasOwnProperty('impressions') && getType(eec.impressions) === 'array') {
return eec.impressions.map(mapImpressionData);
}
if (eec.hasOwnProperty('promoView') && getType(eec.promoView.promotions) === 'array') {
return eec.promoView.promotions.map(mapPromotionData);
}
if (eec.hasOwnProperty('promoClick') && getType(eec.promoClick.promotions) === 'array') {
return eec.promoClick.promotions.map(mapPromotionData);
}
return [];
}
if (data.option === 'products' && getType(data.productsVar) === 'array') {
return data.productsVar.map(mapProductData);
}
if (data.option === 'impressions' && getType(data.impressionsVar) === 'array') {
return data.impressionsVar.map(mapImpressionData);
}
if (data.option === 'promotions') {
return data.promotionsVar.map(mapPromotionData);
}
___WEB_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_data_layer",
"versionId": "1"
},
"param": [
{
"key": "keyPatterns",
"value": {
"type": 2,
"listItem": [
{
"type": 1,
"string": "ecommerce"
}
]
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios:
- name: Read custom products variable correctly
code: |-
mockData.option = 'products';
// Call runCode to run the template's code.
let variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo(expected.products);
- name: Read custom impressions variable correctly
code: |-
mockData.option = 'impressions';
// Call runCode to run the template's code.
let variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo(expected.impressions);
- name: Read custom promotions variable correctly
code: |-
mockData.option = 'promotions';
// Call runCode to run the template's code.
let variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo(expected.promotions);
- name: Read ecommerce_impressions dataLayer correctly
code: |-
mock('copyFromDataLayer', (n, v) => {
assertThat(v).isEqualTo(1);
return {impressions: mockData.impressionsVar};
});
// Call runCode to run the template's code.
let variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo(expected.impressions);
- name: Read ecommerce_promoView dataLayer correctly
code: |-
mock('copyFromDataLayer', (n, v) => {
assertThat(v).isEqualTo(1);
return {promoView: {promotions: mockData.promotionsVar}};
});
// Call runCode to run the template's code.
let variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo(expected.promotions);
- name: Read ecommerce_checkout dataLayer correctly
code: |-
mock('copyFromDataLayer', (n, v) => {
assertThat(v).isEqualTo(1);
return {checkout: {products: mockData.productsVar}};
});
expected.products = expected.products.map(p => {
p.item_list_name = undefined;
return p;
});
// Call runCode to run the template's code.
let variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo(expected.products);
setup: |-
const mockData = {
option: 'auto',
productsVar: [{
id: 'p1',
name: 'n1',
price: 10.00,
brand: 'b1',
variant: 'v1',
quantity: 1,
dimension1: 'd1',
dimension17: 'd17',
metric1: '1',
metric17: '17'
},{
id: 'p2',
name: 'n2',
price: 11.00,
brand: 'b2',
variant: 'v2',
quantity: '2.00',
dimension2: 'd2',
metric2: '2',
dimension18: 'd18',
metric18: '18'
}],
impressionsVar: [{
id: 'impi1',
name: 'imn1',
price: 12.00,
brand: 'imb1',
variant: 'imv1',
quantity: 1,
dimension3: 'd3',
metric3: '3',
dimension19: 'd19',
metric19: '19',
testDim: 'testDimValue',
testMet: 'testMetValue',
list: 'il1',
position: 1
},{
id: 'impi2',
name: 'imn2',
price: 13.00,
brand: 'imb2',
variant: 'imv2',
quantity: 2,
dimension4: 'd4',
metric4: '4',
dimension20: 'd20',
metric20: '20',
list: 'il1',
position: 2
}],
promotionsVar: [{
id: 'promo1',
name: 'promon1',
creative: 'promoc1',
position: 'slot1'
},{
id: 'promo2',
name: 'promon2',
creative: 'promoc2',
position: 'slot2'
}],
customDims: [
{
cdindex: 'testDim',
cdparam: 'test_dim'
},{
cdindex: '17',
cdparam: 'dim17'
},{
cdindex: '18',
cdparam: 'dim18'
},{
cdindex: '19',
cdparam: 'dim19'
},{
cdindex: '20',
cdparam: 'dim20'
}],
customMets: [{
cmindex: 'testMet',
cmparam: 'test_met'
},{
cmindex: '17',
cmparam: 'met17'
},{
cmindex: '18',
cmparam: 'met18'
},{
cmindex: '19',
cmparam: 'met19'
},{
cmindex: '20',
cmparam: 'met20'
}]
};
const expected = {
products: [{
item_id: 'p1',
item_name: 'n1',
price: 10.00,
item_brand: 'b1',
item_variant: 'v1',
quantity: 1,
dimension1: 'd1',
metric1: 1,
dim17: 'd17',
met17: 17
},{
item_id: 'p2',
item_name: 'n2',
price: 11.00,
item_brand: 'b2',
item_variant: 'v2',
quantity: 2,
dimension2: 'd2',
metric2: 2,
dim18: 'd18',
met18: 18
}],
impressions: [{
item_id: 'impi1',
item_name: 'imn1',
price: 12.00,
item_brand: 'imb1',
item_variant: 'imv1',
quantity: 1,
dimension3: 'd3',
metric3: 3,
dim19: 'd19',
met19: 19,
test_dim: 'testDimValue',
test_met: 'testMetValue',
item_list_name: 'il1',
index: 1
},{
item_id: 'impi2',
item_name: 'imn2',
price: 13.00,
item_brand: 'imb2',
item_variant: 'imv2',
quantity: 2,
dimension4: 'd4',
metric4: 4,
dim20: 'd20',
met20: 20,
item_list_name: 'il1',
index: 2
}],
promotions: [{
promotion_id: 'promo1',
promotion_name: 'promon1',
creative_name: 'promoc1',
creative_slot: 'slot1'
},{
promotion_id: 'promo2',
promotion_name: 'promon2',
creative_name: 'promoc2',
creative_slot: 'slot2'
}]
};
___NOTES___
Created on 17/10/2020, 22:54:27