Skip to content

Commit

Permalink
Add integration hubspot
Browse files Browse the repository at this point in the history
  • Loading branch information
SegmentDestinationsBot committed Aug 20, 2018
1 parent 26906a6 commit eb283f4
Show file tree
Hide file tree
Showing 6 changed files with 505 additions and 0 deletions.
83 changes: 83 additions & 0 deletions integrations/hubspot/HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
2.1.3 / 2018-07-06
==================
* Add support for tabs, carriage returns, new lines, vertical tabs, form feeds

2.1.2 / 2017-09-28
==================

* Bugfix: Upgrade passed-in `identify` to a version with `companyName()`

2.1.1 / 2017-09-26
==================

* Populate Hubspot-reserved `company` from `traits.company.name`

2.1.0 / 2017-03-16
==================

* Bump analytics.js-integration and analytics.js-integration-tester to ^3.x

2.0.1 / 2016-08-31
==================

* fix uppercases and spaces

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

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

1.0.9 / 2016-05-07
==================

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

1.0.8 / 2015-11-14
==================

* Merge pull request #5 from segment-integrations/revert-4-traits-lowercase
* Revert "map firstname, lastname, and jobtitle."

1.0.7 / 2015-11-13
==================

* Merge pull request #4 from segment-integrations/traits-lowercase
* map firstname, lastname, and jobtitle.

1.0.6 / 2015-11-10
==================

* Add support for semantic name fields to Hubspot

1.0.5 / 2015-07-07
==================

* Map `revenue` to `value` param in `#track` calls

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

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

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

* Send `.event` as `.id`, `properties.id` as `_id`
* Hubspot expects the `id` property to be the event name; any events with the `.id` property were getting messed up previously

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/hubspot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# analytics.js-integration-hubspot [![Build Status][ci-badge]][ci-link]

Hubspot 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-hubspot
[ci-badge]: https://circleci.com/gh/segment-integrations/analytics.js-integration-hubspot.svg?style=svg
139 changes: 139 additions & 0 deletions integrations/hubspot/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
'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')
.option('portalId', null)
.tag('<script id="hs-analytics" src="https://js.hs-analytics.net/analytics/{{ cacheBuster }}/{{ portalId }}.js">');

/**
* Initialize.
*
* @api public
*/

HubSpot.prototype.initialize = function() {
window._hsq = [];
var cacheBuster = Math.ceil(new Date() / 300000) * 300000;
this.load({ cacheBuster: cacheBuster }, this.ready);
};

/**
* Loaded?
*
* @api private
* @return {boolean}
*/

HubSpot.prototype.loaded = function() {
return !!(window._hsq && window._hsq.push !== Array.prototype.push);
};

/**
* 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;
}
57 changes: 57 additions & 0 deletions integrations/hubspot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@segment/analytics.js-integration-hubspot",
"description": "The Hubspot analytics.js integration.",
"version": "2.1.3",
"keywords": [
"analytics.js",
"analytics.js-integration",
"segment",
"hubspot"
],
"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/hubspot#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/each": "^2.0.1",
"@segment/analytics.js-integration": "^3.0.0",
"@segment/convert-dates": "^1.0.0",
"global-queue": "^1.0.1",
"segmentio-facade": "^3.2.2"
},
"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/hubspot/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 eb283f4

Please sign in to comment.