-
Notifications
You must be signed in to change notification settings - Fork 143
/
index.js
243 lines (203 loc) · 5.35 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
/* global JSON */
/* eslint no-restricted-globals: [0] */
/**
* Module dependencies.
*/
var integration = require('@segment/analytics.js-integration');
var each = require('component-each');
var is = require('is');
var extend = require('@ndhoule/extend');
var toISOString = require('@segment/to-iso-string');
var toString = Object.prototype.toString; // in case this method has been overridden by the user
/**
* Expose `Heap` integration.
*/
var Heap = (module.exports = integration('Heap')
.global('heap')
.option('appId', '')
.tag('<script src="//cdn.heapanalytics.com/js/heap-{{ appId }}.js">'));
/**
* Initialize.
*
* https://heapanalytics.com/docs/installation#web
*
* @api public
*/
Heap.prototype.initialize = function() {
window.heap = window.heap || [];
window.heap.load = function(appid, config) {
window.heap.appid = appid;
window.heap.config = config;
var methodFactory = function(type) {
return function() {
window.heap.push(
[type].concat(Array.prototype.slice.call(arguments, 0))
);
};
};
var heapMethods = [
'addEventProperties',
'addUserProperties',
'clearEventProperties',
'identify',
'removeEventProperty',
'setEventProperties',
'track',
'unsetEventProperty',
'resetIdentity'
];
each(heapMethods, function(method) {
window.heap[method] = methodFactory(method);
});
};
window.heap.load(this.options.appId);
this.load(this.ready);
};
/**
* Loaded?
*
* @api private
* @return {boolean}
*/
Heap.prototype.loaded = function() {
return !!(window.heap && window.heap.appid);
};
/**
* Identify.
*
* https://heapanalytics.com/docs#identify
*
* @api public
* @param {Identify} identify
*/
Heap.prototype.identify = function(identify) {
var traits = identify.traits({ email: '_email' });
var id = identify.userId();
if (id) window.heap.identify(id);
window.heap.addUserProperties(clean(traits));
};
/**
* Track.
*
* https://heapanalytics.com/docs#track
*
* @api public
* @param {Track} track
*/
Heap.prototype.track = function(track) {
window.heap.track(track.event(), clean(track.properties()));
};
/**
* Clean all nested objects and arrays.
*
* @param {Object} obj
* @return {Object}
* @api private
*/
function clean(obj) {
var ret = {};
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
var value = obj[k];
// Heap's natively library will drop null and undefined properties anyway
// so no need to send these
// also prevents uncaught errors since we call .toString() on non objects
if (value === null || value === undefined) continue;
// date
if (is.date(value)) {
ret[k] = toISOString(value);
continue;
}
// leave boolean as is
if (is.bool(value)) {
ret[k] = value;
continue;
}
// leave numbers as is
if (is.number(value)) {
ret[k] = value;
continue;
}
// arrays of objects (eg. `products` array)
if (toString.call(value) === '[object Array]') {
ret = extend(ret, trample(k, value));
continue;
}
// non objects
if (toString.call(value) !== '[object Object]') {
ret[k] = value.toString();
continue;
}
ret = extend(ret, trample(k, value));
}
}
// json
// must flatten including the name of the original trait/property
function trample(key, value) {
var nestedObj = {};
nestedObj[key] = value;
var flattenedObj = flatten(nestedObj, { safe: true });
// stringify arrays inside nested object to be consistent with top level behavior of arrays
for (var k in flattenedObj) {
if (is.array(flattenedObj[k]))
flattenedObj[k] = JSON.stringify(flattenedObj[k]);
}
return flattenedObj;
}
return ret;
}
/**
* Flatten nested objects
* taken from https://www.npmjs.com/package/flat
* @param {Object} obj
* @return {Object} obj
* @api public
*/
function flatten(target, opts) {
var options = opts || {};
var delimiter = options.delimiter || '.';
var maxDepth = options.maxDepth;
var currentDepth = 1;
var output = {};
function step(object, prev) {
Object.keys(object).forEach(function(key) {
var value = object[key];
var isarray = options.safe && Array.isArray(value);
var type = Object.prototype.toString.call(value);
var isobject = type === '[object Object]' || type === '[object Array]';
var newKey = prev ? prev + delimiter + key : key;
if (!options.maxDepth) {
maxDepth = currentDepth + 1;
}
if (
!isarray &&
isobject &&
Object.keys(value).length &&
currentDepth < maxDepth
) {
++currentDepth;
return step(value, newKey);
}
output[newKey] = value;
});
}
step(target);
return output;
}
/**
* Polyfill Object.keys
* // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
* Note: Had to do this because for some reason, the above will not work properly without using Object.keys
*/
if (!Object.keys) {
Object.keys = function(o) {
if (o !== Object(o)) {
throw new TypeError('Object.keys called on a non-object');
}
var k = [];
var p;
for (p in o) if (Object.prototype.hasOwnProperty.call(o, p)) k.push(p);
return k;
};
}