-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.tpl
1164 lines (1100 loc) · 36.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
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
___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": "CLIENT",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "elbwalker",
"categories": [
"UTILITY"
],
"brand": {
"id": "mbaersch",
"displayName": "mbaersch",
"thumbnail": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAYCAYAAACbU/80AAAABmJLR0QAvgD/ALe9KR/IAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH5gseEAEKLjc4rQAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAMSSURBVEjH1VZLSJRRFP7O/f9hMoqMYoasGBOpaFNhSIvohSA12juSorIHahKJLaRVm6BAKiQj8gGJEC6KQJ0BLUjalIsgadVLZ4wk/ikpSR0dnXtaKGYzZ/75d9Fdnu+e8333nHPPvcA/XuR0o6cztJiUug6iEgDL5wDm3Zbf12Pn6w2GNwFUB6KtANyzfl8sv2+16ZA8gwyjB0DeXwBzH0fHXtiSB0K7QKobgCsBqgUA5ShNhnEliXxm1UeObNApyTsHCKSaBfIx1vFWRwI87Z8WADgnQF/1RLTNXjkdAFFOkp25IVKcM+JIAJmukwBWCkFavh1eH7V1VkaVYNUA189tcVCBMsE2zvH4bdvadwxsBrBDEN5u+bPDjgR4g4MFINoiBHkU2Zfz3Va2YVSnQG7+laQ0p5dTyFyX5totBdFRQfhby+976UiANziYC6K9AvTcKsruSyO8BsACwX4jqU1sglSKuNZ37O992AVSZ5IRHgbwxJEAbyCcCaJyAfrAU7FAmtOfBeAV0l9n+X2xRLOZ4v6eBrBQCHI/cnAte4Ph7QBVAygEeDYz3GUVrzkEpS4IESdBqsHRW+Bp7zfJND+DaEUC+y+enMwil+swlNEiiPOD9RSU8VTAmiy/r0wcFcmDx9yTTA6A0QTTZUIZ9QJBSP8Y7oJSVWJGmRtTzirBdlUKAa1vk1IXASwW8FtqydJcgAoF12dWUfZrRwK8gXBeisHTbhWvGQLhvEAwDq0fQqlLYk9pvms7rROa73KKFNZ6AqHjAPmE3NxjsALRCcHvnR4dCTgS4OnozwLRMSHIG6so+xUp44xYGuYHZJinAGQK+L1vJRu1nQDzz5tvVgIwhD213s7QZgAFgrguAJ/A3A1KulDfOTbxIN1LZwKA58n7DBBVCARDHI8/JtNoTFGaOyAqBtEqAWuNHFw36kgAud2lAJYJeDmIMgHxYWnjWKyb3O5ewe8jT09dc/LbmukBUhUJwXvBeqfl9wVJqUoAi+ahEbCu4eh4Kbnd+SDKn/9PAHMzpqe3Rfbn/sT/sH4DfEUaJgR8khoAAAAASUVORK5CYII\u003d"
},
"description": "Client for receiving walker.js events (with optional session data \u0026 conversion handling for GA4)",
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "GROUP",
"name": "walkerJs",
"displayName": "walker.js",
"subParams": [
{
"type": "CHECKBOX",
"name": "serveWalkerJs",
"checkboxText": "Serve walker.js through SGTM",
"simpleValueType": true,
"alwaysInSummary": true,
"defaultValue": false
},
{
"type": "TEXT",
"name": "walkerJsMaxAgeInMilliseconds",
"displayName": "Cache Max Age (Milliseconds)",
"simpleValueType": true,
"defaultValue": 60000,
"valueValidators": [
{
"type": "POSITIVE_NUMBER"
}
],
"enablingConditions": [
{
"paramName": "serveWalkerJs",
"paramValue": true,
"type": "EQUALS"
}
]
},
{
"type": "TEXT",
"name": "walkerJsPath",
"displayName": "walker.js URL path",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "serveWalkerJs",
"paramValue": true,
"type": "EQUALS"
}
],
"help": "The URL path under which the walker.js is served. Defaults to /walker.js if empty.",
"valueValidators": [
{
"type": "REGEX",
"args": [
"^\\/.*"
],
"errorMessage": "Enter your URL path with a leading slash, e. g. /my-awesome-path.js"
}
],
"alwaysInSummary": true,
"defaultValue": "/walker.js"
},
{
"type": "RADIO",
"name": "walkerJsVersionUseVersion",
"displayName": "walker.js Version",
"radioItems": [
{
"value": "specific",
"displayValue": "Specific Version"
},
{
"value": "latest",
"displayValue": "Always Use Latest Version"
}
],
"simpleValueType": true,
"defaultValue": "specific",
"enablingConditions": [
{
"paramName": "serveWalkerJs",
"paramValue": true,
"type": "EQUALS"
}
]
},
{
"type": "TEXT",
"name": "walkerJsVersion",
"displayName": "walker.js Version",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "walkerJsVersionUseVersion",
"paramValue": "specific",
"type": "EQUALS"
}
],
"help": "Enter the walker.js version you\u0027d like to use. Use the X.X.X format as specified on the walker.js Github repo, e. g. 2.0.0",
"defaultValue": "2.0.0"
},
{
"type": "LABEL",
"name": "walkerJsLatestVersionWarning",
"displayName": "WARNING! Do not use the this setting in production. Whenever a new version of walker.js is released, it will automatically be served on your site. If this version introduces any breaking changes, your data collection might not work as expected.",
"enablingConditions": [
{
"paramName": "walkerJsVersionUseVersion",
"paramValue": "latest",
"type": "EQUALS"
}
]
}
],
"groupStyle": "NO_ZIPPY"
},
{
"type": "GROUP",
"name": "sessionData",
"displayName": "Session \u0026 Client IDs",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "LABEL",
"name": "infoSessionNumber",
"displayName": "Most analytics tags (not only GA4) need at least a session id in order to know how to bundle events to a session. A separate client id is needed to recognize visitors beyond single sessions. If you want to use a GA4 tag, a session number is also mandatory. Leave fields blank to use fallback or default values."
},
{
"type": "GROUP",
"name": "groupSessionID",
"displayName": "Session ID",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "sessionDataSource",
"displayName": "Source",
"macrosInSelect": false,
"selectItems": [
{
"value": "request",
"displayValue": "Event Data"
},
{
"value": "user",
"displayValue": "User-Defined"
},
{
"value": "cookie",
"displayValue": "Cookies"
}
],
"simpleValueType": true,
"defaultValue": "request"
},
{
"type": "TEXT",
"name": "sessionIdSource",
"displayName": "Session ID Source Path",
"simpleValueType": true,
"defaultValue": "user.session",
"help": "enter additional lookup path to session id information in incoming events (default: user.session), if not sent as \"globals.session_started\". Will be a changing timestamp if no value is present anywhere",
"enablingConditions": [
{
"paramName": "sessionDataSource",
"paramValue": "request",
"type": "EQUALS"
}
]
},
{
"type": "TEXT",
"name": "sessionIdValue",
"displayName": "Session ID",
"simpleValueType": true,
"help": "enter constant or define variable to populate the session id. If none is present, a random number will be used inste",
"enablingConditions": [
{
"paramName": "sessionDataSource",
"paramValue": "user",
"type": "EQUALS"
}
],
"valueValidators": []
},
{
"type": "TEXT",
"name": "sessionCookieName",
"displayName": "Session ID Cookie-Name",
"simpleValueType": true,
"alwaysInSummary": false,
"enablingConditions": [
{
"paramName": "sessionDataSource",
"paramValue": "cookie",
"type": "EQUALS"
}
]
}
]
},
{
"type": "GROUP",
"name": "groupSessionNumber",
"displayName": "Session Number",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "sessionNumberDataSource",
"displayName": "Source",
"macrosInSelect": false,
"selectItems": [
{
"value": "request",
"displayValue": "Event Data"
},
{
"value": "user",
"displayValue": "User-Defined"
},
{
"value": "cookie",
"displayValue": "Cookies"
}
],
"simpleValueType": true,
"defaultValue": "request"
},
{
"type": "TEXT",
"name": "sessionNumberSource",
"displayName": "Session Number Source Path",
"simpleValueType": true,
"defaultValue": "globals.session_number",
"help": "enter lookup path to session number information in incoming events (default: globals.session_number). Will be \"1\" if not available in request",
"enablingConditions": [
{
"paramName": "sessionNumberDataSource",
"paramValue": "request",
"type": "EQUALS"
}
]
},
{
"type": "TEXT",
"name": "sessionNumberValue",
"displayName": "Session Number",
"simpleValueType": true,
"help": "enter constant or define variable to populate the session number (fallback will be \"1\")",
"enablingConditions": [
{
"paramName": "sessionNumberDataSource",
"paramValue": "user",
"type": "EQUALS"
}
],
"valueValidators": []
},
{
"type": "TEXT",
"name": "sessionNumberCookieName",
"displayName": "Session Number Cookie-Name",
"simpleValueType": true,
"alwaysInSummary": false,
"enablingConditions": [
{
"paramName": "sessionNumberDataSource",
"paramValue": "cookie",
"type": "EQUALS"
}
]
}
]
},
{
"type": "GROUP",
"name": "groupClientID",
"displayName": "Client ID",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "clientIdDataSource",
"displayName": "Source",
"macrosInSelect": false,
"selectItems": [
{
"value": "request",
"displayValue": "Event Data"
},
{
"value": "user",
"displayValue": "User-Defined"
},
{
"value": "cookie",
"displayValue": "Cookies"
}
],
"simpleValueType": true,
"defaultValue": "request"
},
{
"type": "TEXT",
"name": "clientIdSource",
"displayName": "Client ID Source Path",
"simpleValueType": true,
"defaultValue": "user.device",
"help": "enter lookup path to client id in incoming events (default: user.device). Will be set to session id if not available in request as \"user.device\" or \"user.session\".",
"enablingConditions": [
{
"paramName": "clientIdDataSource",
"paramValue": "request",
"type": "EQUALS"
}
]
},
{
"type": "TEXT",
"name": "clientIdValue",
"displayName": "Client ID",
"simpleValueType": true,
"help": "enter constant or define variable to populate the client id (fallback will be session id)",
"enablingConditions": [
{
"paramName": "clientIdDataSource",
"paramValue": "user",
"type": "EQUALS"
}
],
"valueValidators": []
},
{
"type": "TEXT",
"name": "clientCookieName",
"displayName": "Client ID Cookie-Name",
"simpleValueType": true,
"alwaysInSummary": false,
"enablingConditions": [
{
"paramName": "clientIdDataSource",
"paramValue": "cookie",
"type": "EQUALS"
}
]
}
]
}
]
},
{
"type": "GROUP",
"name": "eventData",
"displayName": "Event Name Processing",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "LABEL",
"name": "InfoEventNames",
"displayName": "If you send standard event names from walker.js without a specific client-side mapping, you can optionally adjust event names to match what your tags expect."
},
{
"type": "SELECT",
"name": "eventNameHandling",
"displayName": "Adjust Event Names",
"macrosInSelect": false,
"selectItems": [
{
"value": "none",
"displayValue": "No Changes"
},
{
"value": "normalize",
"displayValue": "Lowercase + Whitespace -\u003e Underscore"
},
{
"value": "map",
"displayValue": "Define Mapping Table"
}
],
"simpleValueType": true,
"defaultValue": "normalize"
},
{
"type": "SIMPLE_TABLE",
"name": "eventMapping",
"displayName": "Change Event Names",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Incoming Event Name",
"name": "walkerName",
"type": "TEXT",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"defaultValue": "",
"displayName": "New Event Name",
"name": "newName",
"type": "TEXT",
"isUnique": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
}
],
"enablingConditions": [
{
"paramName": "eventNameHandling",
"paramValue": "map",
"type": "EQUALS"
}
]
}
]
},
{
"type": "GROUP",
"name": "advancedSettings",
"displayName": "Advanced Settings",
"groupStyle": "ZIPPY_CLOSED",
"subParams": [
{
"type": "GROUP",
"name": "endpointSettings",
"displayName": "Endpoint Settings",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "TEXT",
"name": "endpointPath",
"displayName": "Endpoint Path",
"simpleValueType": true,
"help": "Enter a path where this client should listen for incoming events",
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"defaultValue": "/elbwalker"
}
]
},
{
"type": "GROUP",
"name": "eventSettings",
"displayName": "Event Model Options",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "LABEL",
"name": "labelEventSettings",
"displayName": "Define if and how the walker.js event structure should be part of the event model - additionally to all standard parameters that get populated by this client."
},
{
"type": "CHECKBOX",
"name": "includeWalkerEvent",
"checkboxText": "Include walker.js Event",
"simpleValueType": true,
"help": "check to add the complete received event as \"x-elb-event\"",
"defaultValue": true
},
{
"type": "CHECKBOX",
"name": "consentAsParams",
"checkboxText": "Add \"consent\" As Event Parameters",
"simpleValueType": true,
"defaultValue": true
},
{
"type": "CHECKBOX",
"name": "dataAsParams",
"checkboxText": "Add \"data\" As Event Parameters",
"simpleValueType": true,
"defaultValue": true
},
{
"type": "CHECKBOX",
"name": "contextAsParams",
"checkboxText": "Add \"context\" As Event Parameters",
"simpleValueType": true,
"defaultValue": true
},
{
"type": "CHECKBOX",
"name": "globalsAsParams",
"checkboxText": "Add \"globals\" As Event Parameters",
"simpleValueType": true,
"defaultValue": true
},
{
"type": "CHECKBOX",
"name": "customAsParams",
"checkboxText": "Add \"custom\" As Event Parameters",
"simpleValueType": true,
"defaultValue": true
},
{
"type": "LABEL",
"name": "labelObjInfo",
"displayName": "Note: When adding parameters from globals, context or data, keys with the same name will always contain the more specific value."
},
{
"type": "SIMPLE_TABLE",
"name": "addEventParameters",
"displayName": "Add / Edit Parameters",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Parameter Name",
"name": "paramName",
"type": "TEXT",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"defaultValue": "",
"displayName": "Parameter Value",
"name": "paramValue",
"type": "TEXT"
}
]
}
]
},
{
"type": "GROUP",
"name": "ga4Settings",
"displayName": "GA4 Specific Settings",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "LABEL",
"name": "ga4Info",
"displayName": "All settings in this section are only relevant if you want to send walker.js events to GA4."
},
{
"type": "CHECKBOX",
"name": "nestedToItems",
"checkboxText": "Convert \"nested\" To \"items\"",
"simpleValueType": true,
"defaultValue": true,
"help": "optionally convert nested data to e-commerce items for GA4"
},
{
"type": "TEXT",
"name": "nestedType",
"displayName": "Type Name",
"simpleValueType": true,
"help": "define item type (e. g. \"product\") to convert to event items. NOTE: The item properties for this type have to match the expected structure for GA4 e-commerce events",
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"enablingConditions": [
{
"paramName": "nestedToItems",
"paramValue": true,
"type": "EQUALS"
}
],
"defaultValue": "product"
},
{
"type": "TEXT",
"name": "conversionNames",
"displayName": "Conversion Event Names",
"simpleValueType": true,
"lineCount": 10,
"textAsList": true,
"help": "enter one event name (after changes defined in this client) per line to be marked as GA4 conversion",
"defaultValue": "purchase"
},
{
"type": "CHECKBOX",
"name": "sendAsDebug",
"checkboxText": "Send GA4 Debug Flag",
"simpleValueType": true,
"help": "activate for enabling GA4 DebugView"
},
{
"type": "GROUP",
"name": "GrpSessionStartMarker",
"displayName": "Session Start Marker",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "sessionStartDataSource",
"displayName": "Source",
"macrosInSelect": false,
"selectItems": [
{
"value": "request",
"displayValue": "Event Data"
},
{
"value": "user",
"displayValue": "User-Defined"
}
],
"simpleValueType": true,
"defaultValue": "request"
},
{
"type": "TEXT",
"name": "sessionStartPath",
"displayName": "GA4 Session Start Marker Path",
"simpleValueType": true,
"defaultValue": "globals.session_start",
"help": "if you want to send data to GA4, a value must be present in a certain key (default: globals.session_start). Define a fallback path here. Any \"truthy\" value will trigger a \"session_start\" marker in incoming events if the event counter (\"count\") indicates the first event after loading. An additional \"first_visit\" marker is added in case the session number equals \"1\".",
"enablingConditions": [
{
"paramName": "sessionStartDataSource",
"paramValue": "user",
"type": "NOT_EQUALS"
}
]
},
{
"type": "TEXT",
"name": "sessionStartVar",
"displayName": "GA4 Session Start Marker Variable",
"simpleValueType": true,
"defaultValue": "",
"help": "if you want to send data to GA4, define a variable for a marker here. Any \"truthy\" value will trigger a \"session_start\" marker in incoming events if the event counter (\"count\") indicates the first event after loading. An additional \"first_visit\" marker is added in case the session number equals \"1\".",
"enablingConditions": [
{
"paramName": "sessionStartDataSource",
"paramValue": "user",
"type": "EQUALS"
}
]
}
]
}
]
}
]
}
]
___SANDBOXED_JS_FOR_SERVER___
/**
* @description Custom Google Tag Manager Client Template for walker.js
* @version 0.2.1
* @see {@link https://github.com/elbwalker|elbwalker on GitHub} for more about walker.js
*/
const getType = require('getType');
const claimRequest = require('claimRequest');
const returnResponse = require('returnResponse');
const runContainer = require('runContainer');
const decodeUriComponent = require('decodeUriComponent');
const setPixelResponse = require('setPixelResponse');
const setResponseHeader = require('setResponseHeader');
const setResponseStatus = require("setResponseStatus");
const setResponseBody = require("setResponseBody");
const getRequestHeader = require('getRequestHeader');
const getRequestBody = require('getRequestBody');
const getRequestQueryParameter = require('getRequestQueryParameter');
const getRequestPath = require('getRequestPath');
const getCookie = require('getCookieValues');
const JSON = require('JSON');
const getRemoteAddress = require('getRemoteAddress');
const fromBase64 = require('fromBase64');
const makeNumber = require('makeNumber');
const sendHttpGet = require("sendHttpGet");
const getTimestampMillis = require("getTimestampMillis");
const templateDataStorage = require('templateDataStorage');
const Object = require('Object');
//helper
/**
* creates an event parameter if value is set.
* @param {Object} event - event object to handle.
* @param {string} fieldName - name for key / parameter to add.
* @param {*} val - parameter value object.
*/
const createField = (event, fieldName, val) => {
if (val) event[fieldName] = val;
};
/**
* reads nested object properties.
* @param {Object} ob - object to get value from.
* @param {string} path - (user defined) path to value using regular JS / "dot" notation.
*/
const valueByPath = (ob, path) => {
//if there is no valid path, return the path name / object
if (getType(path) !== "string") return path;
return path.split('.').reduce(function(o, k) {
var ind = k.indexOf('[');
var ind2 = k.indexOf(']');
if (ind >=0 && ind2 > ind) {
var kOrg = k;
k = kOrg.substring(0,ind);
var key = kOrg.substring(ind+1, ind2);
if (k != "")
return o && o[k][key];
else
return o && o[key];
} else return o && o[k];
}, ob);
};
/**
* responds with cached or fresh version of walker.js script as client result.
* @param {string} scriptBody - script content to include in response.
* @param {boolean} isCached - flag for script origin - used to send a custom [debug] header.
*/
const returnWalkerJs = function(scriptBody, isCached) {
setResponseStatus(200);
setResponseBody(scriptBody);
setResponseHeader("Content-Type", "text/javascript");
setResponseHeader("Content-Encoding", "gzip");
setResponseHeader("Vary", "accept-encoding");
if (isCached) {
setResponseHeader("x-sgtm-templatestorage-cached", "true");
}
returnResponse();
};
/**
* adds array data from walker.js script as event parameters.
* Object has no "assign" method in this sandbox :(
* @param {object} event - object to add parameters.
* @param {object} dims - object to add.
*/
const addParamsFromArray = function(event, dims) {
if (dims && typeof(dims === "object")) {
for (var key in dims) {
event[key] = dims[key];
}
}
};
/******************************************************************/
const requestPath = getRequestPath();
//claim elbwalker requests
if (requestPath === (data.endpointPath || '/elbwalker')) {
claimRequest();
setResponseHeader("Access-Control-Allow-Origin", "*");
setResponseHeader("Access-Control-Allow-Credentials", "true");
setResponseHeader("Access-Control-Allow-Headers", "*");
const ip = getRemoteAddress();
const ua = getRequestHeader('user-agent');
const ref = getRequestHeader('Referer');
const lng = getRequestHeader('Accept-Language') || "";
var objs = getRequestBody();
//use GET Parameter as fallback
if (!objs) objs = getRequestQueryParameter('o');
var evtData;
if (objs && objs != "") {
if (objs.substring(0,1) == "{") evtData = JSON.parse(objs);
else {
//if data is base64 coded, decode and re-parse
if (!evtData) evtData = JSON.parse(fromBase64(objs));
}
}
evtData = evtData||{};
const fallbackSession = getTimestampMillis();
//session id or client id can be included in request or user as defined fields
if (data.sessionDataSource === "user") {
var sid = data.sessionIdValue || fallbackSession;
} else if (data.sessionDataSource === "cookie") {
var sid = getCookie(data.sessionCookieName)[0] || fallbackSession;
} else {
if (!data.sessionIdSource || data.sessionIdSource === "") data.sessionIdSource = "globals.session_started";
var sid = valueByPath(evtData, data.sessionIdSource) || fallbackSession;
}
if (data.sessionNumberDataSource === "user") {
var snum = data.sessionNumberValue || "1";
} else if (data.sessionDataSource === "cookie") {
var snum = getCookie(data.sessionNumberCookieName)[0] || "1";
} else {
if (!data.sessionNumberSource || data.sessionNumberSource === "") data.sessionNumberSource = "globals.session_number";
var snum = valueByPath(evtData, data.sessionNumberSource) || "1";
}
if (data.clientIdDataSource === "user") {
var cid = data.clientIdValue || sid;
} else if (data.sessionDataSource === "cookie") {
var cid = getCookie(data.clientCookieName)[0] || fallbackSession;
} else {
if (!data.clientIdSource || data.clientIdSource === "") data.clientIdSource = "user.device";
var cid = valueByPath(evtData, data.clientIdSource) || valueByPath(evtData, "user.device") ||
valueByPath(evtData, "user.session") || valueByPath(evtData, "user.hash") ||
fallbackSession;
}
var evName = evtData.event || "elb_event";
if (data.eventNameHandling === "normalize")
evName = evName.toLowerCase().replace(" ", "_");
else if ((data.eventNameHandling === "map") &&
data.eventMapping && (data.eventMapping.length > 0)) {
for (var i=0;i<data.eventMapping.length;i++)
if (evName === data.eventMapping[i].walkerName) {
evName = data.eventMapping[i].newName;
break;
}
}
var loc = (evtData.source||{}).id || ref;
var event = { anonymize_ip: true,
event_name: evName,
client_id : cid,
ip_override : ip,
user_agent : ua,
page_location : loc
};
if (evtData.data) {
createField(event, "page_hostname", evtData.data.domain);
createField(event, "page_path", evtData.data.id);
createField(event, "page_title", evtData.data.title);
}
if (evtData.user && evtData.user.id)
createField(event, "user_id", evtData.user.id);
createField(event, "page_referrer", (evtData.source||{}).previous_id);
createField(event, "client_timestamp", evtData.timestamp);
createField(event, "event_id", evtData.id);
var version = (evtData.version||{});
//add some walker.js properties
createField(event, "x-elb-config-version", version.config||version.tagging||0);
createField(event, "x-elb-walker-version", version.walker||version.client||0);
createField(event, "x-elb-event", evtData);
//add proprietary Google Analytics properties
event['x-ga-js_client_id'] = cid;
event['x-ga-page_id'] = evtData.group;
event['x-ga-request_count'] = evtData.count;
event.ga_session_number = snum;
event.ga_session_id = sid;
//use consent as event parameters
if (data.consentAsParams === true)
addParamsFromArray(event, evtData.consent);
//use globals as event parameters
if (data.globalsAsParams === true)
addParamsFromArray(event, evtData.globals);
//use context data as event parameters
if (data.contextAsParams === true)
addParamsFromArray(event, evtData.context);
//use data as event parameters
if (data.dataAsParams === true)
addParamsFromArray(event, evtData.data);
//use custom as event parameters
if (data.customAsParams === true) {
addParamsFromArray(event, evtData.custom);
}
if (data.nestedToItems === true) {
var items = [];
(evtData.nested||{}).filter(
x => x.type==data.nestedType
).forEach(
x => items.push(x.data)
);
if (items.length > 0) event.items = items;
}
//add / edit event parameters
if (data.addEventParameters)
data.addEventParameters.forEach((v, i) => {
event[v.paramName] = v.paramValue;
});
//GA4 system properties
var sprops = {};
if (!data.sessionStartPath || data.sessionStartPath === "") data.sessionStartPath = "globals.session_start";
var isStart = (data.sessionStartDataSource === "user") ? data.sessionStartVar : valueByPath(evtData,
data.sessionStartPath);
isStart = (isStart == true);
isStart = isStart && (makeNumber(evtData.count) === 1);
if (isStart) sprops.ss = "1";
if (isStart && (makeNumber(snum) === 1)) sprops.fv = "1";
//use final event_name value
if (data.conversionNames.indexOf(event.event_name) >= 0) sprops.c = "1";
if (data.sendAsDebug === true) sprops.dbg = "1";
if (sprops.ss || sprops.fv || sprops.dbg || sprops.c)
event["x-ga-system_properties"] = sprops;
//everything except session start will be "engaged"
event['x-ga-mp2-seg'] = isStart == true ? "0" : "1";
/*2DO:
------------------------------------------------------------------------
- no hit contains engagement time. If you need engagement metrics,
a client side timer will be needed and engagement time must be sent as
parameter or set as field in this client manually
(using the advanced settings)
*/
runContainer(event, () => {
setPixelResponse();
returnResponse();
});
} else if ((data.serveWalkerJs === true) && (requestPath === (data.walkerJsPath || "/walker.js"))) {
//walker.js Proxy - by Justus ;)
claimRequest();
//walker.js is cached in the container to avoid hitting the CDN for every request
//and to speed up delivery to the browser
const walkerJsMaxAgeInMilliseconds = data.walkerJsMaxAgeInMilliseconds || 60000;
const walkerJsCached = templateDataStorage.getItemCopy("walkerJs");
const walkerJsLastModified = makeNumber(templateDataStorage.getItemCopy("walkerJsLastModified"));
//if the cached walker.js is still fresh, return it
if (walkerJsCached && walkerJsCached.indexOf("elbwalker") > -1 &&
getTimestampMillis() - walkerJsLastModified < walkerJsMaxAgeInMilliseconds) {
if (walkerJsLastModified && getTimestampMillis() -
walkerJsLastModified < walkerJsMaxAgeInMilliseconds) {
returnWalkerJs(walkerJsCached, true);
}
} else {
var version = "latest";
if (data.walkerJsVersionUseVersion === "specific")
version = data.walkerJsVersion;
sendHttpGet("https://cdn.jsdelivr.net/npm/@elbwalker/walker.js@" + version + "/dist/walker.js")
.then((result) => {
//make extra-sure that the script actually looks reasonable before
//caching it in the container
if (result.statusCode === 200 && result.body.indexOf("elbwalker") > -1) {
templateDataStorage.setItemCopy("walkerJs", result.body);
templateDataStorage.setItemCopy("walkerJsLastModified", getTimestampMillis());
}
returnWalkerJs(result.body);
}, () => {
setResponseStatus(500);
returnResponse();
});
}
}