@segment/analytics.js-integration-segmentio
Advanced tools
Comparing version 3.2.2 to 3.3.0-beta
@@ -40,2 +40,3 @@ 'use strict'; | ||
.option('apiHost', 'api.segment.io/v1') | ||
.option('crossDomainIdDomains', []) | ||
.option('beacon', false) | ||
@@ -79,5 +80,61 @@ .option('addBundledMetadata', false) | ||
}); | ||
// At this moment we intentionally do not want events to be queued while we retrieve the `crossDomainId` | ||
// so `.ready` will get called right away and we'll try to figure out `crossDomainId` | ||
// separately | ||
if (this.options.crossDomainIdDomains && this.options.crossDomainIdDomains.length > 0) { | ||
this.retrieveCrossDomainId(); | ||
} | ||
}; | ||
/** | ||
* retrieveCrossDomainId. | ||
* | ||
* @api private | ||
*/ | ||
Segment.prototype.retrieveCrossDomainId = function(onCrossDomainIdReady) { | ||
var self = this; | ||
var crossDomainId = this.cookie('segment_cross_domain_id'); | ||
if (!crossDomainId) { | ||
var domains = this.options.crossDomainIdDomains; | ||
var writeKey = this.options.apiKey; | ||
var crossDomainIdFound = false; | ||
var finishedRequests = 0; | ||
for (var i=0; i<domains.length; i++) { | ||
var domain = domains[i]; | ||
var endpoint = 'https://' + domain + '/v1/id/'; | ||
// For v0 we're just gonna grab the first id that came back | ||
getJsonWithAuth(endpoint, writeKey, '', function(err, resJson) { | ||
finishedRequests++; | ||
if (resJson && resJson.id && !crossDomainIdFound) { | ||
crossDomainIdFound = true; | ||
crossDomainId = resJson.id; | ||
self.cookie('segment_cross_domain_id', crossDomainId); | ||
self.analytics.identify({ | ||
crossDomainId: crossDomainId | ||
}); | ||
self.analytics.track('Cross Domain ID Obtained', { | ||
crossDomainId: crossDomainId, | ||
fromDomain: domain, | ||
currentDomain: window.location.hostname | ||
}); | ||
onCrossDomainIdReady(crossDomainId); | ||
} else if (finishedRequests === domains.length) { | ||
// So all requests have finished but we still did not find a crossDomainId, let's just set one now | ||
crossDomainId = uuid(); | ||
self.cookie('segment_cross_domain_id', crossDomainId); | ||
self.analytics.identify({ | ||
crossDomainId: crossDomainId | ||
}); | ||
self.analytics.track('Cross Domain ID Generated', { | ||
crossDomainId: crossDomainId, | ||
currentDomain: window.location.hostname | ||
}); | ||
onCrossDomainIdReady(crossDomainId); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
/** | ||
* Loaded. | ||
@@ -305,1 +362,24 @@ * | ||
function noop() {} | ||
/** | ||
* getJsonWithAuth | ||
* TODO: Add some queuing (multiple requests) and retry? | ||
*/ | ||
function getJsonWithAuth(url, username, password, callback) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url, true); | ||
if (username || password) { | ||
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ':' + password)); | ||
} | ||
xhr.withCredentials = true; | ||
xhr.onreadystatechange = function() { | ||
if (xhr.readyState === XMLHttpRequest.DONE) { | ||
if (xhr.status >= 200 && xhr.status < 300) { | ||
callback(null, json.parse(xhr.responseText)); | ||
} else { | ||
callback(xhr.statusText, null); | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
} |
{ | ||
"name": "@segment/analytics.js-integration-segmentio", | ||
"description": "The Segmentio analytics.js integration.", | ||
"version": "3.2.2", | ||
"version": "3.3.0-beta", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "analytics.js", |
@@ -59,2 +59,4 @@ 'use strict'; | ||
cookie('segment_amp_id', null, { maxage: -1, path: '/' }); | ||
store('segment_cross_domain_id', null); | ||
cookie('segment_cross_domain_id', null, { maxage: -1, path: '/' }); | ||
} | ||
@@ -742,3 +744,71 @@ | ||
}); | ||
describe('#crossDomainId', function() { | ||
var server; | ||
beforeEach(function() { | ||
server = sinon.fakeServer.create(); | ||
segment.options.crossDomainIdDomains = [ | ||
'userdata.example1.com', | ||
'userdata.domain2.com' | ||
]; | ||
analytics.stub(segment, 'onidentify'); | ||
analytics.stub(segment, 'ontrack'); | ||
}); | ||
afterEach(function() { | ||
server.restore(); | ||
}); | ||
it('should obtain crossDomainId', function() { | ||
segment.retrieveCrossDomainId(); | ||
// server.respondWith('GET', 'https://userdata.example1.com/v1/id/', [ | ||
// 200, | ||
// { 'Content-Type': 'application/json' }, | ||
// '{ "id": "xdomain-id-1" }' | ||
// ]); | ||
// server.respondWith('GET', 'https://userdata.domain2.com/v1/id/', [ | ||
// 404, | ||
// { 'Content-Type': 'application/json' }, | ||
// '' | ||
// ]); | ||
// TODO: Make this more deterministic | ||
server.requests[0].respond(200, | ||
{ 'Content-Type': 'application/json' }, | ||
'{ "id": "xdomain-id-1" }' | ||
); | ||
var identify = segment.onidentify.args[0]; | ||
analytics.assert(identify[0].traits().crossDomainId === 'xdomain-id-1'); | ||
var track = segment.ontrack.args[0]; | ||
var properties = track[0].properties(); | ||
analytics.assert(track[0].event() === 'Cross Domain ID Obtained'); | ||
analytics.assert(properties.crossDomainId === 'xdomain-id-1'); | ||
analytics.assert(properties.fromDomain === 'userdata.domain2.com'); | ||
analytics.assert(properties.currentDomain === 'localhost'); | ||
}); | ||
it('should generate crossDomainId', function() { | ||
segment.retrieveCrossDomainId(); | ||
// TODO: Make this more deterministic | ||
server.requests[0].respond(404, { 'Content-Type': 'application/json' }, ''); | ||
server.requests[1].respond(404, { 'Content-Type': 'application/json' }, ''); | ||
var identify = segment.onidentify.args[0]; | ||
var crossDomainId = identify[0].traits().crossDomainId; | ||
analytics.assert(crossDomainId); | ||
var track = segment.ontrack.args[0]; | ||
var properties = track[0].properties(); | ||
analytics.assert(track[0].event() === 'Cross Domain ID Generated'); | ||
analytics.assert(properties.crossDomainId === crossDomainId); | ||
analytics.assert(properties.fromDomain == null); | ||
analytics.assert(properties.currentDomain === 'localhost'); | ||
}); | ||
}); | ||
}); | ||
}); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
50733
1147
1
1