Skip to content
This repository was archived by the owner on Nov 23, 2019. It is now read-only.

Commit c6d9155

Browse files
authored
Fixes error that occurs when local storage is enabled. (#62)
* Fixes an error when local storage is disabled. * Added unit test to make sure getCachedCrossDomainId isn’t called if crossDomain is disabled. * Updated test based on feedback.
1 parent dcfb8f0 commit c6d9155

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,14 @@ Segment.prototype.normalize = function(msg) {
275275
msg.writeKey = this.options.apiKey;
276276
ctx.userAgent = navigator.userAgent;
277277
if (!ctx.library) ctx.library = { name: 'analytics.js', version: this.analytics.VERSION };
278-
var crossDomainId = this.getCachedCrossDomainId();
279-
if (crossDomainId && this.isCrossDomainAnalyticsEnabled()) {
280-
if (!ctx.traits) {
281-
ctx.traits = { crossDomainId: crossDomainId };
282-
} else if (!ctx.traits.crossDomainId) {
283-
ctx.traits.crossDomainId = crossDomainId;
278+
if (this.isCrossDomainAnalyticsEnabled()) {
279+
var crossDomainId = this.getCachedCrossDomainId();
280+
if (crossDomainId) {
281+
if (!ctx.traits) {
282+
ctx.traits = { crossDomainId: crossDomainId };
283+
} else if (!ctx.traits.crossDomainId) {
284+
ctx.traits.crossDomainId = crossDomainId;
285+
}
284286
}
285287
}
286288
// if user provides campaign via context, do not overwrite with UTM qs param

test/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,14 @@ describe('Segment.io', function() {
505505
analytics.stub(segment, 'enqueue');
506506
});
507507

508+
it('identify should not ultimately call getCachedCrossDomainId if crossDomainAnalytics is not enabled', function() {
509+
segment.options.crossDomainIdServers = [];
510+
var getCachedCrossDomainIdSpy = sinon.spy(segment, 'getCachedCrossDomainId');
511+
segment.normalize({});
512+
sinon.assert.notCalled(getCachedCrossDomainIdSpy);
513+
segment.getCachedCrossDomainId.restore();
514+
});
515+
508516
it('should enqueue an id and traits', function() {
509517
analytics.identify('id', { trait: true }, { opt: true });
510518
var args = segment.enqueue.args[0];

0 commit comments

Comments
 (0)