-
Notifications
You must be signed in to change notification settings - Fork 143
/
index.js
173 lines (149 loc) · 3.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
/**
* Module dependencies.
*/
var Identify = require('segmentio-facade').Identify;
var convert = require('@segment/convert-dates');
var integration = require('@segment/analytics.js-integration');
var push = require('global-queue')('_hsq');
var each = require('@ndhoule/each');
/**
* Expose `HubSpot` integration.
*/
var HubSpot = (module.exports = integration('HubSpot')
.assumesPageview()
.global('_hsq')
.global('hbspt')
.option('portalId', null)
.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 = window._hsq || [];
var cacheBuster = Math.ceil(new Date() / 300000) * 300000;
var shouldLoadLeadForms = this.options.loadFormsSdk;
var self = this;
if (shouldLoadLeadForms) {
this.load('forms', function() {
self.load('lib', { cacheBuster: cacheBuster }, self.ready);
});
} else {
this.load('lib', { cacheBuster: cacheBuster }, this.ready);
}
};
/**
* Loaded?
*
* @api private
* @return {boolean}
*/
HubSpot.prototype.loaded = function() {
var libLoaded = !!(window._hsq && window._hsq.push !== Array.prototype.push);
// Due to limitations with our testing framework, we cannot test more than a single script loading configuration.
// Therefore, we are ignoring this conditional in code coverage.
/* istanbul ignore if */
if (!this.options.loadFormsSdk) {
return libLoaded;
}
return libLoaded && !!(window.hbspt && window.hbspt.forms);
};
/**
* Page.
*
* @api public
* @param {Page} page
*/
HubSpot.prototype.page = function() {
push('trackPageView');
};
/**
* Identify.
*
* @api public
* @param {Identify} identify
*/
HubSpot.prototype.identify = function(identify) {
// use newer version of Identify to have access to `companyName`
var newIdentify = new Identify({
traits: identify.traits(),
userId: identify.userId()
});
if (!newIdentify.email()) {
return;
}
var traits = newIdentify.traits({
firstName: 'firstname',
lastName: 'lastname'
});
traits = convertDates(traits);
traits = formatTraits(traits);
if (newIdentify.companyName() !== undefined) {
traits.company = newIdentify.companyName();
}
push('identify', traits);
};
/**
* Track.
*
* @api public
* @param {Track} track
*/
HubSpot.prototype.track = function(track) {
// Hubspot expects properties.id to be the name of the .track() event
// Ref: http://developers.hubspot.com/docs/methods/enterprise_events/javascript_api
var props = convertDates(track.properties({ id: '_id', revenue: 'value' }));
props.id = track.event();
push('trackEvent', track.event(), props);
};
/**
* Convert all the dates in the HubSpot properties to millisecond times
*
* @api private
* @param {Object} properties
*/
function convertDates(properties) {
return convert(properties, function(date) {
return date.getTime();
});
}
/**
* lowercase & snakecase any trait with uppercase letters or spaces
* Hubspot cannot accept uppercases or spaces
*
* @api private
* @param {Object} traits
* @return {Object} ret
*/
function formatTraits(traits) {
var ret = {};
each(function(value, key) {
// Using split/join due to IE 11 failing to properly support regex in str.replace()
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@replace
var k = key
.toLowerCase()
.split(' ')
.join('_') // spaces
.split('.')
.join('_') // Periods
.split('\n')
.join('_') // new lines
.split('\v')
.join('_') // Vertical tabs
.split('\t')
.join('_') // Regular tabs
.split('\f')
.join('_') // form feeds
.split('\r')
.join('_'); // Carriage returns
ret[k] = value;
}, traits);
return ret;
}