Skip to content

Commit 6ba0fbb

Browse files
authored
Move heap repo (#179)
* Add integration heap This commit copies the content of the integration repo into the "integrations" folder. Original repo: https://github.com/segment-integrations/analytics.js-integration-heap Readme: https://github.com/segment-integrations/analytics.js-integration-heap/blob/master/README.md * Fix lint
1 parent 6a6a663 commit 6ba0fbb

File tree

7 files changed

+602
-0
lines changed

7 files changed

+602
-0
lines changed

integrations/heap/HISTORY.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
2.1.1 / 2018-07-06
3+
==================
4+
5+
* Merge pull request #14 from segment-integrations/revert_heap_issue
6+
7+
2.1.0 / 2018-05-21
8+
==================
9+
10+
* Update heap integration for heap.js v4 (#12)
11+
* Bump a.js-int-tester version to ^3.1.0 (#10)
12+
* Pin karma, karma-mocha dev dependencies (#9)
13+
14+
2.0.3 / 2016-09-07
15+
==================
16+
17+
* update analytics.js-integration
18+
* fix flattening
19+
20+
2.0.2 / 2016-07-08
21+
==================
22+
23+
* bump version for npm
24+
25+
2.0.1 / 2016-07-08
26+
==================
27+
28+
* fix uncaught bug when sending undefined properties
29+
30+
2.0.0 / 2016-06-21
31+
==================
32+
33+
* Remove Duo compatibility
34+
* Add CI setup (coverage, linting, cross-browser compatibility, etc.)
35+
* Update eslint configuration
36+
37+
1.1.1 / 2016-05-07
38+
==================
39+
40+
* Bump Analytics.js core, tester, integration to use Facade 2.x
41+
42+
1.1.0 / 2016-04-11
43+
==================
44+
45+
* Update to use new identify and addUserProperties
46+
47+
1.0.5 / 2016-02-23
48+
==================
49+
50+
* support nested objects and arrays
51+
52+
1.0.4 / 2015-06-30
53+
==================
54+
55+
* Replace analytics.js dependency with analytics.js-core
56+
57+
1.0.3 / 2015-06-30
58+
==================
59+
60+
* Replace analytics.js dependency with analytics.js-core
61+
62+
1.0.2 / 2015-06-24
63+
==================
64+
65+
* Bump analytics.js-integration version
66+
67+
1.0.1 / 2015-06-24
68+
==================
69+
70+
* Bump analytics.js-integration version
71+
72+
1.0.0 / 2015-06-09
73+
==================
74+
75+
* Initial commit :sparkles:

integrations/heap/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# analytics.js-integration-heap [![Build Status][ci-badge]][ci-link]
2+
3+
Heap integration for [Analytics.js][].
4+
5+
## License
6+
7+
Released under the [MIT license](LICENSE).
8+
9+
10+
[Analytics.js]: https://segment.com/docs/libraries/analytics.js/
11+
[ci-link]: https://circleci.com/gh/segment-integrations/analytics.js-integration-heap
12+
[ci-badge]: https://circleci.com/gh/segment-integrations/analytics.js-integration-heap.svg?style=svg

integrations/heap/karma.conf-ci.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../karma.conf-ci.js');

integrations/heap/karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../karma.conf');

integrations/heap/lib/index.js

+243
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
'use strict';
2+
3+
/* global JSON */
4+
/* eslint no-restricted-globals: [0] */
5+
6+
/**
7+
* Module dependencies.
8+
*/
9+
10+
var integration = require('@segment/analytics.js-integration');
11+
var each = require('component-each');
12+
var is = require('is');
13+
var extend = require('@ndhoule/extend');
14+
var toISOString = require('@segment/to-iso-string');
15+
var toString = Object.prototype.toString; // in case this method has been overridden by the user
16+
17+
/**
18+
* Expose `Heap` integration.
19+
*/
20+
21+
var Heap = (module.exports = integration('Heap')
22+
.global('heap')
23+
.option('appId', '')
24+
.tag('<script src="//cdn.heapanalytics.com/js/heap-{{ appId }}.js">'));
25+
26+
/**
27+
* Initialize.
28+
*
29+
* https://heapanalytics.com/docs/installation#web
30+
*
31+
* @api public
32+
*/
33+
34+
Heap.prototype.initialize = function() {
35+
window.heap = window.heap || [];
36+
window.heap.load = function(appid, config) {
37+
window.heap.appid = appid;
38+
window.heap.config = config;
39+
40+
var methodFactory = function(type) {
41+
return function() {
42+
window.heap.push(
43+
[type].concat(Array.prototype.slice.call(arguments, 0))
44+
);
45+
};
46+
};
47+
48+
var heapMethods = [
49+
'addEventProperties',
50+
'addUserProperties',
51+
'clearEventProperties',
52+
'identify',
53+
'removeEventProperty',
54+
'setEventProperties',
55+
'track',
56+
'unsetEventProperty',
57+
'resetIdentity'
58+
];
59+
each(heapMethods, function(method) {
60+
window.heap[method] = methodFactory(method);
61+
});
62+
};
63+
64+
window.heap.load(this.options.appId);
65+
this.load(this.ready);
66+
};
67+
68+
/**
69+
* Loaded?
70+
*
71+
* @api private
72+
* @return {boolean}
73+
*/
74+
75+
Heap.prototype.loaded = function() {
76+
return !!(window.heap && window.heap.appid);
77+
};
78+
79+
/**
80+
* Identify.
81+
*
82+
* https://heapanalytics.com/docs#identify
83+
*
84+
* @api public
85+
* @param {Identify} identify
86+
*/
87+
88+
Heap.prototype.identify = function(identify) {
89+
var traits = identify.traits({ email: '_email' });
90+
var id = identify.userId();
91+
if (id) window.heap.identify(id);
92+
window.heap.addUserProperties(clean(traits));
93+
};
94+
95+
/**
96+
* Track.
97+
*
98+
* https://heapanalytics.com/docs#track
99+
*
100+
* @api public
101+
* @param {Track} track
102+
*/
103+
104+
Heap.prototype.track = function(track) {
105+
window.heap.track(track.event(), clean(track.properties()));
106+
};
107+
108+
/**
109+
* Clean all nested objects and arrays.
110+
*
111+
* @param {Object} obj
112+
* @return {Object}
113+
* @api private
114+
*/
115+
116+
function clean(obj) {
117+
var ret = {};
118+
119+
for (var k in obj) {
120+
if (obj.hasOwnProperty(k)) {
121+
var value = obj[k];
122+
// Heap's natively library will drop null and undefined properties anyway
123+
// so no need to send these
124+
// also prevents uncaught errors since we call .toString() on non objects
125+
if (value === null || value === undefined) continue;
126+
127+
// date
128+
if (is.date(value)) {
129+
ret[k] = toISOString(value);
130+
continue;
131+
}
132+
133+
// leave boolean as is
134+
if (is.bool(value)) {
135+
ret[k] = value;
136+
continue;
137+
}
138+
139+
// leave numbers as is
140+
if (is.number(value)) {
141+
ret[k] = value;
142+
continue;
143+
}
144+
145+
// arrays of objects (eg. `products` array)
146+
if (toString.call(value) === '[object Array]') {
147+
ret = extend(ret, trample(k, value));
148+
continue;
149+
}
150+
151+
// non objects
152+
if (toString.call(value) !== '[object Object]') {
153+
ret[k] = value.toString();
154+
continue;
155+
}
156+
157+
ret = extend(ret, trample(k, value));
158+
}
159+
}
160+
// json
161+
// must flatten including the name of the original trait/property
162+
function trample(key, value) {
163+
var nestedObj = {};
164+
nestedObj[key] = value;
165+
var flattenedObj = flatten(nestedObj, { safe: true });
166+
167+
// stringify arrays inside nested object to be consistent with top level behavior of arrays
168+
for (var k in flattenedObj) {
169+
if (is.array(flattenedObj[k]))
170+
flattenedObj[k] = JSON.stringify(flattenedObj[k]);
171+
}
172+
173+
return flattenedObj;
174+
}
175+
176+
return ret;
177+
}
178+
179+
/**
180+
* Flatten nested objects
181+
* taken from https://www.npmjs.com/package/flat
182+
* @param {Object} obj
183+
* @return {Object} obj
184+
* @api public
185+
*/
186+
187+
function flatten(target, opts) {
188+
var options = opts || {};
189+
190+
var delimiter = options.delimiter || '.';
191+
var maxDepth = options.maxDepth;
192+
var currentDepth = 1;
193+
var output = {};
194+
195+
function step(object, prev) {
196+
Object.keys(object).forEach(function(key) {
197+
var value = object[key];
198+
var isarray = options.safe && Array.isArray(value);
199+
var type = Object.prototype.toString.call(value);
200+
var isobject = type === '[object Object]' || type === '[object Array]';
201+
202+
var newKey = prev ? prev + delimiter + key : key;
203+
204+
if (!options.maxDepth) {
205+
maxDepth = currentDepth + 1;
206+
}
207+
208+
if (
209+
!isarray &&
210+
isobject &&
211+
Object.keys(value).length &&
212+
currentDepth < maxDepth
213+
) {
214+
++currentDepth;
215+
return step(value, newKey);
216+
}
217+
218+
output[newKey] = value;
219+
});
220+
}
221+
222+
step(target);
223+
224+
return output;
225+
}
226+
227+
/**
228+
* Polyfill Object.keys
229+
* // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
230+
* Note: Had to do this because for some reason, the above will not work properly without using Object.keys
231+
*/
232+
233+
if (!Object.keys) {
234+
Object.keys = function(o) {
235+
if (o !== Object(o)) {
236+
throw new TypeError('Object.keys called on a non-object');
237+
}
238+
var k = [];
239+
var p;
240+
for (p in o) if (Object.prototype.hasOwnProperty.call(o, p)) k.push(p);
241+
return k;
242+
};
243+
}

integrations/heap/package.json

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "@segment/analytics.js-integration-heap",
3+
"description": "The Heap analytics.js integration.",
4+
"version": "2.1.1",
5+
"keywords": [
6+
"analytics.js",
7+
"analytics.js-integration",
8+
"segment",
9+
"heap"
10+
],
11+
"main": "lib/index.js",
12+
"scripts": {
13+
"test": "karma start",
14+
"test:ci": "karma start karma.conf-ci.js"
15+
},
16+
"author": "Segment <[email protected]>",
17+
"license": "SEE LICENSE IN LICENSE",
18+
"homepage": "https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/heap#readme",
19+
"bugs": {
20+
"url": "https://github.com/segmentio/analytics.js-integrations/issues"
21+
},
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/segmentio/analytics.js-integrations.git"
25+
},
26+
"dependencies": {
27+
"@ndhoule/extend": "^2.0.0",
28+
"@segment/analytics.js-integration": "^3.0.0",
29+
"@segment/to-iso-string": "^1.0.1",
30+
"component-each": "^0.2.6",
31+
"is": "^3.1.0"
32+
},
33+
"devDependencies": {
34+
"@segment/analytics.js-core": "^3.0.0",
35+
"@segment/analytics.js-integration-tester": "^3.1.0",
36+
"@segment/clear-env": "^2.0.0",
37+
"@segment/eslint-config": "^3.1.1",
38+
"browserify": "^13.0.0",
39+
"browserify-istanbul": "^2.0.0",
40+
"eslint": "^2.9.0",
41+
"eslint-plugin-mocha": "^2.2.0",
42+
"eslint-plugin-require-path-exists": "^1.1.5",
43+
"istanbul": "^0.4.3",
44+
"karma": "1.3.0",
45+
"karma-browserify": "^5.0.4",
46+
"karma-chrome-launcher": "^1.0.1",
47+
"karma-coverage": "^1.0.0",
48+
"karma-junit-reporter": "^1.0.0",
49+
"karma-mocha": "1.0.1",
50+
"karma-phantomjs-launcher": "^1.0.0",
51+
"karma-sauce-launcher": "^1.0.0",
52+
"karma-spec-reporter": "0.0.26",
53+
"mocha": "^2.2.5",
54+
"npm-check": "^5.2.1",
55+
"phantomjs-prebuilt": "^2.1.7",
56+
"watchify": "^3.7.0"
57+
}
58+
}

0 commit comments

Comments
 (0)