Skip to content

Commit 9ea307c

Browse files
committed
Explicitly set _initializationDeferred to false upon enabling tracking (when a user explicitly opts into analytics), move deferInitialization check where this._storageSuffix is actually defined (in the try block)
* set initializedDeferred = false in enableTracking() * make sure this._storageSuffix is defined when cookies are being checked * Added tests for sending events after tracking is enabled * Added assertions to ensure that each test starts with a clean slate (no unsentEvents) before enabling tracking
1 parent 91ae64c commit 9ea307c

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/amplitude-client.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
7575
return;
7676
}
7777

78-
var hasExistingCookie = !!this.cookieStorage.get(this.options.cookieName + this._storageSuffix);
79-
if (opt_config && opt_config.deferInitialization && !hasExistingCookie) {
80-
this._deferInitialization(apiKey, opt_userId, opt_config, opt_callback);
81-
return;
82-
}
83-
8478
try {
8579
this.options.apiKey = apiKey;
8680
this._storageSuffix = '_' + apiKey + this._legacyStorageSuffix;
8781

82+
var hasExistingCookie = !!this.cookieStorage.get(this.options.cookieName + this._storageSuffix);
83+
if (opt_config && opt_config.deferInitialization && !hasExistingCookie) {
84+
this._deferInitialization(apiKey, opt_userId, opt_config, opt_callback);
85+
return;
86+
}
87+
8888
_parseConfig(this.options, opt_config);
8989

9090
if (type(this.options.logLevel) === 'string') {
@@ -1619,10 +1619,12 @@ AmplitudeClient.prototype._deferInitialization = function _deferInitialization(a
16191619
/**
16201620
* Enable tracking via logging events and dropping a cookie
16211621
* Intended to be used with the deferInitialization configuration flag
1622+
* This will drop a cookie and reset initialization deferred
16221623
* @public
16231624
*/
16241625
AmplitudeClient.prototype.enableTracking = function enableTracking() {
16251626
// This will call init (which drops the cookie) and will run any pending tasks
1627+
this._initializationDeferred = false;
16261628
_saveCookieData(this);
16271629
this.runQueuedFunctions();
16281630
};

test/amplitude-client.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3369,7 +3369,7 @@ describe('setVersionName', function() {
33693369
});
33703370
});
33713371

3372-
describe('upon to opting into analytics', function () {
3372+
describe('upon opting into analytics', function () {
33733373
it('should drop a cookie', function () {
33743374
amplitude.enableTracking();
33753375
var cookieData = cookie.get(amplitude.options.cookieName + '_' + apiKey);
@@ -3380,12 +3380,28 @@ describe('setVersionName', function() {
33803380
amplitude.logEvent('Event Type 1');
33813381
amplitude.logEvent('Event Type 2');
33823382
amplitude.logEventWithTimestamp('test', null, 2000, null);
3383+
assert.lengthOf(amplitude._unsentEvents, 0, 'should not have any pending events to be sent');
33833384
amplitude.enableTracking();
33843385

3386+
assert.lengthOf(server.requests, 1, 'should have sent a request to Amplitude');
33853387
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
33863388
assert.lengthOf(events, 1, 'should have sent a request to Amplitude');
33873389
assert.lengthOf(amplitude._unsentEvents, 3, 'should have saved the remaining events')
33883390
});
3391+
it('should send new events', function () {
3392+
assert.lengthOf(amplitude._unsentEvents, 0, 'should start with no pending events to be sent');
3393+
amplitude.identify(new Identify().set('prop1', 'value1'));
3394+
amplitude.logEvent('Event Type 1');
3395+
amplitude.logEvent('Event Type 2');
3396+
amplitude.logEventWithTimestamp('test', null, 2000, null);
3397+
assert.lengthOf(amplitude._unsentEvents, 0, 'should not have any pending events to be sent');
3398+
3399+
amplitude.enableTracking();
3400+
assert.lengthOf(amplitude._unsentEvents, 3, 'should have saved the remaining events')
3401+
3402+
amplitude.logEvent('Event Type 3');
3403+
assert.lengthOf(amplitude._unsentEvents, 4, 'should save the new events')
3404+
});
33893405
it('should not continue to deferInitialization if an amplitude cookie exists', function () {
33903406
amplitude.enableTracking();
33913407
amplitude.init(apiKey, null, { cookieExpiration: 365, deferInitialization: true });

0 commit comments

Comments
 (0)