Skip to content

Commit 56af796

Browse files
committed
Merge pull request #8 from amplitude/utm-flag
Put utm parameter sending behind an option.
2 parents 1e834f2 + 9dc2d5a commit 56af796

File tree

10 files changed

+94
-28
lines changed

10 files changed

+94
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Amplitude-Javascript
99

1010
<script type="text/javascript">
1111
(function(h,a){var f=h.amplitude||{};var b=a.createElement("script");b.type="text/javascript";
12-
b.async=true;b.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.0.3-min.js";
12+
b.async=true;b.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.0.4-min.js";
1313
var g=a.getElementsByTagName("script")[0];g.parentNode.insertBefore(b,g);
1414
f._q=[];function e(i){f[i]=function(){f._q.push([i].concat(Array.prototype.slice.call(arguments,0)))}}
1515
var c=["init","logEvent","setUserId","setUserProperties","setVersionName","setDomain","setDeviceId",

amplitude.js

Lines changed: 20 additions & 8 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 || {};
@@ -2489,7 +2501,7 @@ exports.isEmpty = function(obj){
24892501
};
24902502
}, {}],
24912503
14: [function(require, module, exports) {
2492-
module.exports = '2.0.3';
2504+
module.exports = '2.0.4';
24932505

24942506
}, {}]}, {}, {"1":""})
24952507
);

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.

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"main": "src/index.js",
44
"repo": "amplitude/amplitude-javascript",
55
"description": "Javascript library for Amplitude Analytics",
6-
"version": "2.0.3",
6+
"version": "2.0.4",
77
"keywords": [
88
"analytics",
99
"amplitude"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "amplitude-js",
33
"author": "Amplitude <[email protected]>",
4-
"version": "2.0.3",
4+
"version": "2.0.4",
55
"license": "MIT",
66
"description": "Javascript library for Amplitude Analytics",
77
"keywords": [

scripts/version.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,29 @@ var fs = require('fs');
22
var path = require('path');
33
var package = require('../package');
44
var component = require('../component');
5+
var previous = require('../src/version');
56

67
var version = package.version;
7-
component.version = version;
88

99
var cwd = process.cwd();
1010

11+
function replaceVersion(filepath) {
12+
var filename = path.join(cwd, filepath);
13+
fs.writeFileSync(filename, fs.readFileSync(filename, 'utf-8').replace(previous, version));
14+
console.log('Updated ', filepath);
15+
}
16+
1117
console.log('Updating to version ' + version);
18+
19+
component.version = version;
1220
fs.writeFileSync(path.join(cwd, 'component.json'), JSON.stringify(component, null, 2) + '\n');
1321
console.log('Updated component.json');
1422

15-
fs.writeFileSync(path.join(cwd, 'src', 'version.js'), "module.exports = '" + version + "';\n");
16-
console.log('Updated src/version.js');
23+
var files = [
24+
'README.md',
25+
path.join('src', 'amplitude-snippet.js'),
26+
path.join('src', 'version.js'),
27+
];
28+
files.map(replaceVersion);
29+
30+
console.log('Updated version from', previous, 'to', version);

src/amplitude-snippet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var as = document.createElement('script');
44
as.type = 'text/javascript';
55
as.async = true;
6-
as.src = 'https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.0.1-min.js';
6+
as.src = 'https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.0.4-min.js';
77
var s = document.getElementsByTagName('script')[0];
88
s.parentNode.insertBefore(as, s);
99
amplitude._q = [];

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));

src/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = '2.0.3';
1+
module.exports = '2.0.4';

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)