Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

* Update README with link to our [Google Tag Manager integration demo app](https://github.com/amplitude/GTM-Web-Demo).
* Fix bug where referrer and UTM params were being captured more than once per session.

### 3.0.0 (May 27, 2016)

Expand Down
16 changes: 8 additions & 8 deletions amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,14 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
this._newSession = true;
this._sessionId = now;

// only capture UTM params and referrer if new session
if (this.options.includeUtm) {
this._initUtmData();
}
if (this.options.includeReferrer) {
this._saveReferrer(this._getReferrer());
}
}
this._lastEventTime = now;
_saveCookieData(this);
Expand All @@ -594,14 +602,6 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o

this._sendEventsIfReady(); // try sending unsent events
}

if (this.options.includeUtm) {
this._initUtmData();
}

if (this.options.includeReferrer) {
this._saveReferrer(this._getReferrer());
}
} catch (e) {
utils.log(e);
} finally {
Expand Down
4 changes: 2 additions & 2 deletions amplitude.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion documentation/Amplitude.html
Original file line number Diff line number Diff line change
Expand Up @@ -3137,7 +3137,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion documentation/AmplitudeClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -2791,7 +2791,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion documentation/Identify.html
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion documentation/Revenue.html
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
18 changes: 9 additions & 9 deletions documentation/amplitude-client.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ <h1 class="page-title">Source: amplitude-client.js</h1>
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
this._newSession = true;
this._sessionId = now;

// only capture UTM params and referrer if new session
if (this.options.includeUtm) {
this._initUtmData();
}
if (this.options.includeReferrer) {
this._saveReferrer(this._getReferrer());
}
}
this._lastEventTime = now;
_saveCookieData(this);
Expand All @@ -141,14 +149,6 @@ <h1 class="page-title">Source: amplitude-client.js</h1>

this._sendEventsIfReady(); // try sending unsent events
}

if (this.options.includeUtm) {
this._initUtmData();
}

if (this.options.includeReferrer) {
this._saveReferrer(this._getReferrer());
}
} catch (e) {
utils.log(e);
} finally {
Expand Down Expand Up @@ -1170,7 +1170,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion documentation/amplitude.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion documentation/identify.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion documentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion documentation/revenue.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri May 27 2016 15:00:10 GMT-0700 (PDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 22 2016 14:19:08 GMT-0700 (PDT)
</footer>

<script> prettyPrint(); </script>
Expand Down
16 changes: 8 additions & 8 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
this._newSession = true;
this._sessionId = now;

// only capture UTM params and referrer if new session
if (this.options.includeUtm) {
this._initUtmData();
}
if (this.options.includeReferrer) {
this._saveReferrer(this._getReferrer());
}
}
this._lastEventTime = now;
_saveCookieData(this);
Expand All @@ -113,14 +121,6 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o

this._sendEventsIfReady(); // try sending unsent events
}

if (this.options.includeUtm) {
this._initUtmData();
}

if (this.options.includeReferrer) {
this._saveReferrer(this._getReferrer());
}
} catch (e) {
utils.log(e);
} finally {
Expand Down
95 changes: 91 additions & 4 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2054,17 +2054,21 @@ describe('setVersionName', function() {
});

describe('gatherUtm', function() {
var clock;
beforeEach(function() {
clock = sinon.useFakeTimers(100);
amplitude.init(apiKey);
});

afterEach(function() {
reset();
clock.restore();
});

it('should not send utm data when the includeUtm flag is false', function() {
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
reset();
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
clock.tick(30 * 60 * 1000 + 1);
amplitude.init(apiKey, undefined, {});

amplitude.setUserProperties({user_prop: true});
Expand All @@ -2078,8 +2082,9 @@ describe('setVersionName', function() {
});

it('should send utm data via identify when the includeUtm flag is true', function() {
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
reset();
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
clock.tick(30 * 60 * 1000 + 1);
amplitude.init(apiKey, undefined, {includeUtm: true, batchEvents: true, eventUploadThreshold: 2});

amplitude.logEvent('UTM Test Event', {});
Expand All @@ -2103,9 +2108,10 @@ describe('setVersionName', function() {
});

it('should parse utm params', function() {
reset();
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');

var utmParams = '?utm_source=amplitude&utm_medium=email&utm_term=terms';
clock.tick(30 * 60 * 1000 + 1);
amplitude._initUtmData(utmParams);

var expectedProperties = {
Expand Down Expand Up @@ -2154,6 +2160,7 @@ describe('setVersionName', function() {

cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
var utmParams = '?utm_source=amplitude&utm_medium=email&utm_term=terms';
clock.tick(30 * 60 * 1000 + 1);
amplitude._initUtmData(utmParams);

assert.lengthOf(server.requests, 1);
Expand All @@ -2175,20 +2182,57 @@ describe('setVersionName', function() {
// should not override any existing utm properties values in session storage
assert.equal(sessionStorage.getItem('amplitude_utm_properties'), JSON.stringify(existingProperties));
});

it('should only send utm data once per session', function() {
reset();
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
amplitude.init(apiKey, undefined, {includeUtm: true});

// still same session, should not send any identify events
assert.lengthOf(server.requests, 0);
assert.lengthOf(amplitude._unsentEvents, 0);
assert.lengthOf(amplitude._unsentIdentifys, 0);

// advance clock to force new session
clock.tick(30 * 60 * 1000 + 1);
amplitude.init(apiKey, undefined, {includeUtm: true, batchEvents: true, eventUploadThreshold: 2});
amplitude.logEvent('UTM Test Event', {});

assert.lengthOf(server.requests, 1);
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
assert.equal(events[0].event_type, '$identify');
assert.deepEqual(events[0].user_properties, {
'$setOnce': {
initial_utm_campaign: 'new',
initial_utm_content: 'top'
},
'$set': {
utm_campaign: 'new',
utm_content: 'top'
}
});

assert.equal(events[1].event_type, 'UTM Test Event');
assert.deepEqual(events[1].user_properties, {});
});
});

describe('gatherReferrer', function() {
var clock;
beforeEach(function() {
clock = sinon.useFakeTimers(100);
amplitude.init(apiKey);
sinon.stub(amplitude, '_getReferrer').returns('https://amplitude.com/contact');
});

afterEach(function() {
amplitude._getReferrer.restore();
clock.restore();
reset();
});

it('should not send referrer data when the includeReferrer flag is false', function() {
clock.tick(30 * 60 * 1000 + 1);
amplitude.init(apiKey, undefined, {});

amplitude.setUserProperties({user_prop: true});
Expand All @@ -2200,6 +2244,7 @@ describe('setVersionName', function() {

it('should only send referrer via identify call when the includeReferrer flag is true', function() {
reset();
clock.tick(30 * 60 * 1000 + 1);
amplitude.init(apiKey, undefined, {includeReferrer: true, batchEvents: true, eventUploadThreshold: 2});
amplitude.logEvent('Referrer Test Event', {});
assert.lengthOf(server.requests, 1);
Expand Down Expand Up @@ -2231,6 +2276,7 @@ describe('setVersionName', function() {

it('should not set referrer if referrer data already in session storage', function() {
reset();
clock.tick(30 * 60 * 1000 + 1);
sessionStorage.setItem('amplitude_referrer', 'https://www.google.com/search?');
amplitude.init(apiKey, undefined, {includeReferrer: true, batchEvents: true, eventUploadThreshold: 2});
amplitude.logEvent('Referrer Test Event', {});
Expand All @@ -2254,6 +2300,7 @@ describe('setVersionName', function() {

it('should not override any existing initial referrer values in session storage', function() {
reset();
clock.tick(30 * 60 * 1000 + 1);
sessionStorage.setItem('amplitude_referrer', 'https://www.google.com/search?');
amplitude.init(apiKey, undefined, {includeReferrer: true, batchEvents: true, eventUploadThreshold: 3});
amplitude._saveReferrer('https://facebook.com/contact');
Expand Down Expand Up @@ -2327,6 +2374,46 @@ describe('setVersionName', function() {
// existing value persists
assert.equal(sessionStorage.getItem('amplitude_referrer_new_app'), 'https://www.google.com/search?');
});

it('should only send referrer once per session', function() {
reset();
amplitude.init(apiKey, undefined, {includeReferrer: true});

// still same session, no referrer sent
assert.lengthOf(server.requests, 0);
assert.lengthOf(amplitude._unsentEvents, 0);
assert.lengthOf(amplitude._unsentIdentifys, 0);

// advance the clock to force a new session
clock.tick(30 * 60 * 1000 + 1);
amplitude.init(apiKey, undefined, {includeReferrer: true, batchEvents: true, eventUploadThreshold: 2});
amplitude.logEvent('Referrer Test Event', {});
assert.lengthOf(server.requests, 1);
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
assert.lengthOf(events, 2);

var expected = {
'referrer': 'https://amplitude.com/contact',
'referring_domain': 'amplitude.com'
};

// first event should be identify with initial_referrer and referrer
assert.equal(events[0].event_type, '$identify');
assert.deepEqual(events[0].user_properties, {
'$set': expected,
'$setOnce': {
'initial_referrer': 'https://amplitude.com/contact',
'initial_referring_domain': 'amplitude.com'
}
});

// second event should be the test event with no referrer information
assert.equal(events[1].event_type, 'Referrer Test Event');
assert.deepEqual(events[1].user_properties, {});

// referrer should be propagated to session storage
assert.equal(sessionStorage.getItem('amplitude_referrer'), JSON.stringify(expected));
});
});

describe('logRevenue', function() {
Expand Down Expand Up @@ -2502,4 +2589,4 @@ describe('setVersionName', function() {
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
});
});
});
});
Loading