Skip to content

Commit 8ec2c4e

Browse files
committed
Merge pull request #59 from amplitude/sync_across_windows
reload cookie when logging event
2 parents 003c80d + 2ebb943 commit 8ec2c4e

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
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+
* Keeping sessions and event metadata in sync across multiple windows/tabs.
56

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

amplitude.js

Lines changed: 1 addition & 0 deletions
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');

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: 1 addition & 0 deletions
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');

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)