Skip to content

Commit 2d16001

Browse files
committed
Put utm parameter sending behind an option.
1 parent 1e834f2 commit 2d16001

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

amplitude.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ var DEFAULT_OPTIONS = {
135135
domain: undefined,
136136
sessionTimeout: 30 * 60 * 1000,
137137
platform: 'Web',
138-
language: language.language
138+
language: language.language,
139+
includeUtm: false
139140
};
140141
var LocalStorageKeys = {
141142
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -165,8 +166,7 @@ Amplitude.prototype._newSession = false;
165166
* opt_userId An identifier for this user
166167
* opt_config Configuration options
167168
* - saveEvents (boolean) Whether to save events to local storage. Defaults to true.
168-
* - utmParams (string) Optional utm data in query string format.
169-
* Pulled from location.search otherwise.
169+
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
170170
*/
171171
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
172172
try {
@@ -178,6 +178,9 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
178178
if (opt_config.domain !== undefined) {
179179
this.options.domain = opt_config.domain;
180180
}
181+
if (opt_config.includeUtm !== undefined) {
182+
this.options.includeUtm = !!opt_config.includeUtm;
183+
}
181184
this.options.platform = opt_config.platform || this.options.platform;
182185
this.options.language = opt_config.language || this.options.language;
183186
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
@@ -214,9 +217,9 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
214217
this.sendEvents();
215218
}
216219

217-
// Parse the utm properties out of cookies and query for adding to user properties.
218-
var utmParams = opt_config && opt_config.utmParams || location.search;
219-
this._utmProperties = Amplitude._getUtmData(Cookie.get('__utmz'), utmParams);
220+
if (this.options.includeUtm) {
221+
this._initUtmData();
222+
}
220223

221224
this._lastEventTime = parseInt(localStorage.getItem(LocalStorageKeys.LAST_EVENT_TIME)) || null;
222225
this._sessionId = parseInt(localStorage.getItem(LocalStorageKeys.SESSION_ID)) || null;
@@ -291,6 +294,15 @@ Amplitude._getUtmData = function(rawCookie, query) {
291294
};
292295
};
293296

297+
/**
298+
* Parse the utm properties out of cookies and query for adding to user properties.
299+
*/
300+
Amplitude.prototype._initUtmData = function(queryParams, cookieParams) {
301+
queryParams = queryParams || location.search;
302+
cookieParams = cookieParams || Cookie.get('__utmz');
303+
this._utmProperties = Amplitude._getUtmData(cookieParams, queryParams);
304+
};
305+
294306
Amplitude.prototype.saveEvents = function() {
295307
try {
296308
localStorage.setItem(this.options.unsentKey, JSON.stringify(this._unsentEvents));
@@ -375,7 +387,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) {
375387

376388
// Add the utm properties, if any, onto the user properties.
377389
var userProperties = {};
378-
object.merge(userProperties, this.options.userProperties || {}, this._utmProperties);
390+
object.merge(userProperties, this.options.userProperties || {});
379391
object.merge(userProperties, this._utmProperties);
380392

381393
eventProperties = eventProperties || {};

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ var DEFAULT_OPTIONS = {
2525
domain: undefined,
2626
sessionTimeout: 30 * 60 * 1000,
2727
platform: 'Web',
28-
language: language.language
28+
language: language.language,
29+
includeUtm: false
2930
};
3031
var LocalStorageKeys = {
3132
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -55,8 +56,7 @@ Amplitude.prototype._newSession = false;
5556
* opt_userId An identifier for this user
5657
* opt_config Configuration options
5758
* - saveEvents (boolean) Whether to save events to local storage. Defaults to true.
58-
* - utmParams (string) Optional utm data in query string format.
59-
* Pulled from location.search otherwise.
59+
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
6060
*/
6161
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
6262
try {
@@ -68,6 +68,9 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
6868
if (opt_config.domain !== undefined) {
6969
this.options.domain = opt_config.domain;
7070
}
71+
if (opt_config.includeUtm !== undefined) {
72+
this.options.includeUtm = !!opt_config.includeUtm;
73+
}
7174
this.options.platform = opt_config.platform || this.options.platform;
7275
this.options.language = opt_config.language || this.options.language;
7376
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
@@ -104,9 +107,9 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
104107
this.sendEvents();
105108
}
106109

107-
// Parse the utm properties out of cookies and query for adding to user properties.
108-
var utmParams = opt_config && opt_config.utmParams || location.search;
109-
this._utmProperties = Amplitude._getUtmData(Cookie.get('__utmz'), utmParams);
110+
if (this.options.includeUtm) {
111+
this._initUtmData();
112+
}
110113

111114
this._lastEventTime = parseInt(localStorage.getItem(LocalStorageKeys.LAST_EVENT_TIME)) || null;
112115
this._sessionId = parseInt(localStorage.getItem(LocalStorageKeys.SESSION_ID)) || null;
@@ -181,6 +184,15 @@ Amplitude._getUtmData = function(rawCookie, query) {
181184
};
182185
};
183186

187+
/**
188+
* Parse the utm properties out of cookies and query for adding to user properties.
189+
*/
190+
Amplitude.prototype._initUtmData = function(queryParams, cookieParams) {
191+
queryParams = queryParams || location.search;
192+
cookieParams = cookieParams || Cookie.get('__utmz');
193+
this._utmProperties = Amplitude._getUtmData(cookieParams, queryParams);
194+
};
195+
184196
Amplitude.prototype.saveEvents = function() {
185197
try {
186198
localStorage.setItem(this.options.unsentKey, JSON.stringify(this._unsentEvents));

test/amplitude.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,42 @@ describe('Amplitude', function() {
196196
reset();
197197
});
198198

199-
it('should add utm params to the user properties', function() {
199+
it('should not send utm data when the includeUtm flag is false', function() {
200200
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
201+
reset();
202+
amplitude.init(apiKey, undefined, {});
201203

204+
amplitude.setUserProperties({user_prop: true});
205+
amplitude.logEvent('UTM Test Event', {});
206+
assert.lengthOf(server.requests, 1);
207+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
208+
assert.equal(events[0].user_properties.utm_campaign, undefined);
209+
assert.equal(events[0].user_properties.utm_content, undefined);
210+
assert.equal(events[0].user_properties.utm_medium, undefined);
211+
assert.equal(events[0].user_properties.utm_source, undefined);
212+
assert.equal(events[0].user_properties.utm_term, undefined);
213+
});
214+
215+
it('should send utm data when the includeUtm flag is true', function() {
216+
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
202217
reset();
203-
amplitude.init(apiKey, undefined, {
204-
utmParams: '?utm_source=amplitude&utm_medium=email&utm_term=terms'
218+
amplitude.init(apiKey, undefined, {includeUtm: true});
219+
220+
amplitude.logEvent('UTM Test Event', {});
221+
222+
assert.lengthOf(server.requests, 1);
223+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
224+
assert.deepEqual(events[0].user_properties, {
225+
utm_campaign: 'new',
226+
utm_content: 'top'
205227
});
228+
});
229+
230+
it('should add utm params to the user properties', function() {
231+
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
232+
233+
var utmParams = '?utm_source=amplitude&utm_medium=email&utm_term=terms';
234+
amplitude._initUtmData(utmParams);
206235

207236
amplitude.setUserProperties({user_prop: true});
208237
amplitude.logEvent('UTM Test Event', {});
@@ -217,7 +246,6 @@ describe('Amplitude', function() {
217246
utm_source: 'amplitude',
218247
utm_term: 'terms'
219248
});
220-
221249
});
222250

223251
it('should get utm params from the query string', function() {

0 commit comments

Comments
 (0)