Skip to content

Commit 8e9b533

Browse files
committed
reload cookie when logging event
1 parent 003c80d commit 8e9b533

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Add support for passing callback function to identify.
44
* Add support for prepend user property operation.
5+
* Add support for keeping sessions and event metadata in sync across multiple windows/tabs.
56

67
### 2.9.1 (March 6, 2016)
78

amplitude.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
669669
callback = null;
670670
}
671671

672+
_loadCookieData(this);
672673
if (!eventType || this.options.optOut) {
673674
if (callback) {
674675
callback(0, 'No request sent');
@@ -689,7 +690,6 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
689690
this._sessionId = eventTime;
690691
}
691692
this._lastEventTime = eventTime;
692-
_saveCookieData(this);
693693

694694
userProperties = userProperties || {};
695695
// Only add utm properties to user properties for events
@@ -724,6 +724,8 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
724724
// country: null
725725
};
726726

727+
_saveCookieData(this);
728+
727729
if (eventType === IDENTIFY_EVENT) {
728730
this._unsentIdentifys.push(event);
729731
this._limitEventsQueued(this._unsentIdentifys);

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.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
563563
callback = null;
564564
}
565565

566+
_loadCookieData(this);
566567
if (!eventType || this.options.optOut) {
567568
if (callback) {
568569
callback(0, 'No request sent');
@@ -583,7 +584,6 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
583584
this._sessionId = eventTime;
584585
}
585586
this._lastEventTime = eventTime;
586-
_saveCookieData(this);
587587

588588
userProperties = userProperties || {};
589589
// Only add utm properties to user properties for events
@@ -618,6 +618,8 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
618618
// country: null
619619
};
620620

621+
_saveCookieData(this);
622+
621623
if (eventType === IDENTIFY_EVENT) {
622624
this._unsentIdentifys.push(event);
623625
this._limitEventsQueued(this._unsentIdentifys);

test/amplitude.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,31 @@ describe('Amplitude', function() {
16211621
'nested_object': {'k':'v', 'l':[0,1], 'o':{'k2':'v2', 'l2': ['e2']}}
16221622
});
16231623
});
1624+
1625+
it('should synchronize event data across multiple amplitude instances that share the same cookie', function() {
1626+
// this test fails if logEvent does not reload cookie data every time
1627+
var amplitude1 = new Amplitude();
1628+
amplitude1.init(apiKey, null, {batchEvents: true, eventUploadThreshold: 5});
1629+
var amplitude2 = new Amplitude();
1630+
amplitude2.init(apiKey, null, {batchEvents: true, eventUploadThreshold: 5});
1631+
1632+
amplitude1.logEvent('test1');
1633+
amplitude2.logEvent('test2');
1634+
amplitude1.logEvent('test3');
1635+
amplitude2.logEvent('test4');
1636+
amplitude2.identify(new amplitude2.Identify().set('key', 'value'));
1637+
amplitude1.logEvent('test5');
1638+
1639+
// the event ids should all be sequential since amplitude1 and amplitude2 have synchronized cookies
1640+
var eventId = amplitude1._unsentEvents[0]['event_id'];
1641+
assert.equal(amplitude2._unsentEvents[0]['event_id'], eventId + 1);
1642+
assert.equal(amplitude1._unsentEvents[1]['event_id'], eventId + 2);
1643+
assert.equal(amplitude2._unsentEvents[1]['event_id'], eventId + 3);
1644+
1645+
var sequenceNumber = amplitude1._unsentEvents[0]['sequence_number'];
1646+
assert.equal(amplitude2._unsentIdentifys[0]['sequence_number'], sequenceNumber + 4);
1647+
assert.equal(amplitude1._unsentEvents[2]['sequence_number'], sequenceNumber + 5);
1648+
});
16241649
});
16251650

16261651
describe('optOut', function() {

0 commit comments

Comments
 (0)