-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.tpl
1223 lines (1128 loc) · 36.4 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": "TAG",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "Mixpanel",
"brand": {
"id": "github.com_stape-io",
"displayName": "stape-io",
"thumbnail": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAAAXNSR0IArs4c6QAAAFBlWElmTU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAYKADAAQAAAABAAAAYAAAAAC77nFQAAABWWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNi4wLjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp0aWZmPSJodHRwOi8vbnMuYWRvYmUuY29tL3RpZmYvMS4wLyI+CiAgICAgICAgIDx0aWZmOk9yaWVudGF0aW9uPjE8L3RpZmY6T3JpZW50YXRpb24+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgoZXuEHAAAGvklEQVR4Ae2aW2wUVRjHvzPTQotiBVQIcscIEiBGor4RHtSQGCBAtsUbGl8AhUQSQ7k9bCKm3BIwGKKoUUHFZmm4Bg3SxMQn8UqIXEXAB1FQQEF6nT3+z3Sn3cLYnd2Z2RL4TzKZmTPn/L8zv+/cz4jwIAESIAESIAESIAESIAESIAESIAESIAESIAESI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ixpanel server API for user and event tracking.",
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "TEXT",
"name": "token",
"displayName": "Project Token",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"help": "You can get the Project Token from \u003ca href\u003d\"https://mixpanel.com/settings/project\" target\u003d\"_blank\"\u003eproject settings\u003c/a\u003e.",
"alwaysInSummary": true
},
{
"type": "SELECT",
"name": "type",
"displayName": "Tag Type",
"selectItems": [
{
"value": "track",
"displayValue": "track"
},
{
"value": "identify",
"displayValue": "identify"
},
{
"value": "alias",
"displayValue": "alias"
},
{
"value": "reset",
"displayValue": "reset"
},
{
"value": "profile-set",
"displayValue": "set"
},
{
"value": "profile-append",
"displayValue": "append"
}
],
"simpleValueType": true,
"defaultValue": "track"
},
{
"type": "CHECKBOX",
"name": "serverEU",
"checkboxText": "Route data to Mixpanel\u0027s EU servers",
"simpleValueType": true,
"defaultValue": false
},
{
"type": "CHECKBOX",
"name": "identifyAuto",
"checkboxText": "Automatically handle customer distinct_id",
"simpleValueType": true,
"help": "Mixpanel server API optimized for stateless shared usage; e.g., in a web application, the same mixpanel instance is used across requests for all users. Rather than setting a distinct_id through identify() calls like Mixpanel client-side libraries (where a single Mixpanel instance is tied to a single user), this api requires you to pass the distinct_id with every tracking call.",
"defaultValue": true,
"enablingConditions": [
{
"paramName": "type",
"paramValue": "reset",
"type": "NOT_EQUALS"
}
]
},
{
"type": "TEXT",
"name": "identifyCustom",
"displayName": "Distinct ID",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "identifyAuto",
"paramValue": false,
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "appendGroup",
"displayName": "User Properties to append",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "SIMPLE_TABLE",
"name": "userPropertiesToAppend",
"displayName": "",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "User Property",
"name": "propertyName",
"type": "TEXT"
},
{
"defaultValue": "",
"displayName": "Value",
"name": "valueToAppend",
"type": "TEXT"
}
]
}
],
"enablingConditions": [
{
"paramName": "type",
"paramValue": "profile-append",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "setGroup",
"displayName": "User Properties",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "SIMPLE_TABLE",
"name": "userPropertiesTable",
"displayName": "",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "User Property",
"name": "userProperty",
"type": "TEXT"
},
{
"defaultValue": "",
"displayName": "Value",
"name": "value",
"type": "TEXT"
}
]
}
],
"enablingConditions": [
{
"paramName": "type",
"paramValue": "profile-set",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "identifyGroup",
"displayName": "Identify Options",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "TEXT",
"name": "identifier",
"displayName": "Identifier",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
}
],
"enablingConditions": [
{
"paramName": "type",
"paramValue": "identify",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "aliasGroup",
"displayName": "Alias Options",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "TEXT",
"name": "alias",
"displayName": "Alias",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"help": ""
}
],
"enablingConditions": [
{
"paramName": "type",
"paramValue": "alias",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "trackGroup",
"displayName": "Track Options",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "TEXT",
"name": "trackName",
"displayName": "Event Name",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "CHECKBOX",
"name": "trackCommonData",
"checkboxText": "Send common data with request",
"simpleValueType": true,
"help": "Adds to track request user_agent, path, $current_url, $screen_width, $screen_height, $referrer, user ip, etc.",
"defaultValue": true
},
{
"type": "CHECKBOX",
"name": "trackFromVariable",
"checkboxText": "Get parameters from variable",
"simpleValueType": true
},
{
"type": "SELECT",
"name": "trackParametersObject",
"displayName": "Parameters object",
"macrosInSelect": true,
"selectItems": [],
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "trackFromVariable",
"paramValue": true,
"type": "EQUALS"
}
]
},
{
"type": "SIMPLE_TABLE",
"name": "trackParameters",
"displayName": "Additional Parameters (Optional)",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Parameter Name",
"name": "name",
"type": "TEXT"
},
{
"defaultValue": "",
"displayName": "Value",
"name": "value",
"type": "TEXT"
}
]
},
{
"type": "SIMPLE_TABLE",
"name": "trackList",
"displayName": "Parameters as lists",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Parameter Name",
"name": "listName",
"type": "TEXT"
},
{
"defaultValue": "",
"displayName": "Values",
"name": "listValues",
"type": "TEXT"
}
],
"help": "List of parameters separated by comma sent as a Data Type \u003d \"list\". For Example: tags: tag1,tag2,tag3"
},
{
"type": "SIMPLE_TABLE",
"name": "trackParametersRemove",
"displayName": "Remove parameters from the request",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "Parameter Name",
"name": "name",
"type": "TEXT"
}
],
"help": "Useful for removing common parameters like $current_url or ip"
}
],
"enablingConditions": [
{
"paramName": "type",
"paramValue": "track",
"type": "EQUALS"
}
]
},
{
"displayName": "Logs Settings",
"name": "logsGroup",
"groupStyle": "ZIPPY_CLOSED",
"type": "GROUP",
"subParams": [
{
"type": "RADIO",
"name": "logType",
"radioItems": [
{
"value": "no",
"displayValue": "Do not log"
},
{
"value": "debug",
"displayValue": "Log to console during debug and preview"
},
{
"value": "always",
"displayValue": "Always log to console"
}
],
"simpleValueType": true,
"defaultValue": "debug"
}
]
}
]
___SANDBOXED_JS_FOR_SERVER___
const getAllEventData = require('getAllEventData');
const JSON = require('JSON');
const sendHttpRequest = require('sendHttpRequest');
const getTimestampMillis = require('getTimestampMillis');
const setCookie = require('setCookie');
const getCookieValues = require('getCookieValues');
const getContainerVersion = require('getContainerVersion');
const logToConsole = require('logToConsole');
const getRequestHeader = require('getRequestHeader');
const generateRandom = require('generateRandom');
const parseUrl = require('parseUrl');
const makeString = require('makeString');
const Object = require('Object');
const makeNumber = require('makeNumber');
const getTimestamp = require('getTimestamp');
const postUrl = 'https://' + (data.serverEU ? 'api-eu.mixpanel.com' : 'api.mixpanel.com');
const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');
const eventData = getAllEventData();
let cookieOptions = {
domain: 'auto',
path: '/',
samesite: 'Lax',
secure: true,
'max-age': 31536000, // 1 year
httpOnly: false
};
if (data.type === 'track') {
sendTrackRequest();
} else if (data.type === 'alias') {
sendAliasRequest();
} else if (data.type === 'identify') {
sendIdentifyRequest();
} else if (data.type === 'profile-set') {
sendSetProfileRequest();
} else if (data.type === 'profile-append') {
sendAppendProfileRequest();
} else if (data.type === 'reset') {
cookieOptions['max-age'] = 1;
setCookie('stape_mixpanel_distinct_id', 'empty', cookieOptions);
setCookie('stape_mixpanel_device_id', 'empty', cookieOptions);
data.gtmOnSuccess();
return;
}
function sendAppendProfileRequest() {
const propertiesToAppend = {};
data.userPropertiesToAppend.forEach(row => {
if (!propertiesToAppend[row.propertyName]) {
propertiesToAppend[row.propertyName] = [];
}
propertiesToAppend[row.propertyName].push(row.valueToAppend);
});
const profileBody = {
'$token': data.token,
'$distinct_id': getDistinctId(),
'$append': propertiesToAppend
};
const postUrlAppend = postUrl + '/engage#profile-list-append';
// Logging the request if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Request',
'TraceId': traceId,
'EventName': 'Profile Append',
'RequestMethod': 'POST',
'RequestUrl': postUrlAppend,
'RequestBody': profileBody,
}));
}
// Sending the HTTP request to Mixpanel
sendHttpRequest(postUrlAppend, (statusCode, headers, body) => {
// Logging the response if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Response',
'TraceId': traceId,
'EventName': 'Profile Append',
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}
// Handling the response
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}
function sendSetProfileRequest() {
const userProperties = {};
data.userPropertiesTable.forEach(row => {
userProperties[row.userProperty] = row.value;
});
const profileBody = {
'$token': data.token,
'$distinct_id': getDistinctId(),
'$ip': eventData.ip_override || eventData.ip,
'$set': userProperties
};
const postUrlSet = postUrl + '/engage#profile-set';
// Logging the request if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Request',
'TraceId': traceId,
'EventName': 'Profile Set',
'RequestMethod': 'POST',
'RequestUrl': postUrlSet,
'RequestBody': profileBody,
}));
}
// Sending the HTTP request to Mixpanel
sendHttpRequest(postUrlSet, (statusCode, headers, body) => {
// Logging the response if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Response',
'TraceId': traceId,
'EventName': 'Profile Set',
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}
// Handling the response
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}
function sendTrackRequest() {
let postBody = {
properties: {}
};
if (data.trackCommonData) {
postBody = trackCommonData(postBody);
}
if (data.trackFromVariable && data.trackParametersObject) {
for (let key in data.trackParametersObject) {
postBody.properties[key] = data.trackParametersObject[key];
}
}
if (data.trackParameters) {
data.trackParameters.forEach(d => {
postBody.properties[d.name] = d.value;
});
}
if (data.trackList) {
data.trackList.forEach(d => {
// Check if listValues is a string and have commas
if (typeof d.listValues === 'string' && d.listValues.indexOf(',') !== -1) {
// Convert a string to an array of strings, removing whitespace characters and dividing by commas
postBody.properties[d.listName] = d.listValues.split(',').map(tag => tag.trim());
} else {
// If it is not a comma-delimited string, assign the value of listValues unchanged
postBody.properties[d.listName] = d.listValues;
}
});
}
if (data.trackParametersRemove) {
data.trackParametersRemove.forEach(d => {
Object.delete(postBody.properties, d.name);
});
}
sendRequest(data.trackName, postBody);
}
function sendAliasRequest() {
sendRequest('$create_alias', {
properties: {
alias: data.alias
}
});
}
function sendIdentifyRequest() {
sendRequest('$identify', {
properties: {
'$identified_id': data.identifier,
'$anon_id': getDistinctId()
}
});
}
function sendRequest(eventName, postBody) {
postBody.event = eventName;
if (!postBody.properties) postBody.properties = {};
postBody.properties.token = data.token;
if (data.type !== 'identify') {
postBody.properties.distinct_id = getDistinctId();
postBody.properties['$device_id'] = getDeviceId(postBody.properties.distinct_id);
if (data.identifyAuto) setDistinctIdCookies(postBody.properties.distinct_id, postBody.properties['$device_id']);
}
const postUrl = 'https://' + (data.serverEU ? 'api-eu.mixpanel.com' : 'api.mixpanel.com') + '/track?verbose=1';
postBody = [postBody];
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Request',
'TraceId': traceId,
'EventName': eventName,
'RequestMethod': 'POST',
'RequestUrl': postUrl,
'RequestBody': postBody,
}));
}
sendHttpRequest(postUrl, (statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Response',
'TraceId': traceId,
'EventName': eventName,
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify(postBody));
}
function getDistinctId() {
if (!data.identifyAuto) return data.identifyCustom;
let mpData = getCookieValues('mp_' + data.token + '_mixpanel')[0];
if (mpData) return JSON.parse(mpData).distinct_id;
let distinctIdCookie = getCookieValues('stape_mixpanel_distinct_id')[0];
if (distinctIdCookie) return distinctIdCookie;
return UUID();
}
function getDeviceId(distinctId) {
if (!data.identifyAuto) return data.identifyCustom;
let mpData = getCookieValues('mp_' + data.token + '_mixpanel')[0];
if (mpData) return JSON.parse(mpData)['$device_id'];
let distinctIdCookie = getCookieValues('stape_mixpanel_device_id')[0];
if (distinctIdCookie) return distinctIdCookie;
return distinctId;
}
function setDistinctIdCookies(distinctId, deviceId) {
setCookie('stape_mixpanel_distinct_id', makeString(distinctId), cookieOptions);
setCookie('stape_mixpanel_device_id', makeString(deviceId), cookieOptions);
}
function trackCommonData(postBody) {
postBody = {
properties: {
'ip': eventData.ip_override || eventData.ip,
'mp_lib': 'stape',
'$lib_version': '1.0.0',
}
};
if (eventData.user_agent) postBody.properties.user_agent = eventData.user_agent;
if (eventData.page_path) postBody.properties.path = eventData.page_path;
if (eventData.page_location) postBody.properties['$current_url'] = eventData.page_location;
if (eventData.screen_resolution) postBody.properties['$screen_width'] = eventData.screen_resolution.split('x')[0];
if (eventData.screen_resolution) postBody.properties['$screen_height'] = eventData.screen_resolution.split('x')[1];
if (eventData.page_referrer) postBody.properties['$referrer'] = eventData.page_referrer;
if (eventData.page_referrer) postBody.properties['$referring_domain'] = parseUrl(eventData.page_referrer).hostname;
const url = eventData.page_location || getRequestHeader('referer');
const urlParsed = parseUrl(url);
if (urlParsed && urlParsed.searchParams.utm_medium) postBody.properties['utm_medium'] = urlParsed.searchParams.utm_medium;
if (urlParsed && urlParsed.searchParams.utm_source) postBody.properties['utm_source'] = urlParsed.searchParams.utm_source;
if (urlParsed && urlParsed.searchParams.utm_campaign) postBody.properties['utm_campaign'] = urlParsed.searchParams.utm_campaign;
if (urlParsed && urlParsed.searchParams.utm_content) postBody.properties['utm_content'] = urlParsed.searchParams.utm_content;
if (postBody.properties['$referrer']) {
let searchEngine = getSearchEngine(postBody.properties['$referrer']);
if (searchEngine) {
let searchEngineKeyword = null;
if (urlParsed) {
searchEngineKeyword = searchEngine !== 'yahoo' ? urlParsed.searchParams.q : urlParsed.searchParams.p;
}
postBody.properties['$search_engine'] = searchEngine;
if (searchEngineKeyword) postBody.properties['mp_keyword'] = searchEngineKeyword;
}
}
if (postBody.properties.user_agent) {
let os = getOS(postBody.properties.user_agent);
let device = getDevice(postBody.properties.user_agent);
let browser = getBrowser(postBody.properties.user_agent);
let browserVersion = getBrowserVersion(postBody.properties.user_agent, browser);
if (os) postBody.properties['$os'] = os;
if (device) postBody.properties['$device'] = device;
if (browser) postBody.properties['$browser'] = browser;
if (browserVersion) postBody.properties['$browser_version'] = browserVersion;
}
if (!data.trackParametersRemove || !data.trackParametersRemove.some(el => el.name === '$initial_referrer')) {
let initialReferrer = getCookieValues('stape_mixpanel_initial_referrer')[0];
if (initialReferrer) postBody.properties['$initial_referrer'] = initialReferrer;
if (!initialReferrer) {
postBody.properties['$initial_referrer'] = postBody.properties['$referrer'] ? postBody.properties['$referrer'] : 'direct';
setCookie('stape_mixpanel_initial_referrer', makeString(postBody.properties['$initial_referrer']), cookieOptions);
}
}
if (postBody.properties['$initial_referrer'] && postBody.properties['$initial_referrer'] !== 'direct') postBody.properties['$initial_referring_domain'] = parseUrl(postBody.properties['$initial_referrer']).hostname;
return postBody;
}
function getOS(userAgent) {
if (userAgent.toLowerCase().match('windows') && userAgent.match('Phone')) return 'Windows Mobile';
else if (userAgent.toLowerCase().match('windows')) return 'Windows';
else if (userAgent.match('(iPhone|iPad|iPod)')) return 'iOS';
else if (userAgent.match('Android')) return 'Android';
else if (userAgent.match('(BlackBerry|PlayBook|BB10)')) return 'BlackBerry';
else if (userAgent.match('Mac')) return 'Mac OS X';
else if (userAgent.match('Linux')) return 'Linux';
return '';
}
function getDevice(userAgent) {
if (userAgent.match('iPad')) return 'iPad';
else if (userAgent.match('iPod')) return 'iPod Touch';
else if (userAgent.match('iPhone')) return 'iPhone';
else if (userAgent.toLowerCase().match('(blackberry|playbook|bb10)')) return 'BlackBerry';
else if (userAgent.toLowerCase().match('windows phone')) return 'Windows Phone';
else if (userAgent.match('Android')) return 'Android';
return '';
}
function getBrowser(userAgent) {
if (userAgent.match('Opera Mini')) return 'Opera Mini';
if (userAgent.match('Opera')) return 'Opera';
if (userAgent.toLowerCase().match('(BlackBerry|PlayBook|BB10)')) return 'BlackBerry';
if (userAgent.match('FBIOS')) return 'Facebook Mobile';
if (userAgent.match('Chrome')) return 'Chrome';
if (userAgent.match('CriOS')) return 'Chrome iOS';
if (userAgent.match('Apple') && userAgent.match('Mobile')) return 'Mobile Safari';
if (userAgent.match('Apple')) return 'Safari';
if (userAgent.match('Android')) return 'Android Mobile';
if (userAgent.match('Konqueror')) return 'Konqueror';
if (userAgent.match('Firefox')) return 'Firefox';
if (userAgent.match('MSIE') || userAgent.match('Trident')) return 'Internet Explorer';
if (userAgent.match('Gecko')) return 'Mozilla';
return '';
}
function getBrowserVersion(userAgent, browser) {
const versionRegexs = {
'Internet Explorer Mobile': 'rv:([0-9]+(.[0-9]+)?)',
'Microsoft Edge': 'Edge?/([0-9]+(.[0-9]+)?)',
'Chrome': 'Chrome/([0-9]+(.[0-9]+)?)',
'Chrome iOS': 'CriOS/([0-9]+(.[0-9]+)?)',
'UC Browser' : '(UCBrowser|UCWEB)/([0-9]+(.[0-9]+)?)',
'Safari': 'Version/([0-9]+(.[0-9]+)?)',
'Mobile Safari': 'Version/([0-9]+(.[0-9]+)?)',
'Opera': '(Opera|OPR)/([0-9]+(.[0-9]+)?)',
'Firefox': 'Firefox/([0-9]+(.[0-9]+)?)',
'Firefox iOS': 'FxiOS/([0-9]+(.[0-9]+)?)',
'Konqueror': 'Konqueror:([0-9]+(.[0-9]+)?)',
'BlackBerry': 'BlackBerry ([0-9]+(.[0-9]+)?)',
'Android Mobile': 'android.([0-9]+(.[0-9]+)?)',
'Samsung Internet': 'SamsungBrowser/([0-9]+(.[0-9]+)?)',
'Internet Explorer': '(rv:|MSIE )([0-9]+(.[0-9]+)?)',
'Mozilla': 'rv:([0-9]+(.[0-9]+)?)'
};
let regex = versionRegexs[browser];
if (regex === undefined) {
return null;
}
let matches = userAgent.match(regex);
if (!matches) {
return null;
}
return makeNumber(matches[matches.length - 2]);
}
function getSearchEngine(referrer) {
if (referrer.search('https?://(.*)google.([^/?]*)') === 0) return 'google';
else if (referrer.search('https?://(.*)bing.com') === 0) return 'bing';
else if (referrer.search('https?://(.*)yahoo.com') === 0) return 'yahoo';
else if (referrer.search('https?://(.*)duckduckgo.com') === 0) return 'duckduckgo';
return null;
}
function random() {
return generateRandom(1000000000000000, 10000000000000000)/10000000000000000;
}
function UUID() {
function s(n) { return h((random() * (1<<(n<<2)))^getTimestamp()).slice(-n); }
function h(n) { return (n|0).toString(16); }
return [
s(4) + s(4), s(4),
'4' + s(3),
h(8|(random()*4)) + s(3),
getTimestamp().toString(16).slice(-10) + s(2)
].join('-');
}
function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
}
if (data.logType === 'no') {
return false;
}
if (data.logType === 'debug') {
return isDebug;
}
return data.logType === 'always';
}
___SERVER_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_request",
"versionId": "1"
},
"param": [
{
"key": "headerWhitelist",
"value": {
"type": 2,
"listItem": [
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "headerName"
}
],
"mapValue": [
{
"type": 1,
"string": "trace-id"
}
]
},
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "headerName"
}
],
"mapValue": [
{
"type": 1,
"string": "referer"
}
]
}
]
}
},
{
"key": "headersAllowed",
"value": {
"type": 8,
"boolean": true
}
},
{
"key": "requestAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "headerAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "queryParameterAccess",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "get_cookies",
"versionId": "1"
},
"param": [
{
"key": "cookieAccess",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "logging",
"versionId": "1"
},
"param": [
{
"key": "environments",
"value": {
"type": 1,
"string": "all"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "read_container_data",
"versionId": "1"
},
"param": []
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "read_event_data",
"versionId": "1"
},
"param": [
{
"key": "eventDataAccess",
"value": {
"type": 1,
"string": "any"
}
}