forked from gtm-templates-simo-ahava/data-layer-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.tpl
718 lines (631 loc) · 20.2 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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
___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,
"categories": [
"UTILITY"
],
"__wm": "VGVtcGxhdGUtQXV0aG9yX0RhdGFMYXllclBpY2tlci1TaW1vLUFoYXZh",
"securityGroups": [],
"displayName": "Data Layer Picker",
"description": "Pick keys and values from the dataLayer push (and that push alone) that caused this tag to be evaluated.",
"containerContexts": [
"WEB"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "RADIO",
"name": "option",
"displayName": "Value to return",
"radioItems": [
{
"value": "object",
"displayValue": "Entire object"
},
{
"value": "key",
"displayValue": "Specific property",
"subParams": [
{
"type": "TEXT",
"name": "keyName",
"displayName": "Property name",
"simpleValueType": true
}
],
"help": ""
}
],
"simpleValueType": true,
"help": "If you choose \"Entire object\", the whole object that was pushed into dataLayer will be returned. If you choose \"Specific property\", you can then provide the key (using dot notation if necessary) whose value you want to pull from the pushed dataLayer object."
},
{
"type": "SELECT",
"name": "mapEvent",
"displayName": "From Specific Event?",
"macrosInSelect": false,
"selectItems": [
{
"value": "no",
"displayValue": "no"
},
{
"value": 1,
"displayValue": "Event Occurrence Number (For all events)"
},
{
"value": 2,
"displayValue": "By Especific Event and Occurrence Number"
},
{
"value": 3,
"displayValue": "By Especific Event - First Occurrence"
},
{
"value": 4,
"displayValue": "By Especific Event - Penult Occurrence"
},
{
"value": 5,
"displayValue": "By Especific Event - Last Occurrence"
}
],
"simpleValueType": true,
"help": "No - Will get the variable value at the at the current event\u003cbr\u003e\u003cbr\u003e\n\nEvent Occurrence Number - It will take the value of the variable from the specified number of the event according to the firing order of all events\u003cbr\u003e\u003cbr\u003e\n\nBy Especific Event and Occurrence Number - It will take the variable value of the specified ccurrence number of an event specified by the name\u003cbr\u003e\u003cbr\u003e\n\nBy Especific Event - Penult Occurrence - It will take the value of the variable from the penultimate occurrence of an event specified by the name\nBy Especific Event - First Occurrence - It will take the value of the variable from the first occurrence of an event specified by the name",
"defaultValue": "no"
},
{
"type": "TEXT",
"name": "eventName",
"displayName": "Event Name",
"simpleValueType": true,
"help": "Especify the name of the event. Must be the identification of the event, not the user friendly name.\n\nE.g.: If you want to especify the click event, you must input \"gtm.click\", not \"Click\"",
"enablingConditions": [
{
"paramName": "mapEvent",
"paramValue": 2,
"type": "EQUALS"
},
{
"paramName": "mapEvent",
"paramValue": 3,
"type": "EQUALS"
},
{
"paramName": "mapEvent",
"paramValue": 4,
"type": "EQUALS"
},
{
"paramName": "mapEvent",
"paramValue": 5,
"type": "EQUALS"
}
]
},
{
"type": "TEXT",
"name": "eventIdx",
"displayName": "Event Especific Ocurrency Number",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "mapEvent",
"paramValue": 1,
"type": "EQUALS"
},
{
"paramName": "mapEvent",
"paramValue": 2,
"type": "EQUALS"
}
],
"help": "Especify the occurence number of the event. If the event \"gtm.click\" occured 3 times and you want to get the value of an variable whem the 2nd ocurrency, input 2 as the value of this filed.",
"valueValidators": [
{
"type": "NON_EMPTY"
},
{
"type": "POSITIVE_NUMBER"
}
]
}
]
___SANDBOXED_JS_FOR_WEB_TEMPLATE___
const copyFromDataLayer = require('copyFromDataLayer');
const copyFromWindow = require('copyFromWindow');
const JSON = require('JSON');
const log = require('logToConsole');
const gtmId = copyFromDataLayer('gtm.uniqueEventId');
const dataLayer = copyFromWindow('dataLayer');
const makeNumber = require('makeNumber');
let obj = [];
// Helper inspired by https://bit.ly/3bAAz32
// Modified by Rodrigo Passos Gregorio to ensure that props with "gtm." doens't cause in bugs
const get = (obj, path, def) => {
path = path.split('.');
// If first element of path equals "gtm" it means the searched key have the "." character when declared
// Normally these are default GTM keys in dataLayer
if(path.length >= 2 && path[0] == 'gtm'){
path.shift();
path[0] = 'gtm.' + path[0];
}
let current = obj;
for (let i = 0; i < path.length; i++) {
if (!current[path[i]]) return def;
current = current[path[i]];
}
return current;
};
if (dataLayer && gtmId) {
// Get object from dataLayer that matches the gtm.uniqueEventId
obj = dataLayer.map(o => {
// If a regular dataLayer object, return it
if (o['gtm.uniqueEventId']){
return o;
}
// Other wise assume it's a template constructor-based object
// Clone the object to remove constructor, then return first
// property in the object (the wrapper).
o = JSON.parse(JSON.stringify(o));
for (let prop in o) {
return o[prop];
}
}).filter(o => {
// Filter to only include the item(s) where the event ID matches - No specific ocurrency or event name
if(data.mapEvent == "no" && o['gtm.uniqueEventId'] === gtmId) return true;
// If search must be at all dataLayer events, return all ocurrencies - Specified ocurrency only
else if(o && o['gtm.uniqueEventId'] && data && data.mapEvent == '1') return true;
// If search must be at specific dataLayer event, return all specific dataLayer events - Specified ocurrency and event name
else if(o && o['gtm.uniqueEventId'] && data && data.mapEvent && data.mapEvent != '1' && o.event == data.eventName) return true;
});
var dme = data.mapEvent;
data.eventIdx = makeNumber(data.eventIdx);
let idx = 0;
if(!!((dme && obj.length) && ((dme == "no") || (dme == '1') || (dme == '2')) && (obj.length >= data.eventIdx))){
idx = (data.eventIdx-1);
} else if(dme && dme == '3' && obj.length){
idx = 0;
} else if(dme && dme == '4' && obj.length >=2){
idx = obj.length-2;
} else if(dme && dme == '5' && obj.length){
idx = obj.length-1;
} else if(obj.length){
idx = obj.length - 1;
} else {
idx = 0;
}
obj = obj.length ? obj[idx] : {};
switch (data.option) {
case 'object':
return obj;
case 'key':
return get(obj, data.keyName, obj[data.keyName]);
}
}
___WEB_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "access_globals",
"versionId": "1"
},
"param": [
{
"key": "keys",
"value": {
"type": 2,
"listItem": [
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "key"
},
{
"type": 1,
"string": "read"
},
{
"type": 1,
"string": "write"
},
{
"type": 1,
"string": "execute"
}
],
"mapValue": [
{
"type": 1,
"string": "dataLayer"
},
{
"type": 8,
"boolean": true
},
{
"type": 8,
"boolean": false
},
{
"type": 8,
"boolean": false
}
]
}
]
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "read_data_layer",
"versionId": "1"
},
"param": [
{
"key": "keyPatterns",
"value": {
"type": 2,
"listItem": [
{
"type": 1,
"string": "gtm"
},
{
"type": 1,
"string": "gtm.*"
},
{
"type": 1,
"string": "gtm.uniqueEventId"
},
{
"type": 1,
"string": "ecommerce"
},
{
"type": 1,
"string": "ecommerce.*"
}
]
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "logging",
"versionId": "1"
},
"param": [
{
"key": "environments",
"value": {
"type": 1,
"string": "debug"
}
}
]
},
"isRequired": true
}
]
___TESTS___
scenarios:
- name: Fetches OBJECT from regular dL array
code: |-
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 1, event: 'Event1', firstItem: {test: 'yes'}, ecommerce: {impression:[{name: 'product_name1'}]}});
- name: Fetches KEY from regular dL array
code: |-
mockData.option = 'key';
mockData.keyName = 'ecommerce.impression.0.name';
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo('product_name1');
- name: Fetches OBJECT from template dL array
code: |-
mock('copyFromDataLayer', key => {
return 2;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 2, event: 'Event1', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name2'}]}});
- name: Fetches KEY from template dL array
code: |-
mockData.option = 'key';
mockData.keyName = 'ecommerce.impression.0.name';
mock('copyFromDataLayer', key => {
return 2;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo('product_name2');
- name: Fetches OBJECT from specif occurency in regular dL array
code: |-
mockData.mapEvent= '1';
mockData.eventIdx = 2;
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 2, event: 'Event1', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name2'}]}});
- name: Fetches KEY from specif occurency in regular dL array
code: |-
mockData.option = 'key';
mockData.keyName = 'ecommerce.impression.0.name';
mockData.mapEvent= '1';
mockData.eventIdx = 2;
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
/*assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 2, event: 'Event1', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name2'}]}});*/
assertThat(variableResult).isEqualTo('product_name2');
- name: Fetches OBJECT from specif event and specif occurency in regular dL array
code: |-
mockData.mapEvent= '2';
mockData.eventIdx = 2;
mockData.eventName = "Event2";
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 6, event: 'Event2', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name6'}]}});
- name: Fetches KEY from specif event and specif occurency in regular dL array
code: |-
mockData.option = 'key';
mockData.keyName = 'ecommerce.impression.0.name';
mockData.mapEvent= '2';
mockData.eventIdx = 2;
mockData.eventName = "Event2";
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
/*assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 2, event: 'Event1', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name2'}]}});*/
assertThat(variableResult).isEqualTo('product_name6');
- name: Fetches OBJECT from first occurency of specif Event in regular dL array
code: |-
mockData.mapEvent= '3';
mockData.eventName = "Event2";
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 5, event: 'Event2', firstItem: {test: 'yes'}, ecommerce: {impression:[{name: 'product_name5'}]}});
- name: Fetches KEY from first occurency of specif Event in regular dL array
code: |-
mockData.option = 'key';
mockData.keyName = 'ecommerce.impression.0.name';
mockData.mapEvent= '3';
mockData.eventName = "Event2";
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
/*assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 2, event: 'Event1', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name2'}]}});*/
assertThat(variableResult).isEqualTo('product_name5');
- name: Fetches OBJECT from penult occurency of specif Event in regular dL array
code: |-
mockData.mapEvent= '4';
mockData.eventName = "Event1";
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo({'gtm.uniqueEventId':3, event: 'Event1', firstItem: {test: 'yes'}, ecommerce: {impression:[{name: 'product_name3'}]}});
- name: Fetches KEY from penult occurency of specif Event in regular dL array
code: |-
mockData.option = 'key';
mockData.keyName = 'ecommerce.impression.0.name';
mockData.mapEvent= '4';
mockData.eventName = "Event1";
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
/*assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 2, event: 'Event1', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name2'}]}});*/
assertThat(variableResult).isEqualTo('product_name3');
- name: Fetches KEY from last occurency of specif Event in regular dL array
code: |-
mockData.option = 'key';
mockData.keyName = 'ecommerce.impression.0.name';
mockData.mapEvent= '5';
mockData.eventName = "Event2";
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
/*assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 2, event: 'Event1', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name2'}]}});*/
assertThat(variableResult).isEqualTo('product_name8');
- name: Fetches OBJECT from last occurency of specif Event in regular dL array
code: |-
mockData.mapEvent= '5';
mockData.eventName = "Event2";
mock('copyFromDataLayer', key => {
return 1;
});
mock('copyFromWindow', key => {
return dataLayer;
});
// Call runCode to run the template's code.
const variableResult = runCode(mockData);
// Verify that the variable returns a result.
// Verify that the variable returns a result.
assertThat(variableResult).isEqualTo({'gtm.uniqueEventId': 8, event: 'Event2', secondItem: {test: 'no'}, ecommerce: {impression:[{name: 'product_name8'}]}});
setup: |-
const mockData = {
option: 'object',
keyName: 'ecommerce.impression.0.name',
mapEvent: 'no'
};
const dataLayer = [{
'gtm.uniqueEventId': 1,
event: 'Event1',
firstItem: {
test: 'yes'
},
ecommerce: {impression:[{name: 'product_name1'}]}
},{
a: {
'gtm.uniqueEventId': 2,
event: 'Event1',
secondItem: {
test: 'no'
},
ecommerce: {impression:[{name: 'product_name2'}]}
}
},{
'gtm.uniqueEventId': 3,
event: 'Event1',
firstItem: {
test: 'yes'
},
ecommerce: {impression:[{name: 'product_name3'}]}
},{
a: {
'gtm.uniqueEventId': 4,
event: 'Event1',
secondItem: {
test: 'no'
},
ecommerce: {impression:[{name: 'product_name4'}]}
}
},{
'gtm.uniqueEventId': 5,
event: 'Event2',
firstItem: {
test: 'yes'
},
ecommerce: {impression:[{name: 'product_name5'}]}
},{
a: {
'gtm.uniqueEventId': 6,
event: 'Event2',
secondItem: {
test: 'no'
},
ecommerce: {impression:[{name: 'product_name6'}]}
}
},{
'gtm.uniqueEventId': 7,
event: 'Event2',
firstItem: {
test: 'yes'
},
ecommerce: {impression:[{name: 'product_name7'}]}
},{
a: {
'gtm.uniqueEventId': 8,
event: 'Event2',
secondItem: {
test: 'no'
},
ecommerce: {impression:[{name: 'product_name8'}]}
}
}];
___NOTES___
Created on 25/02/2021, 19:33:41
Updated on 26/02/2021, 20:00:00