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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

* Fix bug where tracking UTM params and referrer drops unsent events that have been saved to localStorage.

### 3.3.0 (October 19, 2016)

* Add option to track GCLID (Google Click ID) as a user property (set `includeGclid` to `true` in the SDK configuration).
Expand Down
39 changes: 20 additions & 19 deletions amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,24 +566,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
this.options.userId = (type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
this.options.userId || null;

var now = new Date().getTime();
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.saveParamsReferrerOncePerSession) {
this._trackParamsAndReferrer();
}
}

if (!this.options.saveParamsReferrerOncePerSession) {
this._trackParamsAndReferrer();
}

this._lastEventTime = now;
_saveCookieData(this);

// load unsent events and identifies before any attempt to log new ones
if (this.options.saveEvents) {
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey);
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey);
Expand All @@ -603,9 +586,27 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
this._unsentIdentifys[j].user_properties = utils.validateProperties(userProperties);
this._unsentIdentifys[j].groups = utils.validateGroups(identifyGroups);
}
}

var now = new Date().getTime();
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.saveParamsReferrerOncePerSession) {
this._trackParamsAndReferrer();
}
}

this._sendEventsIfReady(); // try sending unsent events
if (!this.options.saveParamsReferrerOncePerSession) {
this._trackParamsAndReferrer();
}

this._lastEventTime = now;
_saveCookieData(this);

this._sendEventsIfReady(); // try sending unsent events
} catch (e) {
utils.log(e);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion amplitude.min.js

Large diffs are not rendered by default.

39 changes: 20 additions & 19 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
this.options.userId = (type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
this.options.userId || null;

var now = new Date().getTime();
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.saveParamsReferrerOncePerSession) {
this._trackParamsAndReferrer();
}
}

if (!this.options.saveParamsReferrerOncePerSession) {
this._trackParamsAndReferrer();
}

this._lastEventTime = now;
_saveCookieData(this);

// load unsent events and identifies before any attempt to log new ones
if (this.options.saveEvents) {
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey);
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey);
Expand All @@ -122,9 +105,27 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
this._unsentIdentifys[j].user_properties = utils.validateProperties(userProperties);
this._unsentIdentifys[j].groups = utils.validateGroups(identifyGroups);
}
}

var now = new Date().getTime();
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.saveParamsReferrerOncePerSession) {
this._trackParamsAndReferrer();
}
}

this._sendEventsIfReady(); // try sending unsent events
if (!this.options.saveParamsReferrerOncePerSession) {
this._trackParamsAndReferrer();
}

this._lastEventTime = now;
_saveCookieData(this);

this._sendEventsIfReady(); // try sending unsent events
} catch (e) {
utils.log(e);
} finally {
Expand Down
42 changes: 42 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,48 @@ describe('setVersionName', function() {
}
});
});

it('should not delete unsent events saved to localStorage', function() {
var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
'"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
'"event_properties":{},"user_properties":{},"uuid":"3c508faa-a5c9-45fa-9da7-9f4f3b992fb0","library"' +
':{"name":"amplitude-js","version":"2.9.0"},"sequence_number":130, "groups":{}}]';
var existingIdentify = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769338995,' +
'"event_id":82,"session_id":1453763315544,"event_type":"$identify","version_name":"Web","platform":"Web"' +
',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
'"event_properties":{},"user_properties":{"$set":{"age":30,"city":"San Francisco, CA"}},"uuid":"' +
'c50e1be4-7976-436a-aa25-d9ee38951082","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number"' +
':131, "groups":{}}]';
localStorage.setItem('amplitude_unsent', existingEvent);
localStorage.setItem('amplitude_unsent_identify', existingIdentify);

clock.tick(30 * 60 * 1000 + 1); // force new session
amplitude.init(apiKey, undefined, {
includeReferrer: true, batchEvents: true, eventUploadThreshold: 3, saveParamsReferrerOncePerSession: false
});

assert.lengthOf(server.requests, 1);
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
assert.lengthOf(events, 3);

// validate the events
assert.equal(events[0].event_type, 'clicked');
assert.equal(events[1].event_type, '$identify');
assert.equal(events[2].event_type, '$identify');

assert.deepEqual(events[1].user_properties, {'$set': {'age': 30, 'city': 'San Francisco, CA'}});
assert.deepEqual(events[2].user_properties, {
'$set': {
'referrer': 'https://amplitude.com/contact',
'referring_domain': 'amplitude.com'
},
'$setOnce': {
'initial_referrer': 'https://amplitude.com/contact',
'initial_referring_domain': 'amplitude.com'
}
});
});
});

describe('gatherGclid', function() {
Expand Down