Skip to content

Commit eb283f4

Browse files
Add integration hubspot This commit copies the content of the integration repo into the "integrations" folder. Original repo: https://github.com/segment-integrations/analytics.js-integration-hubspot Readme: https://github.com/segment-integrations/analytics.js-integration-hubspot/blob/master/README.md
1 parent 26906a6 commit eb283f4

File tree

6 files changed

+505
-0
lines changed

6 files changed

+505
-0
lines changed

integrations/hubspot/HISTORY.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2.1.3 / 2018-07-06
2+
==================
3+
* Add support for tabs, carriage returns, new lines, vertical tabs, form feeds
4+
5+
2.1.2 / 2017-09-28
6+
==================
7+
8+
* Bugfix: Upgrade passed-in `identify` to a version with `companyName()`
9+
10+
2.1.1 / 2017-09-26
11+
==================
12+
13+
* Populate Hubspot-reserved `company` from `traits.company.name`
14+
15+
2.1.0 / 2017-03-16
16+
==================
17+
18+
* Bump analytics.js-integration and analytics.js-integration-tester to ^3.x
19+
20+
2.0.1 / 2016-08-31
21+
==================
22+
23+
* fix uppercases and spaces
24+
25+
2.0.0 / 2016-06-21
26+
==================
27+
28+
* Remove Duo compatibility
29+
* Add CI setup (coverage, linting, cross-browser compatibility, etc.)
30+
* Update eslint configuration
31+
32+
1.0.9 / 2016-05-07
33+
==================
34+
35+
* Bump Analytics.js core, tester, integration to use Facade 2.x
36+
37+
1.0.8 / 2015-11-14
38+
==================
39+
40+
* Merge pull request #5 from segment-integrations/revert-4-traits-lowercase
41+
* Revert "map firstname, lastname, and jobtitle."
42+
43+
1.0.7 / 2015-11-13
44+
==================
45+
46+
* Merge pull request #4 from segment-integrations/traits-lowercase
47+
* map firstname, lastname, and jobtitle.
48+
49+
1.0.6 / 2015-11-10
50+
==================
51+
52+
* Add support for semantic name fields to Hubspot
53+
54+
1.0.5 / 2015-07-07
55+
==================
56+
57+
* Map `revenue` to `value` param in `#track` calls
58+
59+
1.0.4 / 2015-06-30
60+
==================
61+
62+
* Replace analytics.js dependency with analytics.js-core
63+
64+
1.0.3 / 2015-06-30
65+
==================
66+
67+
* Send `.event` as `.id`, `properties.id` as `_id`
68+
* Hubspot expects the `id` property to be the event name; any events with the `.id` property were getting messed up previously
69+
70+
1.0.2 / 2015-06-24
71+
==================
72+
73+
* Bump analytics.js-integration version
74+
75+
1.0.1 / 2015-06-24
76+
==================
77+
78+
* Bump analytics.js-integration version
79+
80+
1.0.0 / 2015-06-09
81+
==================
82+
83+
* Initial commit :sparkles:

integrations/hubspot/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# analytics.js-integration-hubspot [![Build Status][ci-badge]][ci-link]
2+
3+
Hubspot 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-hubspot
12+
[ci-badge]: https://circleci.com/gh/segment-integrations/analytics.js-integration-hubspot.svg?style=svg

integrations/hubspot/lib/index.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var Identify = require('segmentio-facade').Identify;
8+
var convert = require('@segment/convert-dates');
9+
var integration = require('@segment/analytics.js-integration');
10+
var push = require('global-queue')('_hsq');
11+
var each = require('@ndhoule/each');
12+
13+
/**
14+
* Expose `HubSpot` integration.
15+
*/
16+
17+
var HubSpot = module.exports = integration('HubSpot')
18+
.assumesPageview()
19+
.global('_hsq')
20+
.option('portalId', null)
21+
.tag('<script id="hs-analytics" src="https://js.hs-analytics.net/analytics/{{ cacheBuster }}/{{ portalId }}.js">');
22+
23+
/**
24+
* Initialize.
25+
*
26+
* @api public
27+
*/
28+
29+
HubSpot.prototype.initialize = function() {
30+
window._hsq = [];
31+
var cacheBuster = Math.ceil(new Date() / 300000) * 300000;
32+
this.load({ cacheBuster: cacheBuster }, this.ready);
33+
};
34+
35+
/**
36+
* Loaded?
37+
*
38+
* @api private
39+
* @return {boolean}
40+
*/
41+
42+
HubSpot.prototype.loaded = function() {
43+
return !!(window._hsq && window._hsq.push !== Array.prototype.push);
44+
};
45+
46+
/**
47+
* Page.
48+
*
49+
* @api public
50+
* @param {Page} page
51+
*/
52+
53+
HubSpot.prototype.page = function() {
54+
push('trackPageView');
55+
};
56+
57+
/**
58+
* Identify.
59+
*
60+
* @api public
61+
* @param {Identify} identify
62+
*/
63+
64+
HubSpot.prototype.identify = function(identify) {
65+
// use newer version of Identify to have access to `companyName`
66+
var newIdentify = new Identify({
67+
traits: identify.traits(),
68+
userId: identify.userId()
69+
});
70+
71+
if (!newIdentify.email()) {
72+
return;
73+
}
74+
75+
var traits = newIdentify.traits({ firstName: 'firstname', lastName: 'lastname' });
76+
traits = convertDates(traits);
77+
traits = formatTraits(traits);
78+
79+
if (newIdentify.companyName() !== undefined) {
80+
traits.company = newIdentify.companyName();
81+
}
82+
83+
push('identify', traits);
84+
};
85+
86+
/**
87+
* Track.
88+
*
89+
* @api public
90+
* @param {Track} track
91+
*/
92+
93+
HubSpot.prototype.track = function(track) {
94+
// Hubspot expects properties.id to be the name of the .track() event
95+
// Ref: http://developers.hubspot.com/docs/methods/enterprise_events/javascript_api
96+
var props = convertDates(track.properties({ id: '_id', revenue: 'value' }));
97+
props.id = track.event();
98+
99+
push('trackEvent', track.event(), props);
100+
};
101+
102+
/**
103+
* Convert all the dates in the HubSpot properties to millisecond times
104+
*
105+
* @api private
106+
* @param {Object} properties
107+
*/
108+
109+
function convertDates(properties) {
110+
return convert(properties, function(date) { return date.getTime(); });
111+
}
112+
113+
/**
114+
* lowercase & snakecase any trait with uppercase letters or spaces
115+
* Hubspot cannot accept uppercases or spaces
116+
*
117+
* @api private
118+
* @param {Object} traits
119+
* @return {Object} ret
120+
*/
121+
122+
function formatTraits(traits) {
123+
var ret = {};
124+
each(function(value, key) {
125+
// Using split/join due to IE 11 failing to properly support regex in str.replace()
126+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@replace
127+
var k = key.toLowerCase()
128+
.split(' ').join('_') // spaces
129+
.split('.').join('_') // Periods
130+
.split('\n').join('_') // new lines
131+
.split('\v').join('_') // Vertical tabs
132+
.split('\t').join('_') // Regular tabs
133+
.split('\f').join('_') // form feeds
134+
.split('\r').join('_'); // Carriage returns
135+
ret[k] = value;
136+
}, traits);
137+
138+
return ret;
139+
}

integrations/hubspot/package.json

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

0 commit comments

Comments
 (0)