From e0a853d586a33adbe9cd9dc22e1d03c30a0f8452 Mon Sep 17 00:00:00 2001 From: "julio.alegria" Date: Tue, 15 Feb 2022 12:33:34 +0100 Subject: [PATCH 1/3] Added support for Hotjar Events API. --- integrations/hotjar/HISTORY.md | 5 +++ integrations/hotjar/lib/index.js | 21 ++++++--- integrations/hotjar/package.json | 2 +- .../hotjar/test/index.contract.test.js | 3 +- integrations/hotjar/test/index.unit.test.js | 43 +++++++++++++------ 5 files changed, 52 insertions(+), 22 deletions(-) diff --git a/integrations/hotjar/HISTORY.md b/integrations/hotjar/HISTORY.md index 5e91b9322..0cc9bb829 100644 --- a/integrations/hotjar/HISTORY.md +++ b/integrations/hotjar/HISTORY.md @@ -1,3 +1,8 @@ +1.4.0 / 2022-02-14 +=================== + + * Added support for Hotjar Events API. + 1.3.1 / 2020-12-14 =================== diff --git a/integrations/hotjar/lib/index.js b/integrations/hotjar/lib/index.js index 10d04a20c..84ec2136b 100644 --- a/integrations/hotjar/lib/index.js +++ b/integrations/hotjar/lib/index.js @@ -7,9 +7,7 @@ var is = require('is'); * Expose `HotJar` integration */ -var Hotjar = (module.exports = integration('Hotjar') - .option('hjid', null) - .option('hjPlaceholderPolyfill', true)); +var Hotjar = (module.exports = integration('Hotjar').option('hjid', null)); /** * Initialize HotJar @@ -20,8 +18,7 @@ Hotjar.prototype.initialize = function() { function areOptionsValid(options) { var validators = { isHjidValid: - is.number(options.hjid) && !is.nan(options.hjid) && options.hjid !== 0, // Make sure that HJID is a number (and isn't NaN) - isPlaceholderPolyfillValid: is.bool(options.hjPlaceholderPolyfill) // Make sure we received a boolean. + is.number(options.hjid) && !is.nan(options.hjid) && options.hjid !== 0 // Make sure that HJID is a number (and isn't NaN) }; for (var validator in validators) { @@ -53,8 +50,7 @@ Hotjar.prototype.initialize = function() { }; h._hjSettings = { hjid: h._hjSelf.options.hjid, - hjsv: 6, - hjPlaceholderPolyfill: h._hjSelf.options.hjPlaceholderPolyfill + hjsv: 6 }; a = o.getElementsByTagName('head')[0]; r = o.createElement('script'); @@ -77,3 +73,14 @@ Hotjar.prototype.identify = function(identify) { window.hj('identify', identify.userId(), traits); }; + +Hotjar.prototype.track = function(track) { + if (!track.event()) { + return this.debug('event name is required'); + } + + // NOTE: Currently, Hotjar's Events API doesn't support properties. + // For now, we're ignoring properties (`track.properties()`). + + window.hj('event', track.event()); +}; diff --git a/integrations/hotjar/package.json b/integrations/hotjar/package.json index 43df7f41b..54daa5bb2 100644 --- a/integrations/hotjar/package.json +++ b/integrations/hotjar/package.json @@ -1,7 +1,7 @@ { "name": "@segment/analytics.js-integration-hotjar", "description": "The Hotjar analytics.js integration.", - "version": "1.3.2", + "version": "1.4.0", "keywords": null, "main": "lib/index.js", "scripts": { diff --git a/integrations/hotjar/test/index.contract.test.js b/integrations/hotjar/test/index.contract.test.js index e66dc1b9e..402b972c5 100644 --- a/integrations/hotjar/test/index.contract.test.js +++ b/integrations/hotjar/test/index.contract.test.js @@ -20,8 +20,7 @@ describe('Hotjar Contract', function() { var options = { hjid: 485778, hjTriggers: {}, - hjTagRecordingEvents: [], - hjPlaceholderPolyfill: true + hjTagRecordingEvents: [] }; beforeEach(function() { diff --git a/integrations/hotjar/test/index.unit.test.js b/integrations/hotjar/test/index.unit.test.js index 27b131ab0..fb30e4945 100644 --- a/integrations/hotjar/test/index.unit.test.js +++ b/integrations/hotjar/test/index.unit.test.js @@ -16,8 +16,7 @@ describe('Hotjar Unit', function() { var hotjar; var customOptions; var options = { - hjid: 485778, - hjPlaceholderPolyfill: true + hjid: 485778 }; beforeEach(function() { @@ -28,8 +27,7 @@ describe('Hotjar Unit', function() { analytics.add(hotjar); customOptions = { - hjid: 485778, - hjPlaceholderPolyfill: false + hjid: 485778 }; }); @@ -46,7 +44,6 @@ describe('Hotjar Unit', function() { Hotjar, Integration('Hotjar') .option('hjid', null) - .option('hjPlaceholderPolyfill', true) ); }); @@ -68,8 +65,7 @@ describe('Hotjar Unit', function() { hotjar.initialize(); analytics.deepEqual(window._hjSettings, { hjid: options.hjid, - hjsv: 6, - hjPlaceholderPolyfill: true + hjsv: 6 }); analytics.assert(typeof window.hj === 'function'); }); @@ -79,11 +75,6 @@ describe('Hotjar Unit', function() { testInvalidInitialize(customOptions); }); - it('should reject an invalid hjPlaceholderPolyfill boolean', function() { - customOptions.hjPlaceholderPolyfill = 1; - testInvalidInitialize(customOptions); - }); - function testInvalidInitialize(invalidOptions) { hotjar.options = invalidOptions; analytics.stub(hotjar, 'ready'); @@ -124,5 +115,33 @@ describe('Hotjar Unit', function() { analytics.didNotCall(window.hj); }); }); + + describe('#track', function() { + beforeEach(function() { + analytics.stub(hotjar, 'debug'); + analytics.stub(window, 'hj'); + }); + + afterEach(function() { + analytics.reset(); + }); + + it('should send event and ignore properties', function() { + analytics.stub(window, 'hj'); + var event = 'the_event'; + var properties = { a: 'a', b: 'b', c: [] }; + analytics.track(event, properties); + analytics.called(window.hj, 'event', event); + analytics.didNotCall(window.hj, 'event', event, properties); + }); + + it('should not send nameless event', function() { + var properties = { a: 'a', b: 'b', c: [] }; + analytics.track(undefined, properties); + + analytics.called(hotjar.debug, 'event name is required'); + analytics.didNotCall(window.hj); + }); + }); }); }); From f348c29b53e97fecebd7809d0a7a070120d40f03 Mon Sep 17 00:00:00 2001 From: "Julio M. Alegria" Date: Tue, 3 May 2022 13:24:27 +0200 Subject: [PATCH 2/3] Start passing properties() to the Event API --- integrations/hotjar/lib/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/integrations/hotjar/lib/index.js b/integrations/hotjar/lib/index.js index 84ec2136b..a6a93d7eb 100644 --- a/integrations/hotjar/lib/index.js +++ b/integrations/hotjar/lib/index.js @@ -79,8 +79,5 @@ Hotjar.prototype.track = function(track) { return this.debug('event name is required'); } - // NOTE: Currently, Hotjar's Events API doesn't support properties. - // For now, we're ignoring properties (`track.properties()`). - - window.hj('event', track.event()); + window.hj('event', track.event(), track.properties()); }; From e2fb5a520ec88b631ddc672fe761579bed8e02fd Mon Sep 17 00:00:00 2001 From: "Julio M. Alegria" Date: Tue, 3 May 2022 13:27:07 +0200 Subject: [PATCH 3/3] Updated tests to check track with/without properties --- integrations/hotjar/test/index.unit.test.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/integrations/hotjar/test/index.unit.test.js b/integrations/hotjar/test/index.unit.test.js index fb30e4945..be959178d 100644 --- a/integrations/hotjar/test/index.unit.test.js +++ b/integrations/hotjar/test/index.unit.test.js @@ -126,13 +126,19 @@ describe('Hotjar Unit', function() { analytics.reset(); }); - it('should send event and ignore properties', function() { + it('should send event without properties', function() { + analytics.stub(window, 'hj'); + var event = 'the_event'; + analytics.track(event); + analytics.called(window.hj, 'event', event); + }); + + it('should send event with properties', function() { analytics.stub(window, 'hj'); var event = 'the_event'; var properties = { a: 'a', b: 'b', c: [] }; analytics.track(event, properties); - analytics.called(window.hj, 'event', event); - analytics.didNotCall(window.hj, 'event', event, properties); + analytics.called(window.hj, 'event', event, properties); }); it('should not send nameless event', function() {