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
5 changes: 5 additions & 0 deletions integrations/hotjar/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.4.0 / 2022-02-14
===================

* Added support for Hotjar Events API.

1.3.1 / 2020-12-14
===================

Expand Down
18 changes: 11 additions & 7 deletions integrations/hotjar/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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');
Expand All @@ -77,3 +73,11 @@ 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');
}

window.hj('event', track.event(), track.properties());
};
2 changes: 1 addition & 1 deletion integrations/hotjar/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions integrations/hotjar/test/index.contract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ describe('Hotjar Contract', function() {
var options = {
hjid: 485778,
hjTriggers: {},
hjTagRecordingEvents: [],
hjPlaceholderPolyfill: true
hjTagRecordingEvents: []
};

beforeEach(function() {
Expand Down
49 changes: 37 additions & 12 deletions integrations/hotjar/test/index.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ describe('Hotjar Unit', function() {
var hotjar;
var customOptions;
var options = {
hjid: 485778,
hjPlaceholderPolyfill: true
hjid: 485778
};

beforeEach(function() {
Expand All @@ -28,8 +27,7 @@ describe('Hotjar Unit', function() {
analytics.add(hotjar);

customOptions = {
hjid: 485778,
hjPlaceholderPolyfill: false
hjid: 485778
};
});

Expand All @@ -46,7 +44,6 @@ describe('Hotjar Unit', function() {
Hotjar,
Integration('Hotjar')
.option('hjid', null)
.option('hjPlaceholderPolyfill', true)
);
});

Expand All @@ -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');
});
Expand All @@ -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');
Expand Down Expand Up @@ -124,5 +115,39 @@ 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 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, 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);
});
});
});
});