-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.tpl
629 lines (603 loc) · 18.7 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
___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": "Ecom Items to String or Sum Values",
"description": "This Variable turns the Ecommerce Item array (e.g. [{item_id: \u002712\u0027, quantity: 1},{item_id: \u002734\u0027, quantity: 2}]) into a string (e.g. \u002712|34\u0027), or sums Item Values (e.g 3).",
"categories": [
"TAG_MANAGEMENT",
"UTILITY",
"ANALYTICS"
],
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "LABEL",
"name": "templateDescription",
"displayName": "Turn the Item array (e.g. \u003cstrong\u003e[{item_id: \u002712\u0027, quantity: 1},{item_id: \u002734\u0027, quantity: 2}]\u003c/strong\u003e) into a string (e.g. \u003cstrong\u003e\u002712|34\u0027\u003c/strong\u003e), or sum Item Values (e.g \u003cstrong\u003e3\u003c/strong\u003e)."
},
{
"type": "GROUP",
"name": "mappingSelectionGroup",
"displayName": "Select Mapping Method",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "selectionType",
"displayName": "",
"macrosInSelect": false,
"selectItems": [
{
"value": "standardMapping",
"displayValue": "GA4 Standard Ecommerce Events"
},
{
"value": "customMapping",
"displayValue": "Custom Mapping"
}
],
"simpleValueType": true,
"alwaysInSummary": false
},
{
"type": "GROUP",
"name": "selectionStandard",
"displayName": "Select Ecommerce Events",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "CHECKBOX",
"name": "add_payment_info",
"checkboxText": "add_payment_info",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "add_shipping_info",
"checkboxText": "add_shipping_info",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "add_to_cart",
"checkboxText": "add_to_cart",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "add_to_wishlist",
"checkboxText": "add_to_wishlist",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "begin_checkout",
"checkboxText": "begin_checkout",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "purchase",
"checkboxText": "purchase",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "refund",
"checkboxText": "refund",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "remove_from_cart",
"checkboxText": "remove_from_cart",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "select_item",
"checkboxText": "select_item",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "select_promotion",
"checkboxText": "select_promotion",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "view_cart",
"checkboxText": "view_cart",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "view_item",
"checkboxText": "view_item",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "view_item_list",
"checkboxText": "view_item_list",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "view_promotion",
"checkboxText": "view_promotion",
"simpleValueType": true
}
],
"help": "Select \u003cstrong\u003eGA4 Standard Ecommerce Events\u003c/strong\u003e that should generate the \u003cstrong\u003estring\u003c/strong\u003e or \u003cstrong\u003esum Item Values\u003c/strong\u003e.",
"enablingConditions": [
{
"paramName": "selectionType",
"paramValue": "standardMapping",
"type": "EQUALS"
}
]
},
{
"type": "SIMPLE_TABLE",
"name": "selectionCustom",
"displayName": "Custom Mapping",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Custom Mapping Parameter",
"name": "customParameter",
"type": "TEXT",
"valueHint": "addToCart",
"selectItems": [],
"isUnique": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"macrosInSelect": false
},
{
"defaultValue": "",
"displayName": "Select Variable for Mapping",
"name": "customParameterVariable",
"type": "SELECT",
"selectItems": [],
"isUnique": false,
"valueHint": "addToCart",
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"macrosInSelect": true
}
],
"newRowButtonText": "Add Custom Mapping Parameter",
"enablingConditions": [
{
"paramName": "selectionType",
"paramValue": "customMapping",
"type": "EQUALS"
}
],
"help": "Type in \u003cstrong\u003eCustom Mapping Parameter\u003c/strong\u003e (e.g. \u003cstrong\u003eaddToCart\u003c/strong\u003e) and \u003cstrong\u003eSelect Variable for Mapping\u003c/strong\u003e against this parameter (e.g. \u003cstrong\u003e{{Event Name}}\u003c/strong\u003e).",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
}
],
"help": "You can either map against \u003cstrong\u003eGA4 Standard Ecommerce Events\u003c/strong\u003e, or choose \u003cstrong\u003eCustom Mapping\u003c/strong\u003e to map against \u003cstrong\u003eVariables\u003c/strong\u003e."
},
{
"type": "RADIO",
"name": "itemTypeSelection",
"displayName": "Select Item Parameter Type \u0026 Action",
"radioItems": [
{
"value": "string",
"displayValue": "Create String from Item Parameter",
"subParams": []
},
{
"value": "metric",
"displayValue": "Sum Values from Item Parameter",
"subParams": []
}
],
"simpleValueType": true,
"help": "Select if you are going to \u003cstrong\u003eCreate a String\u003c/strong\u003e or \u003cstrong\u003eSum Values\u003c/strong\u003e from the \u003cstrong\u003eItem Parameter\u003c/strong\u003e."
},
{
"type": "SELECT",
"name": "itemType",
"displayName": "Select Item Type",
"macrosInSelect": false,
"selectItems": [
{
"value": "itemStandard",
"displayValue": "Standard Item Parameter"
},
{
"value": "itemCustom",
"displayValue": "Custom Item Parameter"
}
],
"simpleValueType": true,
"defaultValue": "itemStandard",
"alwaysInSummary": true,
"help": "Select a \u003cstrong\u003eStandard Item Parameter\u003c/strong\u003e, or choose \u003cstrong\u003eCustom Item Parameter\u003c/strong\u003e."
},
{
"type": "GROUP",
"name": "itemStringGroup",
"displayName": "Select Item Parameter Settings",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "itemStandardString",
"displayName": "Select Standard Item Parameter",
"macrosInSelect": false,
"selectItems": [
{
"value": "item_id",
"displayValue": "item_id"
},
{
"value": "item_name",
"displayValue": "item_name"
},
{
"value": "item_variant",
"displayValue": "item_variant"
},
{
"value": "item_brand",
"displayValue": "item_brand"
},
{
"value": "item_category",
"displayValue": "item_category"
},
{
"value": "item_category2",
"displayValue": "item_category2"
},
{
"value": "item_category3",
"displayValue": "item_category3"
},
{
"value": "item_category4",
"displayValue": "item_category4"
},
{
"value": "item_category5",
"displayValue": "item_category5"
},
{
"value": "affiliation",
"displayValue": "affiliation"
},
{
"value": "coupon",
"displayValue": "coupon"
},
{
"value": "currency",
"displayValue": "currency"
},
{
"value": "index",
"displayValue": "index"
},
{
"value": "item_list_id",
"displayValue": "item_list_id"
},
{
"value": "item_list_name",
"displayValue": "item_list_name"
},
{
"value": "location_id",
"displayValue": "location_id"
},
{
"value": "creative_name",
"displayValue": "creative_name"
},
{
"value": "creative_slot",
"displayValue": "creative_slot"
},
{
"value": "promotion_id",
"displayValue": "promotion_id"
},
{
"value": "promotion_name",
"displayValue": "promotion_name"
}
],
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "itemType",
"paramValue": "itemStandard",
"type": "EQUALS"
}
],
"help": "Select a \u003cstrong\u003eStandard Item Parameter\u003c/strong\u003e from the dropdown to \u003cstrong\u003ecreate a string\u003c/strong\u003e."
},
{
"type": "TEXT",
"name": "itemCustomString",
"displayName": "Custom Item String Parameter",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "itemType",
"paramValue": "itemCustom",
"type": "EQUALS"
}
],
"help": "Type in your \u003cstrong\u003eCustom Item String\u003c/strong\u003e Parameter, e.g. \u003cstrong\u003eitem_color\u003c/strong\u003e.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "paramDelimiter",
"displayName": "Delimiter",
"simpleValueType": true,
"defaultValue": "|",
"help": "Choose \u003cstrong\u003edelimiter\u003c/strong\u003e for separating your values in the string.",
"valueHint": "|",
"enablingConditions": [
{
"paramName": "itemTypeSelection",
"paramValue": "string",
"type": "EQUALS"
}
],
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "CHECKBOX",
"name": "itemUniqueString",
"checkboxText": "Return Unique Items",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "itemTypeSelection",
"paramValue": "string",
"type": "EQUALS"
}
],
"help": "Choose if only \u003cstrong\u003eUnique Item Parameter Values\u003c/strong\u003e should be returned. E.g. if the Item array contains \u003cstrong\u003e[{item_category: \u0027A\u0027},{item_category: \u0027B\u0027 },(item_category: \u0027A\u0027}]\u003c/strong\u003e, the string returned will be \u003cstrong\u003e\u0027A|B\u0027\u003c/strong\u003e."
}
],
"enablingConditions": [
{
"paramName": "itemTypeSelection",
"paramValue": "string",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "itemMetricGroup",
"displayName": "Select Item Value Settings",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "itemStandardMetric",
"displayName": "Select Item Parameter",
"macrosInSelect": false,
"selectItems": [
{
"value": "discount",
"displayValue": "discount"
},
{
"value": "price",
"displayValue": "price"
},
{
"value": "quantity",
"displayValue": "quantity"
}
],
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "itemType",
"paramValue": "itemStandard",
"type": "EQUALS"
}
],
"help": "Select a \u003cstrong\u003eStandard Item Parameter\u003c/strong\u003e from the dropdown to \u003cstrong\u003ecreate values\u003c/strong\u003e from."
},
{
"type": "TEXT",
"name": "itemCustomMetric",
"displayName": "Custom Item Metric Parameter",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "itemType",
"paramValue": "itemCustom",
"type": "EQUALS"
}
],
"help": "Type in a \u003cstrong\u003eCustom Item Parameter\u003c/strong\u003e to \u003cstrong\u003ecreate values\u003c/strong\u003e from.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "CHECKBOX",
"name": "multiplyQuantity",
"checkboxText": "Multiply Value with Quantity",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "itemStandardMetric",
"paramValue": "quantity",
"type": "NOT_EQUALS"
}
],
"help": "Choose if the \u003cstrong\u003eValue (Metric)\u003c/strong\u003e should be \u003cstrong\u003eMultiplied\u003c/strong\u003e with \u003cstrong\u003eQuantity\u003c/strong\u003e."
}
],
"enablingConditions": [
{
"paramName": "itemTypeSelection",
"paramValue": "metric",
"type": "EQUALS"
}
]
}
]
___SANDBOXED_JS_FOR_SERVER___
const getType = require('getType');
const getEventData = require('getEventData');
const queryPermission = require('queryPermission');
const makeNumber = require('makeNumber');
const keyPath = 'items';
if (queryPermission('read_event_data', keyPath)) {
const itemsArray = getEventData(keyPath);
if (getType(itemsArray) !== 'array') {
return;
}
const event = getEventData('event_name');
let standardMapping = {};
if (data.selectionType == 'standardMapping') {
standardMapping = {
'add_payment_info': data.add_payment_info,
'add_shipping_info': data.add_shipping_info,
'add_to_cart': data.add_to_cart,
'add_to_wishlist': data.add_to_wishlist,
'begin_checkout': data.begin_checkout,
'purchase': data.purchase,
'refund': data.refund,
'remove_from_cart': data.remove_from_cart,
'select_item': data.select_item,
'select_promotion': data.select_promotion,
'view_cart': data.view_cart,
'view_item': data.view_item,
'view_item_list': data.view_item_list,
'view_promotion': data.view_promotion
};
}
let customMapping;
if (data.selectionCustom && data.selectionType == 'customMapping') {
data.selectionCustom.forEach((customArray) => {
if (customArray.customParameter === customArray.customParameterVariable) {
customMapping = customArray.customParameter;
}
});
}
const mappingResult = standardMapping[event] ? standardMapping[event] : customMapping;
if (mappingResult) {
if (data.itemTypeSelection === 'string') {
const paramDelimiter = data.paramDelimiter;
const itemString = data.itemStandardString ? data.itemStandardString : data.itemCustomString;
if (data.itemUniqueString) {
return itemsArray
.map(obj => obj[itemString])
.filter((value, index, self) => self.indexOf(value) === index)
.join(paramDelimiter);
} else {
return itemsArray
.map(obj => obj[itemString])
.filter(obj => obj)
.join(paramDelimiter);
}
} else {
const itemMetric = data.itemStandardMetric ? data.itemStandardMetric : data.itemCustomMetric;
if (data.multiplyQuantity) {
return itemsArray
.reduce(function(a, b){return a + makeNumber(b[itemMetric]*(b.quantity||1));}, 0);
} else {
return itemsArray
.map(obj => obj[itemMetric])
.filter(obj => obj)
.reduce((a, b) => a + makeNumber(b), 0);
}
}
}
}
___SERVER_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_event_data",
"versionId": "1"
},
"param": [
{
"key": "keyPatterns",
"value": {
"type": 2,
"listItem": [
{
"type": 1,
"string": "event_name"
},
{
"type": 1,
"string": "items"
}
]
}
},
{
"key": "eventDataAccess",
"value": {
"type": 1,
"string": "specific"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios: []
___NOTES___
Created on 8/6/2021, 3:40:23 PM