Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
[LIBWEB-926] Add support for Hubspot EU SDK (segmentio#645)
Browse files Browse the repository at this point in the history
Adds option
  • Loading branch information
tobiordobi authored Nov 22, 2021
1 parent 3644ec3 commit 05e07c7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
4 changes: 4 additions & 0 deletions integrations/hubspot/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.2.4 / 2021-11-22
===================
* Add support for HubSpots EU Data Residency with EU script tag.

2.2.2 / 2020-12-14
===================

Expand Down
12 changes: 9 additions & 3 deletions integrations/hubspot/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ var HubSpot = (module.exports = integration('HubSpot')
.global('hbspt')
.option('portalId', null)
.option('loadFormsSdk', false)
.option('enableEuropeanDataCenter', false)
.tag(
'lib',
'global-tag',
'<script id="hs-analytics" src="https://js.hs-analytics.net/analytics/{{ cacheBuster }}/{{ portalId }}.js">'
)
.tag(
'eu-tag',
'<script id="hs-analytics" src="https://js-eu1.hs-analytics.net/analytics/{{ cacheBuster }}/{{ portalId }}.js">'
)
.tag('forms', '<script src="//js.hsforms.net/forms/shell.js">'));

/**
Expand All @@ -37,12 +42,13 @@ HubSpot.prototype.initialize = function() {
var cacheBuster = Math.ceil(new Date() / 300000) * 300000;
var shouldLoadLeadForms = this.options.loadFormsSdk;
var self = this;
var tagName = this.options.enableEuropeanDataCenter ? 'eu-tag' : 'global-tag';
if (shouldLoadLeadForms) {
this.load('forms', function() {
self.load('lib', { cacheBuster: cacheBuster }, self.ready);
self.load(tagName, { cacheBuster: cacheBuster }, self.ready);
});
} else {
this.load('lib', { cacheBuster: cacheBuster }, this.ready);
this.load(tagName, { cacheBuster: cacheBuster }, this.ready);
}
};

Expand Down
2 changes: 1 addition & 1 deletion integrations/hubspot/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@segment/analytics.js-integration-hubspot",
"description": "The Hubspot analytics.js integration.",
"version": "2.2.3",
"version": "2.2.4",
"keywords": [
"analytics.js",
"analytics.js-integration",
Expand Down
50 changes: 49 additions & 1 deletion integrations/hubspot/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('HubSpot', function() {
var hubspot;
var options = {
portalId: 62515,
loadFormsSdk: false
loadFormsSdk: false,
enableEuropeanDataCenter: false
};

beforeEach(function() {
Expand All @@ -38,13 +39,20 @@ describe('HubSpot', function() {
.global('hbspt')
.option('loadFormsSdk', false)
.option('portalId', null)
.option('enableEuropeanDataCenter', false)
);
});

describe('before loading', function() {
beforeEach(function() {
analytics.stub(hubspot, 'load');
});
afterEach(function() {
analytics.restore();
analytics.reset();
hubspot.reset();
sandbox();
});

describe('#initialize', function() {
it('should create window._hsq', function() {
Expand All @@ -53,6 +61,46 @@ describe('HubSpot', function() {
analytics.page();
analytics.assert(window._hsq instanceof Array);
});
it('should call #load', function() {
analytics.initialize();
analytics.called(hubspot.load);
});
it('initializes with global tag', function() {
var hubspot;
var options = {
portalId: 62515,
enableEuropeanDataCenter: false
};

analytics = new Analytics();
hubspot = new HubSpot(options);
analytics.use(HubSpot);
analytics.use(tester);
analytics.add(hubspot);

analytics.stub(hubspot, 'load');

analytics.initialize();
analytics.called(hubspot.load, 'global-tag');
});
it('initializes with eu tag', function() {
var hubspot;
var options = {
portalId: 62515,
enableEuropeanDataCenter: true
};

analytics = new Analytics();
hubspot = new HubSpot(options);
analytics.use(HubSpot);
analytics.use(tester);
analytics.add(hubspot);

analytics.stub(hubspot, 'load');

analytics.initialize();
analytics.called(hubspot.load, 'eu-tag');
});
});
});

Expand Down

0 comments on commit 05e07c7

Please sign in to comment.