-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate.js
148 lines (114 loc) · 5.27 KB
/
template.js
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
const getEventData = require('getEventData');
const getCookieValues = require('getCookieValues');
const setCookie = require('setCookie');
const parseUrl = require('parseUrl');
const makeString = require('makeString');
const parsedUrl = parseUrl(data.urlSource === 'page_location_default' ? getEventData('page_location') : data.urlSource);
if (!parsedUrl) {
data.gtmOnFailure();
return;
}
let channelFlow = getChannelFlow();
if (!channelFlow) {
data.gtmOnFailure();
return;
}
const cookieOptions = {
domain: 'auto',
path: '/',
samesite: 'Lax',
secure: true,
httpOnly: false
};
if (data.cookieTime > 0) cookieOptions['max-age'] = data.cookieTime;
setCookie('channel_flow', channelFlow, cookieOptions, true);
setCookie('channel_flow_first', getFirstChannelFromChannelFlow(channelFlow), cookieOptions, true);
setCookie('channel_flow_last', getLastChannelFromChannelFlow(channelFlow), cookieOptions, true);
if (data.storeFirstUTM) storeFirstUTM();
if (data.storeLastUTM) storeLastUTM();
data.gtmOnSuccess();
function getChannelFlow() {
const channelFlowCookie = getCookieValues('channel_flow')[0];
const currentChannel = getCurrentChannel();
if (!channelFlowCookie) {
return currentChannel;
}
if (currentChannel === 'direct/none' || currentChannel === '') {
return channelFlowCookie;
}
const channelFlowLastCookie = getCookieValues('channel_flow_last')[0];
if (currentChannel === channelFlowLastCookie) {
return channelFlowCookie;
}
return channelFlowCookie + ',' + currentChannel;
}
function getCurrentChannel() {
const utmSource = parsedUrl.searchParams.utm_source;
const utmMedium = parsedUrl.searchParams.utm_medium;
if (utmSource || utmMedium) {
return (utmSource ? utmSource : 'direct') + '/' + (utmMedium ? utmMedium : 'none');
}
const parsedReferrer = parseUrl(data.referrerSource === 'page_referrer_default' ? getEventData('page_referrer') : data.referrerSource);
if (!parsedReferrer) {
return 'direct/none';
}
const referrerHostname = parsedReferrer.hostname;
if (referrerHostname && data.ignoreReferrerExpression && referrerHostname.match(data.ignoreReferrerExpression)) {
return '';
}
if (referrerHostname === parsedUrl.hostname) {
return 'direct/none';
}
if (referrerHostname && data.searchEngineExpression && referrerHostname.match(data.searchEngineExpression)) {
const referrerHostnameParts = referrerHostname.replace('www.', '').split('.');
return referrerHostnameParts[0] + '/organic';
}
if (referrerHostname && data.excludeReferrerExpression && referrerHostname.match(data.excludeReferrerExpression)) {
return 'direct/none';
}
return referrerHostname + '/referral';
}
function getLastChannelFromChannelFlow(channelFlow) {
let channels = channelFlow.split(',');
return channels[channels.length - 1];
}
function getFirstChannelFromChannelFlow(channelFlow) {
let channels = channelFlow.split(',');
return channels[0];
}
function storeFirstUTM() {
if (
!getCookieValues('utm_source_first')[0]
&& !getCookieValues('utm_medium_first')[0]
&& !getCookieValues('utm_campaign_first')[0]
&& !getCookieValues('utm_content_first')[0] && !getCookieValues('utm_term_first')[0]
&& (
parsedUrl.searchParams.utm_source
|| parsedUrl.searchParams.utm_medium
|| parsedUrl.searchParams.utm_campaign
|| parsedUrl.searchParams.utm_content
|| parsedUrl.searchParams.utm_term
)
) {
setCookie('utm_source_first', parsedUrl.searchParams.utm_source ? makeString(parsedUrl.searchParams.utm_source) : '', cookieOptions, true);
setCookie('utm_medium_first', parsedUrl.searchParams.utm_medium ? makeString(parsedUrl.searchParams.utm_medium) : '', cookieOptions, true);
setCookie('utm_campaign_first', parsedUrl.searchParams.utm_campaign ? makeString(parsedUrl.searchParams.utm_campaign) : '', cookieOptions, true);
setCookie('utm_content_first', parsedUrl.searchParams.utm_content ? makeString(parsedUrl.searchParams.utm_content) : '', cookieOptions, true);
setCookie('utm_term_first', parsedUrl.searchParams.utm_term ? makeString(parsedUrl.searchParams.utm_term) : '', cookieOptions, true);
}
}
function storeLastUTM() {
if (
parsedUrl.searchParams.utm_source
|| parsedUrl.searchParams.utm_medium
|| parsedUrl.searchParams.utm_campaign
|| parsedUrl.searchParams.utm_content
|| parsedUrl.searchParams.utm_term
) {
setCookie('utm_source', parsedUrl.searchParams.utm_source ? makeString(parsedUrl.searchParams.utm_source) : '', cookieOptions, true);
setCookie('utm_medium', parsedUrl.searchParams.utm_medium ? makeString(parsedUrl.searchParams.utm_medium) : '', cookieOptions, true);
setCookie('utm_campaign', parsedUrl.searchParams.utm_campaign ? makeString(parsedUrl.searchParams.utm_campaign) : '', cookieOptions, true);
setCookie('utm_content', parsedUrl.searchParams.utm_content ? makeString(parsedUrl.searchParams.utm_content) : '', cookieOptions, true);
setCookie('utm_term', parsedUrl.searchParams.utm_term ? makeString(parsedUrl.searchParams.utm_term) : '', cookieOptions, true);
}
}