Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions integrations/hubspot/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@ var each = require('@ndhoule/each')
var HubSpot = module.exports = integration('HubSpot')
.assumesPageview()
.global('_hsq')
.global('hbspt')
.option('portalId', null)
.tag('<script id="hs-analytics" src="https://js.hs-analytics.net/analytics/{{ cacheBuster }}/{{ portalId }}.js">')
.option('loadFormsSdk', false)
.tag('lib', '<script id="hs-analytics" src="https://js.hs-analytics.net/analytics/{{ cacheBuster }}/{{ portalId }}.js">')
.tag('forms', '<script src="//js.hsforms.net/forms/shell.js">')

/**
* Initialize.
*
* @api public
*/

HubSpot.prototype.initialize = function () {
window._hsq = []
HubSpot.prototype.initialize = function() {
window._hsq = window._hsq || []
var cacheBuster = Math.ceil(new Date() / 300000) * 300000
this.load({ cacheBuster: cacheBuster }, this.ready)
var shouldLoadLeadForms = this.options.loadFormsSdk
var self = this
this.load('lib', { cacheBuster: cacheBuster }, function() {
if (shouldLoadLeadForms) {
self.load('forms', self.ready)
} else {
self.ready()
}
})
}

/**
Expand All @@ -39,8 +50,13 @@ HubSpot.prototype.initialize = function () {
* @return {boolean}
*/

HubSpot.prototype.loaded = function () {
return !!(window._hsq && window._hsq.push !== Array.prototype.push)
HubSpot.prototype.loaded = function() {
var libLoaded = !!(window._hsq && window._hsq.push !== Array.prototype.push)
if (!this.options.loadFormsSdk) {
return libLoaded
} else {
return libLoaded && !!(window.hbspt && window.hbspt.forms)
}
}

/**
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.1.3",
"version": "2.2.0",
"keywords": [
"analytics.js",
"analytics.js-integration",
Expand Down
27 changes: 22 additions & 5 deletions integrations/hubspot/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('HubSpot', function () {
var analytics
var hubspot
var options = {
portalId: 62515
portalId: 62515,
loadFormsSdk: false
}

beforeEach(function () {
Expand All @@ -21,7 +22,7 @@ describe('HubSpot', function () {
analytics.add(hubspot)
})

afterEach(function () {
afterEach(function() {
analytics.restore()
analytics.reset()
hubspot.reset()
Expand All @@ -32,6 +33,8 @@ describe('HubSpot', function () {
analytics.compare(HubSpot, integration('HubSpot')
.assumesPageview()
.global('_hsq')
.global('hbspt')
.option('loadFormsSdk', false)
.option('portalId', null))
})

Expand All @@ -50,9 +53,23 @@ describe('HubSpot', function () {
})
})

describe('loading', function () {
it('should load', function (done) {
analytics.load(hubspot, done)
describe('loading', function() {
// It's currently very difficult to run multiple tests against the loading behavior of a third-party script.
// Because Karma doesn't clear the full state of the browser between tests, we end up with errors from the Hubspot sdk saying that it is already loaded on the page.
// For now, we are simply testing that the forms sdk and the analytics sdk both load if the loadFormsSdk option is true.
// Ideally we would have a test here as well that ensure the form DOES NOT load if the option is false but limitations with Karma make this very tricky.
// TODO: look into solutions for full browser refreshes between tests to ensure clean loading behavior.
it('should load the analytics lib and the forms lib if that option is true', function(done) {
hubspot.options.loadFormsSdk = true
analytics.load(hubspot, function() {
try {
var formsSdkLoaded = !!(window.hbspt && window.hbspt.forms)
analytics.assert(formsSdkLoaded, 'Expected Hubspot Forms SDK to be loaded on the page')
done()
} catch (e) {
done(e)
}
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion karma/safari.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function setSafari (config) {
sl_safari_9: {
base: 'SauceLabs',
browserName: 'safari',
version: '9.0'
version: '9'
}
}
config.browsers = Object.keys(config.customLaunchers)
Expand Down