-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.tpl
More file actions
740 lines (681 loc) · 26.7 KB
/
Copy pathtemplate.tpl
File metadata and controls
740 lines (681 loc) · 26.7 KB
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
___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,
"displayName": "GA4 - Item List \u0026 Promotion Attribution",
"description": "Attribute GA4 Item List, Promotion or Search Term to revenue \u0026 ecommerce Events. This Template makes this possible by using ex. Firestore as a \"helper\". Last \u0026 First Click Attribution supported.",
"categories": [
"ANALYTICS",
"UTILITY",
"TAG_MANAGEMENT"
],
"containerContexts": [
"SERVER"
],
"securityGroups": []
}
___TEMPLATE_PARAMETERS___
[
{
"type": "LABEL",
"name": "introLabel",
"displayName": "Extract \u0026 Attribute GA4 Item List \u0026 Promotion data, or merge Item List \u0026 Promotion Data from Second Data Source."
},
{
"type": "GROUP",
"name": "variableTypeGroup",
"displayName": "Variable Type",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "RADIO",
"name": "variableType",
"displayName": "Create Attribution or Return Attributed Output",
"radioItems": [
{
"value": "attribution",
"displayValue": "Extract Item List \u0026 Promotion for Attribution",
"help": "Choose this setting for extracting Item List \u0026 Promotion data, create the attribution, and for storing this data in a Second Data Source (ex. Firestore)."
},
{
"value": "output",
"displayValue": "Return Attributed Output",
"help": "Choose this setting for merging ecommerce data with attributed data from Second Data Source (ex.Firestore)."
}
],
"simpleValueType": true
},
{
"type": "GROUP",
"name": "outputGroup",
"displayName": "Output",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "outputDropDown",
"displayName": "Parameter Output",
"selectItems": [
{
"value": "items",
"displayValue": "Items"
},
{
"value": "promotion_name",
"displayValue": "Promotion Name"
},
{
"value": "promotion_id",
"displayValue": "Promotion ID"
},
{
"value": "creative_name",
"displayValue": "Creative Name"
},
{
"value": "creative_slot",
"displayValue": "Creative Slot"
},
{
"value": "location_id",
"displayValue": "Location ID"
},
{
"value": "search_term",
"displayValue": "Search Term"
},
{
"value": "custom_event",
"displayValue": "Custom Event Parameter"
}
],
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"enablingConditions": [
{
"paramName": "variableType",
"paramValue": "output",
"type": "EQUALS"
}
],
"alwaysInSummary": true
},
{
"type": "TEXT",
"name": "customEventOutputKey",
"displayName": "Custom Parameter Name",
"simpleValueType": true,
"valueHint": "E.g., search_category",
"enablingConditions": [
{
"paramName": "outputDropDown",
"paramValue": "custom_event",
"type": "EQUALS"
}
],
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "CHECKBOX",
"name": "itemSearchTerm",
"checkboxText": "Add Search Term To Items",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "outputDropDown",
"paramValue": "items",
"type": "EQUALS"
}
],
"help": "If you tick this checkbox, \u003cstrong\u003esearch_term\u003c/strong\u003e will be added to \u003cstrong\u003eitems\u003c/strong\u003e. This makes it easier to report search_term related to items purchased.\n\u003cbr /\u003e\u003cbr /\u003e\n\u003cstrong\u003esearch_term\u003c/strong\u003e should be added in GA4 as an \u003cstrong\u003eitem scoped dimension\u003c/strong\u003e."
},
{
"type": "CHECKBOX",
"name": "itemCustomEvents",
"checkboxText": "Add Custom Event Parameters to Items",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "outputDropDown",
"paramValue": "items",
"type": "EQUALS"
}
],
"help": "If you tick this checkbox, \u003cstrong\u003eCustom Event Parameters\u003c/strong\u003e will be added to \u003cstrong\u003eitems\u003c/strong\u003e. \u003cbr /\u003e\u003cbr /\u003e \u003cstrong\u003eCustom Event Parameters\u003c/strong\u003e should be added in GA4 as an \u003cstrong\u003eitem scoped dimension\u003c/strong\u003e."
}
],
"enablingConditions": [
{
"paramName": "variableType",
"paramValue": "output",
"type": "EQUALS"
}
]
}
]
},
{
"type": "GROUP",
"name": "secondDataSourceGroup",
"displayName": "Second Data Source",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "TEXT",
"name": "secondDataSource",
"displayName": "Second Data Source",
"simpleValueType": true,
"alwaysInSummary": true,
"help": "Insert variable with Second Data Source (ex. Firestore) in this field",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
}
]
},
{
"type": "LABEL",
"name": "attributionLabel"
},
{
"type": "GROUP",
"name": "attributionGroup",
"displayName": "Attribution",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "CHECKBOX",
"name": "deleteAttribution",
"checkboxText": "Delete Attribution Data after Purchase",
"simpleValueType": true,
"alwaysInSummary": true,
"help": "Tick this box if you want attribution data to be deleted/reset after \u003cstrong\u003epurchase\u003c/strong\u003e.",
"enablingConditions": [
{
"paramName": "variableType",
"paramValue": "attribution",
"type": "EQUALS"
}
]
},
{
"type": "CHECKBOX",
"name": "customAttributionTime",
"checkboxText": "Custom Attribution Time",
"simpleValueType": true,
"help": "As standard, attribution time is the same as a \u003cstrong\u003e\u003ca href\u003d\"https://support.google.com/analytics/answer/9191807\" target\u003d\"_blank\"\u003eGA4 Session\u003c/a\u003e\u003c/strong\u003e, but you can choose a \u003cstrong\u003ecustom attribution time\u003c/strong\u003e if that better fits your users behaviour.",
"alwaysInSummary": true
},
{
"type": "TEXT",
"name": "attributionTime",
"displayName": "Attribution Time in Minutes",
"simpleValueType": true,
"valueUnit": "minutes",
"help": "How many minutes should \u003cstrong\u003eItem Lists\u003c/strong\u003e or \u003cstrong\u003ePromotion\u003c/strong\u003e being credited to a conversion?\n\u003cbr /\u003e\u003cbr /\u003e\nNote that each \u003cstrong\u003eselect_item\u003c/strong\u003e, \u003cstrong\u003eselect_promotion\u003c/strong\u003e or \u003cstrong\u003eadd_to_cart\u003c/strong\u003e Event will renew the attribution time (if these Events contains Item List or Promotion data).",
"defaultValue": 30,
"alwaysInSummary": true,
"valueValidators": [
{
"type": "NON_EMPTY"
},
{
"type": "POSITIVE_NUMBER"
}
],
"valueHint": "30",
"enablingConditions": [
{
"paramName": "customAttributionTime",
"paramValue": true,
"type": "EQUALS"
}
]
},
{
"type": "RADIO",
"name": "attributionType",
"displayName": "Attribution Type",
"radioItems": [
{
"value": "lastClickAttribution",
"displayValue": "Last Click Attribution"
},
{
"value": "firstClickAttribution",
"displayValue": "First Click Attribution"
}
],
"simpleValueType": true,
"help": "\u003cstrong\u003eLast Click Attribution\u003c/strong\u003e \u003cbr /\u003e\nWith Last Click Attribution, the Last Click on an Item List or a Promotion will be attributed.\n\u003cbr /\u003e\nItem List and Promotion attribution are independent of each other.\n\u003cbr /\u003e\u003cbr /\u003e\nSee \u003ca href\u003d\"https://github.com/gtm-templates-knowit-experience/sgtm-ga4-item-list-promo-attribution\" target\u003d\"_blank\"\u003e\u003cstrong\u003ethe documentation\u003c/strong\u003e\u003c/a\u003e for detailed explanation of attribution.\n\u003cbr /\u003e\u003cbr /\u003e\n\u003cstrong\u003eFirst Click Attribution\u003c/strong\u003e \u003cbr /\u003e\nWith First Click Attribution, the First Click on an Item List or a Promotion will be attributed.\n\u003cbr /\u003e\nItem List and Promotion attribution are independent of each other.",
"enablingConditions": [
{
"paramName": "variableType",
"paramValue": "attribution",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "siteSearchGroup",
"displayName": "Site Search",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "CHECKBOX",
"name": "siteSearchChecbox",
"checkboxText": "Attribute Site Search",
"simpleValueType": true,
"help": "Attribute the \u003cstrong\u003esearch_term\u003c/strong\u003e parameter.",
"alwaysInSummary": true
}
],
"enablingConditions": [
{
"paramName": "variableType",
"paramValue": "attribution",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "otherAttributionGroup",
"displayName": "Custom Item \u0026 Event Parameter Attribution",
"groupStyle": "ZIPPY_OPEN_ON_PARAM",
"subParams": [
{
"type": "SIMPLE_TABLE",
"name": "customItemAttributeParameters",
"displayName": "Custom Item Parameters",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Parameter Name",
"name": "parameterName",
"type": "TEXT"
}
]
},
{
"type": "SIMPLE_TABLE",
"name": "customEventAttributeParameters",
"displayName": "Custom Event Parameters",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Parameter Name",
"name": "parameterName",
"type": "TEXT"
}
]
}
],
"enablingConditions": [
{
"paramName": "variableType",
"paramValue": "attribution",
"type": "EQUALS"
}
]
}
]
},
{
"type": "GROUP",
"name": "otherSettingsGroup",
"displayName": "Other Settings",
"groupStyle": "ZIPPY_OPEN_ON_PARAM",
"subParams": [
{
"type": "CHECKBOX",
"name": "jsonData",
"checkboxText": "Handle data as string",
"simpleValueType": true,
"help": "Tick this box, and data will be saved as a string using \u003cstrong\u003eJSON.stringify\u003c/strong\u003e, and read will be done using \u003cstrong\u003eJSON.parse\u003c/strong\u003e. \n\u003cbr /\u003e\u003cbr /\u003e\nChoose this setting if you ex. are storing the data in a cookie."
},
{
"type": "GROUP",
"name": "limitItemsGroup",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "CHECKBOX",
"name": "limitItems",
"checkboxText": "Limit Items",
"simpleValueType": true,
"help": "Some storages can be limited in size. If you choose to store data in ex. a cookie, you should limit number of items stored."
},
{
"type": "TEXT",
"name": "limitItemsNumber",
"displayName": "Number of Items",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
},
{
"type": "POSITIVE_NUMBER"
}
],
"enablingConditions": [
{
"paramName": "limitItems",
"paramValue": true,
"type": "EQUALS"
}
],
"valueHint": "2",
"valueUnit": "item(s)"
}
]
}
],
"enablingConditions": [
{
"paramName": "variableType",
"paramValue": "attribution",
"type": "EQUALS"
}
]
}
]
___SANDBOXED_JS_FOR_SERVER___
const getEventData = require('getEventData');
const getTimestampMillis = require('getTimestampMillis');
const JSON = require('JSON');
const makeInteger = require('makeInteger');
const Object = require('Object');
const jsonData = data.jsonData;
const secondDataSource = data.secondDataSource && typeof data.secondDataSource === 'string' ? JSON.parse(data.secondDataSource) : data.secondDataSource || undefined;
const event_name = getEventData('event_name');
const items = getEventData('items');
let items2 = secondDataSource ? secondDataSource.items : [{item_id:"helper_id"}];
let promo2 = secondDataSource ? secondDataSource.promotion : undefined;
let searchTerm2 = secondDataSource ? secondDataSource.search_term : undefined;
let customEvents2 = secondDataSource && secondDataSource.custom_events ? secondDataSource.custom_events : {};
const timestamp = data.attributionTime ? getTimestampMillis() : makeInteger(getEventData('ga_session_id'));
const timestamp2 = secondDataSource ? secondDataSource.timestamp : timestamp;
const timestampDiff = secondDataSource && data.attributionTime ? timestamp-secondDataSource.timestamp : timestamp;
const attributionTime = data.attributionTime ? makeInteger(data.attributionTime)*60000 : timestamp2;
const attributionType = data.attributionType;
const limitItemsNumber = data.limitItemsNumber;
const customItemAttributeParameters = data.customItemAttributeParameters || [];
const customEventAttributeParameters = data.customEventAttributeParameters || [];
function hasValue(v) {return v !== undefined && v !== null && v !== '';}
function isMissing(v) {return v === undefined || v === null || v === '';}
if(timestampDiff > attributionTime) {
items2 = [{item_id:"helper_id"}];
promo2 = undefined;
searchTerm2 = undefined;
customEvents2 = {};
}
if(data.variableType === 'attribution') {
if(data.deleteAttribution === true && event_name === 'purchase') {
let extract = {search_term: undefined, custom_events: {}, items:[{item_id:"helper_id"}], promotion: undefined, timestamp: timestamp};
extract = jsonData && extract ? JSON.stringify(extract) : extract;
return extract;
}
let item_list_id = hasValue(getEventData('item_list_id')) ? getEventData('item_list_id') : undefined;
let item_list_name = hasValue(getEventData('item_list_name')) ? getEventData('item_list_name') : undefined;
let creative_name = hasValue(getEventData('creative_name')) ? getEventData('creative_name') : undefined;
let creative_slot = hasValue(getEventData('creative_slot')) ? getEventData('creative_slot') : undefined;
let promotion_id = hasValue(getEventData('promotion_id')) ? getEventData('promotion_id') : undefined;
let promotion_name = hasValue(getEventData('promotion_name')) ? getEventData('promotion_name') : undefined;
let location_id = hasValue(getEventData('location_id')) ? getEventData('location_id') : undefined;
let index = hasValue(getEventData('index')) ? getEventData('index') : undefined;
let currentCustomEvents = {};
if (customEventAttributeParameters.length > 0) {
customEventAttributeParameters.forEach(row => {
const pName = row.parameterName;
const pVal = getEventData(pName);
if (hasValue(pVal)) {
currentCustomEvents[pName] = pVal;
}
});
}
let mergedCustomEvents = {};
// Added fallback to empty object just in case
const oldCeKeys = Object.keys(customEvents2 || {});
oldCeKeys.forEach(k => { mergedCustomEvents[k] = customEvents2[k]; });
const newCeKeys = Object.keys(currentCustomEvents || {});
newCeKeys.forEach(k => {
if (attributionType === 'firstClickAttribution') {
if (isMissing(mergedCustomEvents[k])) {
mergedCustomEvents[k] = currentCustomEvents[k];
}
} else {
mergedCustomEvents[k] = currentCustomEvents[k];
}
});
if (items) {
const mapItemsData = i => {
const itemObj = {
item_id: i.item_id,
item_list_id: hasValue(i.item_list_id) ? i.item_list_id : item_list_id,
item_list_name: hasValue(i.item_list_name) ? i.item_list_name : item_list_name,
creative_name: hasValue(i.creative_name) ? i.creative_name : creative_name,
creative_slot: hasValue(i.creative_slot) ? i.creative_slot : creative_slot,
promotion_id: hasValue(i.promotion_id) ? i.promotion_id : promotion_id,
promotion_name: hasValue(i.promotion_name) ? i.promotion_name : promotion_name,
location_id: hasValue(i.location_id) ? i.location_id : location_id,
index: hasValue(i.index) ? i.index : index
};
if (customItemAttributeParameters.length > 0) {
customItemAttributeParameters.forEach(row => {
const p = row.parameterName;
if (hasValue(i[p])) {
itemObj[p] = i[p];
}
});
}
return itemObj;
};
const items1 = items.map(mapItemsData);
const first = items1[0] || {};
const item_id = first.item_id ? first.item_id : undefined;
item_list_id = hasValue(first.item_list_id) ? first.item_list_id : undefined;
item_list_name = hasValue(first.item_list_name) ? first.item_list_name : undefined;
promotion_id = hasValue(first.promotion_id) ? first.promotion_id : promotion_id;
promotion_name = hasValue(first.promotion_name) ? first.promotion_name : promotion_name;
creative_name = hasValue(first.creative_name) ? first.creative_name : creative_name;
creative_slot = hasValue(first.creative_slot) ? first.creative_slot : creative_slot;
location_id = hasValue(first.location_id) ? first.location_id : location_id;
if (items1 && item_id && (item_list_id || item_list_name || promotion_id || promotion_name)) {
const firstClick = attributionType === 'firstClickAttribution';
const combined = firstClick ? items2.concat(items1) : items1.concat(items2);
const mergedMap = {};
for (let i = 0; i < items2.length; i++) {
const oldRec = items2[i];
const clone = {};
const flds = Object.keys(oldRec || {});
for (let j = 0; j < flds.length; j++) {
const k = flds[j];
clone[k] = oldRec[k];
}
mergedMap[oldRec.item_id] = clone;
}
items1.forEach(rec1 => {
const id = rec1.item_id;
let tgt = mergedMap[id];
if (!tgt) {
tgt = { item_id: id };
}
const isListEvent = hasValue(rec1.item_list_id) || hasValue(rec1.item_list_name);
if (isListEvent) {
if (attributionType === 'firstClickAttribution') {
if (isMissing(tgt.item_list_id)) tgt.item_list_id = rec1.item_list_id;
if (isMissing(tgt.item_list_name)) tgt.item_list_name = rec1.item_list_name;
} else {
tgt.item_list_id = rec1.item_list_id;
tgt.item_list_name = rec1.item_list_name;
}
}
const isPromoEvent = hasValue(rec1.promotion_id) || hasValue(rec1.promotion_name);
if (isPromoEvent) {
if (attributionType === 'firstClickAttribution') {
if (isMissing(tgt.promotion_id)) tgt.promotion_id = rec1.promotion_id;
if (isMissing(tgt.promotion_name)) tgt.promotion_name = rec1.promotion_name;
if (isMissing(tgt.creative_name)) tgt.creative_name = rec1.creative_name;
if (isMissing(tgt.creative_slot)) tgt.creative_slot = rec1.creative_slot;
} else {
tgt.promotion_id = rec1.promotion_id;
tgt.promotion_name = rec1.promotion_name;
tgt.creative_name = rec1.creative_name;
tgt.creative_slot = rec1.creative_slot;
}
}
if (hasValue(rec1.location_id)) {
if (attributionType === 'firstClickAttribution' ? isMissing(tgt.location_id) : true) {
tgt.location_id = rec1.location_id;
}
}
if (hasValue(rec1.index)) {
if (attributionType === 'firstClickAttribution' ? isMissing(tgt.index) : true) {
tgt.index = rec1.index;
}
}
if (customItemAttributeParameters.length > 0) {
customItemAttributeParameters.forEach(row => {
const p = row.parameterName;
if (hasValue(rec1[p])) {
if (attributionType === 'firstClickAttribution' ? isMissing(tgt[p]) : true) {
tgt[p] = rec1[p];
}
}
});
}
mergedMap[id] = tgt;
});
let uniqueItems = Object.keys(mergedMap || {}).map(function(k){ return mergedMap[k]; });
if (limitItemsNumber) {
uniqueItems = uniqueItems.slice(0, makeInteger(limitItemsNumber));
}
const extract = {
items: uniqueItems,
promotion: promo2,
search_term: searchTerm2,
custom_events: mergedCustomEvents,
timestamp: timestamp
};
return jsonData ? JSON.stringify(extract) : extract;
}
}
if (promotion_id||promotion_name) {
const promo = {creative_name: creative_name, creative_slot: creative_slot, promotion_id: promotion_id, promotion_name: promotion_name, location_id: location_id};
const promoAttribution = attributionType === 'firstClickAttribution' && promo2 ? promo2 : promo;
let extract = {items: items2, promotion: promoAttribution, search_term: searchTerm2, custom_events: mergedCustomEvents, timestamp: timestamp};
extract = jsonData && extract ? JSON.stringify(extract) : extract;
return extract;
}
const searchTerm = data.siteSearchChecbox && hasValue(getEventData('search_term')) ? getEventData('search_term') : undefined;
if (searchTerm) {
const siteSearchttribution = attributionType === 'firstClickAttribution' && searchTerm2 ? searchTerm2 : searchTerm;
let extract = {search_term: siteSearchttribution, items: items2, promotion: promo2, custom_events: mergedCustomEvents, timestamp: timestamp};
extract = jsonData && extract ? JSON.stringify(extract) : extract;
return extract;
}
}
else if (data.variableType === 'output') {
let output;
const param = data.outputDropDown;
if (param === 'promotion_id') {
output = promo2 ? promo2.promotion_id : undefined;
} else if (param === 'promotion_name') {
output = promo2 ? promo2.promotion_name : undefined;
} else if (param === 'creative_name') {
output = promo2 ? promo2.creative_name : undefined;
} else if (param === 'creative_slot') {
output = promo2 ? promo2.creative_slot : undefined;
} else if (param === 'location_id') {
output = promo2 ? promo2.location_id : undefined;
} else if (param === 'search_term') {
output = searchTerm2 ? searchTerm2 : undefined;
} else if (param === 'custom_event') {
const requestedKey = data.customEventOutputKey;
output = (customEvents2 && requestedKey) ? customEvents2[requestedKey] : undefined;
} else if (param === 'items' && items) {
items.forEach(item => {
if(data.itemSearchTerm && searchTerm2 ) {
item.search_term = searchTerm2;
}
if (data.itemCustomEvents) {
// Added fallback to empty object just in case
const ceKeys = Object.keys(customEvents2 || {});
if (ceKeys.length > 0) {
ceKeys.forEach(k => {
item[k] = item[k] || customEvents2[k] || undefined;
});
}
}
items2.forEach(item2 => {
if (item.item_id === item2.item_id) {
item.item_list_id = item.item_list_id || item2.item_list_id || undefined;
item.item_list_name = item.item_list_name || item2.item_list_name || undefined;
item.creative_name = item.creative_name || item2.creative_name || undefined;
item.creative_slot = item.creative_slot || item2.creative_slot || undefined;
item.promotion_id = item.promotion_id || item2.promotion_id || undefined;
item.promotion_name = item.promotion_name || item2.promotion_name || undefined;
item.location_id = item.location_id || item2.location_id || undefined;
item.index = item.index || item2.index || undefined;
// FIX: Added fallback to empty object just in case
const storedKeys = Object.keys(item2 || {});
const standardKeys = ['item_id', 'item_list_id', 'item_list_name', 'creative_name', 'creative_slot', 'promotion_id', 'promotion_name', 'location_id', 'index'];
storedKeys.forEach(k => {
if (standardKeys.indexOf(k) === -1) {
item[k] = item[k] || item2[k] || undefined;
}
});
}
});
});
output = items ? items : undefined;
}
return output;
}
___SERVER_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_event_data",
"versionId": "1"
},
"param": [
{
"key": "eventDataAccess",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios: []
___NOTES___
Created on 2/7/2023, 8:20:44 PM