Skip to content

Commit

Permalink
Add integration heap
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SegmentDestinationsBot committed Aug 13, 2019
1 parent 6a6a663 commit 15063ad
Show file tree
Hide file tree
Showing 6 changed files with 566 additions and 0 deletions.
75 changes: 75 additions & 0 deletions integrations/heap/HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

2.1.1 / 2018-07-06
==================

* Merge pull request #14 from segment-integrations/revert_heap_issue

2.1.0 / 2018-05-21
==================

* Update heap integration for heap.js v4 (#12)
* Bump a.js-int-tester version to ^3.1.0 (#10)
* Pin karma, karma-mocha dev dependencies (#9)

2.0.3 / 2016-09-07
==================

* update analytics.js-integration
* fix flattening

2.0.2 / 2016-07-08
==================

* bump version for npm

2.0.1 / 2016-07-08
==================

* fix uncaught bug when sending undefined properties

2.0.0 / 2016-06-21
==================

* Remove Duo compatibility
* Add CI setup (coverage, linting, cross-browser compatibility, etc.)
* Update eslint configuration

1.1.1 / 2016-05-07
==================

* Bump Analytics.js core, tester, integration to use Facade 2.x

1.1.0 / 2016-04-11
==================

* Update to use new identify and addUserProperties

1.0.5 / 2016-02-23
==================

* support nested objects and arrays

1.0.4 / 2015-06-30
==================

* Replace analytics.js dependency with analytics.js-core

1.0.3 / 2015-06-30
==================

* Replace analytics.js dependency with analytics.js-core

1.0.2 / 2015-06-24
==================

* Bump analytics.js-integration version

1.0.1 / 2015-06-24
==================

* Bump analytics.js-integration version

1.0.0 / 2015-06-09
==================

* Initial commit :sparkles:
12 changes: 12 additions & 0 deletions integrations/heap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# analytics.js-integration-heap [![Build Status][ci-badge]][ci-link]

Heap integration for [Analytics.js][].

## License

Released under the [MIT license](LICENSE).


[Analytics.js]: https://segment.com/docs/libraries/analytics.js/
[ci-link]: https://circleci.com/gh/segment-integrations/analytics.js-integration-heap
[ci-badge]: https://circleci.com/gh/segment-integrations/analytics.js-integration-heap.svg?style=svg
226 changes: 226 additions & 0 deletions integrations/heap/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
'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) {
opts = opts || {};

var delimiter = opts.delimiter || '.';
var maxDepth = opts.maxDepth;
var currentDepth = 1;
var output = {};

function step(object, prev) {
Object.keys(object).forEach(function(key) {
var value = object[key];
var isarray = opts.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 (!opts.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;
};
}
57 changes: 57 additions & 0 deletions integrations/heap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@segment/analytics.js-integration-heap",
"description": "The Heap analytics.js integration.",
"version": "2.1.1",
"keywords": [
"analytics.js",
"analytics.js-integration",
"segment",
"heap"
],
"main": "lib/index.js",
"scripts": {
"test": "make test"
},
"author": "Segment \u003cfriends@segment.com\u003e",
"license": "SEE LICENSE IN LICENSE",
"homepage": "https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/heap#readme",
"bugs": {
"url": "https://github.com/segmentio/analytics.js-integrations/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/segmentio/analytics.js-integrations.git"
},
"dependencies": {
"@ndhoule/extend": "^2.0.0",
"@segment/analytics.js-integration": "^3.0.0",
"@segment/to-iso-string": "^1.0.1",
"component-each": "^0.2.6",
"is": "^3.1.0"
},
"devDependencies": {
"@segment/analytics.js-core": "^3.0.0",
"@segment/analytics.js-integration-tester": "^3.1.0",
"@segment/clear-env": "^2.0.0",
"@segment/eslint-config": "^3.1.1",
"browserify": "^13.0.0",
"browserify-istanbul": "^2.0.0",
"eslint": "^2.9.0",
"eslint-plugin-mocha": "^2.2.0",
"eslint-plugin-require-path-exists": "^1.1.5",
"istanbul": "^0.4.3",
"karma": "1.3.0",
"karma-browserify": "^5.0.4",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.0.0",
"karma-junit-reporter": "^1.0.0",
"karma-mocha": "1.0.1",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sauce-launcher": "^1.0.0",
"karma-spec-reporter": "0.0.26",
"mocha": "^2.2.5",
"npm-check": "^5.2.1",
"phantomjs-prebuilt": "^2.1.7",
"watchify": "^3.7.0"
}
}
3 changes: 3 additions & 0 deletions integrations/heap/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@segment/eslint-config/mocha"
}
Loading

0 comments on commit 15063ad

Please sign in to comment.