-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
208 lines (178 loc) · 5.21 KB
/
index.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
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
'use strict';
var querystring = require('querystring');
var cookie = require('cookie');
var merge = require('deepmerge');
/**
* Module exports.
* @public
*/
module.exports = saveLastCampaign;
/**
* saveLastCampaign
*
* Saves campaign query string parameters into a session cookie so they can be
* retrieved and passed to your marketing automation system when needed.
*
* @param {object} [opts] Options object.
* @param {string} [opts.prefix] Cookie name prefix.
* @param {array} [opts.params] Default parameters.
* @param {array} [opts.extra] Extra parameters.
* @public
*/
function saveLastCampaign(opts) {
var options = {
defaults: true,
prefix: '',
params: [
'utm_campaign',
'utm_source',
'utm_medium',
'utm_term',
'utm_content'
],
data: {},
path: '/',
domain: null,
timeout: 30
};
var pageQueryString = getQueryString();
var data = {};
var cookieOptions = {};
var now = new Date();
var expires;
// Remove default parameters if necessary
if (typeof opts === 'object') {
if (typeof opts.defaults !== 'undefined' && opts.defaults === false) {
options.params = [];
}
}
// Merge opts onto options
if (arguments.length && typeof opts === 'object') {
options = merge(options, opts);
}
var dataKeys = Object.keys(options.data);
// Set default cookie options
cookieOptions = {
domain: options.domain,
path: options.path
};
// Set cookie expiration and advance expiration for existing cookies
if (options.timeout) {
expires = new Date(now.setMinutes(now.getMinutes() + options.timeout));
// querystring param cookies
options.params.forEach(function (key) {
updateExpiration(options.prefix + key, expires, cookieOptions);
});
// data object cookies
if (dataKeys.length !== 0) {
dataKeys.forEach(function (key) {
updateExpiration(options.prefix + key, expires, cookieOptions);
});
}
}
// Parse the query string
if (pageQueryString.length !== 0) {
data = querystring.parse(pageQueryString);
var removed = false;
// Create the cookies
options.params.forEach(function (key) {
if (data[key]) {
// param found in querystring so remove all necessary existing cookies first
if (!removed) {
removeCookies(options, cookieOptions);
removed = true;
}
// Merge expires in to prevent the following error:
// Uncaught TypeError: opt.expires.toUTCString is not a function
setCookie(options.prefix + key, data[key], merge(cookieOptions, {
expires: expires
}));
}
});
}
// Save the data object
if (dataKeys.length !== 0) {
dataKeys.forEach(function (key) {
// Skip undefined, null, or empty values
if (typeof options.data[key] === 'undefined' || options.data[key] === null || options.data[key] === '') {
return;
}
// Create the cookie if it doesn't exist
if (!getCookie(key)) {
setCookie(options.prefix + key, options.data[key], merge(cookieOptions, {
expires: expires
}));
}
});
}
}
/**
* Remove all cookies matching options.params
* @param {Object} options
* @private
*/
function removeCookies(options, cookieOptions) {
options.params.forEach(function (key) {
document.cookie = cookie.serialize(options.prefix + key, '', merge(cookieOptions, {
expires: new Date('Thu, 01 Jan 1970 00:00:00 GMT')
}));
});
}
/**
* Returns the query string without its initial question mark.
*
* E.g. foo=bar&baz=qux
*
* @return {string}
* @private
*/
function getQueryString() {
return window.location.search.substring(1);
}
/**
* Sets a browser cookie given a valid cookie string.
*
* E.g. "foo=bar; httpOnly"
*
* @param {string} name - name of cookie
* @param {string} value - value of cookie
* @param {object} options - object containing cookie options
* @private
*/
function setCookie(name, value, options) {
document.cookie = cookie.serialize(name, value, options);
}
/**
* Returns cookie value
*
* @param {string} name - name of cookie
* @return {string} token
* @private
*/
function getCookie(name) {
if (arguments.length === 0 && typeof opts !== 'string') {
return;
}
var match = document.cookie.match('(?:^|; )' + name + '=([^;]+)');
if (match) {
return match[1];
} else {
return '';
}
}
/**
* Update cookie expiration
*
* @param {string} name - name of cookie
* @param {date} expires - expiration date/time of cookie
* @param {object} options - object containing cookie options
* @private
*/
function updateExpiration(name, expires, options) {
var existingValue = getCookie(name);
if (existingValue) {
setCookie(name, existingValue, merge(options, {
expires: expires
}));
}
}