Skip to content

Commit 0a67b23

Browse files
chrisradekjuliomalegriajuliomalegria-hotjararielsilvestri
authored
Added support for Hotjar Events API. (#660) (#677)
* Added support for Hotjar Events API. * Start passing properties() to the Event API * Updated tests to check track with/without properties Co-authored-by: julio.alegria <julio.alegria@hotjar.com> Co-authored-by: Ariel Silvestri <arielsilvestri0715@gmail.com> Co-authored-by: Julio M. Alegria <juliomalegria@users.noreply.github.com> Co-authored-by: julio.alegria <julio.alegria@hotjar.com> Co-authored-by: Ariel Silvestri <arielsilvestri0715@gmail.com>
1 parent 2d65a8e commit 0a67b23

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

integrations/hotjar/HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
1.4.0 / 2022-02-14
2+
===================
3+
4+
* Added support for Hotjar Events API.
5+
16
1.3.1 / 2020-12-14
27
===================
38

integrations/hotjar/lib/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ var is = require('is');
77
* Expose `HotJar` integration
88
*/
99

10-
var Hotjar = (module.exports = integration('Hotjar')
11-
.option('hjid', null)
12-
.option('hjPlaceholderPolyfill', true));
10+
var Hotjar = (module.exports = integration('Hotjar').option('hjid', null));
1311

1412
/**
1513
* Initialize HotJar
@@ -20,8 +18,7 @@ Hotjar.prototype.initialize = function() {
2018
function areOptionsValid(options) {
2119
var validators = {
2220
isHjidValid:
23-
is.number(options.hjid) && !is.nan(options.hjid) && options.hjid !== 0, // Make sure that HJID is a number (and isn't NaN)
24-
isPlaceholderPolyfillValid: is.bool(options.hjPlaceholderPolyfill) // Make sure we received a boolean.
21+
is.number(options.hjid) && !is.nan(options.hjid) && options.hjid !== 0 // Make sure that HJID is a number (and isn't NaN)
2522
};
2623

2724
for (var validator in validators) {
@@ -53,8 +50,7 @@ Hotjar.prototype.initialize = function() {
5350
};
5451
h._hjSettings = {
5552
hjid: h._hjSelf.options.hjid,
56-
hjsv: 6,
57-
hjPlaceholderPolyfill: h._hjSelf.options.hjPlaceholderPolyfill
53+
hjsv: 6
5854
};
5955
a = o.getElementsByTagName('head')[0];
6056
r = o.createElement('script');
@@ -77,3 +73,11 @@ Hotjar.prototype.identify = function(identify) {
7773

7874
window.hj('identify', identify.userId(), traits);
7975
};
76+
77+
Hotjar.prototype.track = function(track) {
78+
if (!track.event()) {
79+
return this.debug('event name is required');
80+
}
81+
82+
window.hj('event', track.event(), track.properties());
83+
};

integrations/hotjar/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-hotjar",
33
"description": "The Hotjar analytics.js integration.",
4-
"version": "1.3.2",
4+
"version": "1.4.0",
55
"keywords": null,
66
"main": "lib/index.js",
77
"scripts": {

integrations/hotjar/test/index.contract.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ describe('Hotjar Contract', function() {
2020
var options = {
2121
hjid: 485778,
2222
hjTriggers: {},
23-
hjTagRecordingEvents: [],
24-
hjPlaceholderPolyfill: true
23+
hjTagRecordingEvents: []
2524
};
2625

2726
beforeEach(function() {

integrations/hotjar/test/index.unit.test.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ describe('Hotjar Unit', function() {
1616
var hotjar;
1717
var customOptions;
1818
var options = {
19-
hjid: 485778,
20-
hjPlaceholderPolyfill: true
19+
hjid: 485778
2120
};
2221

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

3029
customOptions = {
31-
hjid: 485778,
32-
hjPlaceholderPolyfill: false
30+
hjid: 485778
3331
};
3432
});
3533

@@ -46,7 +44,6 @@ describe('Hotjar Unit', function() {
4644
Hotjar,
4745
Integration('Hotjar')
4846
.option('hjid', null)
49-
.option('hjPlaceholderPolyfill', true)
5047
);
5148
});
5249

@@ -68,8 +65,7 @@ describe('Hotjar Unit', function() {
6865
hotjar.initialize();
6966
analytics.deepEqual(window._hjSettings, {
7067
hjid: options.hjid,
71-
hjsv: 6,
72-
hjPlaceholderPolyfill: true
68+
hjsv: 6
7369
});
7470
analytics.assert(typeof window.hj === 'function');
7571
});
@@ -79,11 +75,6 @@ describe('Hotjar Unit', function() {
7975
testInvalidInitialize(customOptions);
8076
});
8177

82-
it('should reject an invalid hjPlaceholderPolyfill boolean', function() {
83-
customOptions.hjPlaceholderPolyfill = 1;
84-
testInvalidInitialize(customOptions);
85-
});
86-
8778
function testInvalidInitialize(invalidOptions) {
8879
hotjar.options = invalidOptions;
8980
analytics.stub(hotjar, 'ready');
@@ -124,5 +115,39 @@ describe('Hotjar Unit', function() {
124115
analytics.didNotCall(window.hj);
125116
});
126117
});
118+
119+
describe('#track', function() {
120+
beforeEach(function() {
121+
analytics.stub(hotjar, 'debug');
122+
analytics.stub(window, 'hj');
123+
});
124+
125+
afterEach(function() {
126+
analytics.reset();
127+
});
128+
129+
it('should send event without properties', function() {
130+
analytics.stub(window, 'hj');
131+
var event = 'the_event';
132+
analytics.track(event);
133+
analytics.called(window.hj, 'event', event);
134+
});
135+
136+
it('should send event with properties', function() {
137+
analytics.stub(window, 'hj');
138+
var event = 'the_event';
139+
var properties = { a: 'a', b: 'b', c: [] };
140+
analytics.track(event, properties);
141+
analytics.called(window.hj, 'event', event, properties);
142+
});
143+
144+
it('should not send nameless event', function() {
145+
var properties = { a: 'a', b: 'b', c: [] };
146+
analytics.track(undefined, properties);
147+
148+
analytics.called(hotjar.debug, 'event name is required');
149+
analytics.didNotCall(window.hj);
150+
});
151+
});
127152
});
128153
});

0 commit comments

Comments
 (0)