Skip to content

Commit 304912a

Browse files
committed
load unsent events before handling utm and referrer
1 parent cd5ee87 commit 304912a

File tree

5 files changed

+85
-39
lines changed

5 files changed

+85
-39
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Unreleased
22

3+
* Fix bug where tracking UTM params and referrer drops unsent events that have been saved to localStorage.
4+
35
### 3.3.0 (October 19, 2016)
46

57
* Add option to track GCLID (Google Click ID) as a user property (set `includeGclid` to `true` in the SDK configuration).

amplitude.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -566,24 +566,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
566566
this.options.userId = (type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
567567
this.options.userId || null;
568568

569-
var now = new Date().getTime();
570-
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
571-
this._newSession = true;
572-
this._sessionId = now;
573-
574-
// only capture UTM params and referrer if new session
575-
if (this.options.saveParamsReferrerOncePerSession) {
576-
this._trackParamsAndReferrer();
577-
}
578-
}
579-
580-
if (!this.options.saveParamsReferrerOncePerSession) {
581-
this._trackParamsAndReferrer();
582-
}
583-
584-
this._lastEventTime = now;
585-
_saveCookieData(this);
586-
569+
// load unsent events and identifies before any attempt to log new ones
587570
if (this.options.saveEvents) {
588571
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey);
589572
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey);
@@ -603,9 +586,27 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
603586
this._unsentIdentifys[j].user_properties = utils.validateProperties(userProperties);
604587
this._unsentIdentifys[j].groups = utils.validateGroups(identifyGroups);
605588
}
589+
}
590+
591+
var now = new Date().getTime();
592+
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
593+
this._newSession = true;
594+
this._sessionId = now;
595+
596+
// only capture UTM params and referrer if new session
597+
if (this.options.saveParamsReferrerOncePerSession) {
598+
this._trackParamsAndReferrer();
599+
}
600+
}
606601

607-
this._sendEventsIfReady(); // try sending unsent events
602+
if (!this.options.saveParamsReferrerOncePerSession) {
603+
this._trackParamsAndReferrer();
608604
}
605+
606+
this._lastEventTime = now;
607+
_saveCookieData(this);
608+
609+
this._sendEventsIfReady(); // try sending unsent events
609610
} catch (e) {
610611
utils.log(e);
611612
} finally {

amplitude.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude-client.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
8585
this.options.userId = (type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
8686
this.options.userId || null;
8787

88-
var now = new Date().getTime();
89-
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
90-
this._newSession = true;
91-
this._sessionId = now;
92-
93-
// only capture UTM params and referrer if new session
94-
if (this.options.saveParamsReferrerOncePerSession) {
95-
this._trackParamsAndReferrer();
96-
}
97-
}
98-
99-
if (!this.options.saveParamsReferrerOncePerSession) {
100-
this._trackParamsAndReferrer();
101-
}
102-
103-
this._lastEventTime = now;
104-
_saveCookieData(this);
105-
88+
// load unsent events and identifies before any attempt to log new ones
10689
if (this.options.saveEvents) {
10790
this._unsentEvents = this._loadSavedUnsentEvents(this.options.unsentKey);
10891
this._unsentIdentifys = this._loadSavedUnsentEvents(this.options.unsentIdentifyKey);
@@ -122,9 +105,27 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
122105
this._unsentIdentifys[j].user_properties = utils.validateProperties(userProperties);
123106
this._unsentIdentifys[j].groups = utils.validateGroups(identifyGroups);
124107
}
108+
}
109+
110+
var now = new Date().getTime();
111+
if (!this._sessionId || !this._lastEventTime || now - this._lastEventTime > this.options.sessionTimeout) {
112+
this._newSession = true;
113+
this._sessionId = now;
114+
115+
// only capture UTM params and referrer if new session
116+
if (this.options.saveParamsReferrerOncePerSession) {
117+
this._trackParamsAndReferrer();
118+
}
119+
}
125120

126-
this._sendEventsIfReady(); // try sending unsent events
121+
if (!this.options.saveParamsReferrerOncePerSession) {
122+
this._trackParamsAndReferrer();
127123
}
124+
125+
this._lastEventTime = now;
126+
_saveCookieData(this);
127+
128+
this._sendEventsIfReady(); // try sending unsent events
128129
} catch (e) {
129130
utils.log(e);
130131
} finally {

test/amplitude-client.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,6 +2369,48 @@ describe('setVersionName', function() {
23692369
}
23702370
});
23712371
});
2372+
2373+
it('should not delete unsent events saved to localStorage', function() {
2374+
var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
2375+
'"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
2376+
',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
2377+
'"event_properties":{},"user_properties":{},"uuid":"3c508faa-a5c9-45fa-9da7-9f4f3b992fb0","library"' +
2378+
':{"name":"amplitude-js","version":"2.9.0"},"sequence_number":130, "groups":{}}]';
2379+
var existingIdentify = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769338995,' +
2380+
'"event_id":82,"session_id":1453763315544,"event_type":"$identify","version_name":"Web","platform":"Web"' +
2381+
',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
2382+
'"event_properties":{},"user_properties":{"$set":{"age":30,"city":"San Francisco, CA"}},"uuid":"' +
2383+
'c50e1be4-7976-436a-aa25-d9ee38951082","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number"' +
2384+
':131, "groups":{}}]';
2385+
localStorage.setItem('amplitude_unsent', existingEvent);
2386+
localStorage.setItem('amplitude_unsent_identify', existingIdentify);
2387+
2388+
clock.tick(30 * 60 * 1000 + 1); // force new session
2389+
amplitude.init(apiKey, undefined, {
2390+
includeReferrer: true, batchEvents: true, eventUploadThreshold: 3, saveParamsReferrerOncePerSession: false
2391+
});
2392+
2393+
assert.lengthOf(server.requests, 1);
2394+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
2395+
assert.lengthOf(events, 3);
2396+
2397+
// validate the events
2398+
assert.equal(events[0].event_type, 'clicked');
2399+
assert.equal(events[1].event_type, '$identify');
2400+
assert.equal(events[2].event_type, '$identify');
2401+
2402+
assert.deepEqual(events[1].user_properties, {'$set': {'age': 30, 'city': 'San Francisco, CA'}});
2403+
assert.deepEqual(events[2].user_properties, {
2404+
'$set': {
2405+
'referrer': 'https://amplitude.com/contact',
2406+
'referring_domain': 'amplitude.com'
2407+
},
2408+
'$setOnce': {
2409+
'initial_referrer': 'https://amplitude.com/contact',
2410+
'initial_referring_domain': 'amplitude.com'
2411+
}
2412+
});
2413+
});
23722414
});
23732415

23742416
describe('gatherGclid', function() {

0 commit comments

Comments
 (0)