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

Commit

Permalink
Fixes error that occurs when local storage is enabled. (#62)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
bsneed authored Aug 8, 2019
1 parent dcfb8f0 commit c6d9155
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,14 @@ Segment.prototype.normalize = function(msg) {
msg.writeKey = this.options.apiKey;
ctx.userAgent = navigator.userAgent;
if (!ctx.library) ctx.library = { name: 'analytics.js', version: this.analytics.VERSION };
var crossDomainId = this.getCachedCrossDomainId();
if (crossDomainId && this.isCrossDomainAnalyticsEnabled()) {
if (!ctx.traits) {
ctx.traits = { crossDomainId: crossDomainId };
} else if (!ctx.traits.crossDomainId) {
ctx.traits.crossDomainId = crossDomainId;
if (this.isCrossDomainAnalyticsEnabled()) {
var crossDomainId = this.getCachedCrossDomainId();
if (crossDomainId) {
if (!ctx.traits) {
ctx.traits = { crossDomainId: crossDomainId };
} else if (!ctx.traits.crossDomainId) {
ctx.traits.crossDomainId = crossDomainId;
}
}
}
// if user provides campaign via context, do not overwrite with UTM qs param
Expand Down
8 changes: 8 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,14 @@ describe('Segment.io', function() {
analytics.stub(segment, 'enqueue');
});

it('identify should not ultimately call getCachedCrossDomainId if crossDomainAnalytics is not enabled', function() {
segment.options.crossDomainIdServers = [];
var getCachedCrossDomainIdSpy = sinon.spy(segment, 'getCachedCrossDomainId');
segment.normalize({});
sinon.assert.notCalled(getCachedCrossDomainIdSpy);
segment.getCachedCrossDomainId.restore();
});

it('should enqueue an id and traits', function() {
analytics.identify('id', { trait: true }, { opt: true });
var args = segment.enqueue.args[0];
Expand Down

0 comments on commit c6d9155

Please sign in to comment.