Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@segment/analytics.js-integration-segmentio

Package Overview
Dependencies
Maintainers
21
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@segment/analytics.js-integration-segmentio - npm Package Compare versions

Comparing version 3.2.2 to 3.3.0-beta

80

lib/index.js

@@ -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();
}

2

package.json
{
"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');
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc