Skip to content

Commit 7226576

Browse files
Add integration keen-io This commit copies the content of the integration repo into the "integrations" folder. Original repo: https://github.com/segment-integrations/analytics.js-integration-keen-io Readme: https://github.com/segment-integrations/analytics.js-integration-keen-io/blob/master/README.md
1 parent 7893f16 commit 7226576

File tree

6 files changed

+682
-0
lines changed

6 files changed

+682
-0
lines changed

integrations/keen-io/HISTORY.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2.1.2 / 2017-01-31
2+
==================
3+
4+
* Update Keen.io SDK from v3.1.0 to v3.4.0
5+
6+
2.1.1 / 2016-10-20
7+
==================
8+
9+
* Fix bug with setGlobalProperties
10+
11+
2.1.0 / 2016-07-12
12+
==================
13+
14+
* Update Karma to 1.1.0
15+
* Add test to ensure correct Keen
16+
* Prevent namespace collisions
17+
* Update keen-js to v3.1.0 (latest possible without breaking changes)
18+
* Create a new `KeenSegment` namespace for analytics.js-installed SDK and return existing `Keen` object if overwritten
19+
20+
2.0.0 / 2016-06-21
21+
==================
22+
23+
* Remove Duo compatibility
24+
* Add CI setup (coverage, linting, cross-browser compatibility, etc.)
25+
* Update eslint configuration
26+
27+
1.0.4 / 2016-05-07
28+
==================
29+
30+
* Bump Analytics.js core, tester, integration to use Facade 2.x
31+
32+
1.0.3 / 2015-06-30
33+
==================
34+
35+
* Replace analytics.js dependency with analytics.js-core
36+
37+
1.0.2 / 2015-06-24
38+
==================
39+
40+
* Bump analytics.js-integration version
41+
42+
1.0.1 / 2015-06-24
43+
==================
44+
45+
* Bump analytics.js-integration version
46+
47+
1.0.0 / 2015-06-09
48+
==================
49+
50+
* Initial commit :sparkles:

integrations/keen-io/README.md

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

integrations/keen-io/lib/index.js

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var integration = require('@segment/analytics.js-integration');
8+
var clone = require('@ndhoule/clone');
9+
10+
/**
11+
* Expose `Keen IO` integration.
12+
*/
13+
14+
var Keen = module.exports = integration('Keen IO')
15+
.global('KeenSegment')
16+
.option('ipAddon', false)
17+
.option('projectId', '')
18+
.option('readKey', '')
19+
.option('referrerAddon', false)
20+
.option('trackAllPages', false)
21+
.option('trackCategorizedPages', true)
22+
.option('trackNamedPages', true)
23+
.option('uaAddon', false)
24+
.option('urlAddon', false)
25+
.option('writeKey', '')
26+
.tag('<script src="//d26b395fwzu5fz.cloudfront.net/3.4.0/{{ lib }}.min.js">');
27+
28+
/**
29+
* Initialize.
30+
*
31+
* https://keen.io/docs/
32+
*
33+
* @api public
34+
*/
35+
36+
Keen.prototype.initialize = function() {
37+
var lib = this.options.readKey ? 'keen' : 'keen-tracker';
38+
var options = this.options;
39+
var previousKeen = window.Keen || null;
40+
var self = this;
41+
42+
// Skip this step if [email protected] is already available
43+
if (!window.Keen || window.Keen.version !== '3.4.0') {
44+
// Force-undefine `Keen` (saved as `previousKeen`)
45+
window.Keen = undefined;
46+
/**
47+
* Shim out the Keen client library.
48+
*
49+
* To update the library, grab the most up-to-date embed code from Keen's
50+
* JS library readme (https://github.com/keen/keen-js) and remove any of the
51+
* script loading/appending business. Next, update the script tag above with
52+
* the new client library URL.
53+
*/
54+
/* eslint-disable */
55+
!(function(a,b){if(void 0===b[a]){b["_"+a]={},b[a]=function(c){b["_"+a].clients=b["_"+a].clients||{},b["_"+a].clients[c.projectId]=this,this._config=c},b[a].ready=function(c){b["_"+a].ready=b["_"+a].ready||[],b["_"+a].ready.push(c)};for(var c=["addEvent","setGlobalProperties","trackExternalLink","on"],d=0;d<c.length;d++){var e=c[d],f=function(a){return function(){return this["_"+a]=this["_"+a]||[],this["_"+a].push(arguments),this}};b[a].prototype[e]=f(e)}}})("Keen",window);
56+
/* eslint-enable */
57+
// [email protected] will be installed once `.load()` is called
58+
}
59+
60+
// Define a safe namespace (stub)
61+
window.KeenSegment = window.Keen;
62+
63+
// Define client (stub)
64+
this.client = new window.KeenSegment({
65+
projectId: options.projectId,
66+
readKey: options.readKey,
67+
writeKey: options.writeKey
68+
});
69+
70+
this.load({ lib: lib }, function() {
71+
// Redefine safe namespace with full library
72+
window.KeenSegment = window.Keen;
73+
// Restore original `Keen`
74+
if (previousKeen) {
75+
window.Keen = previousKeen;
76+
previousKeen = undefined;
77+
}
78+
self.ready();
79+
});
80+
};
81+
82+
/**
83+
* Loaded?
84+
*
85+
* @api private
86+
* @return {boolean}
87+
*/
88+
89+
Keen.prototype.loaded = function() {
90+
return !!(window.KeenSegment && window.KeenSegment.prototype.configure);
91+
};
92+
93+
/**
94+
* Page.
95+
*
96+
* @api public
97+
* @param {Page} page
98+
*/
99+
100+
Keen.prototype.page = function(page) {
101+
var category = page.category();
102+
var name = page.fullName();
103+
var opts = this.options;
104+
105+
// all pages
106+
if (opts.trackAllPages) {
107+
this.track(page.track());
108+
}
109+
110+
// named pages
111+
if (name && opts.trackNamedPages) {
112+
this.track(page.track(name));
113+
}
114+
115+
// categorized pages
116+
if (category && opts.trackCategorizedPages) {
117+
this.track(page.track(category));
118+
}
119+
};
120+
121+
/**
122+
* Identify.
123+
*
124+
* TODO: migrate from old `userId` to simpler `id`
125+
* https://keen.io/docs/data-collection/data-enrichment/#add-ons
126+
*
127+
* Set up the Keen addons object. These must be specifically
128+
* enabled by the settings in order to include the plugins, or else
129+
* Keen will reject the request.
130+
*
131+
* @api public
132+
* @param {Identify} identify
133+
*/
134+
135+
Keen.prototype.identify = function(identify) {
136+
var traits = identify.traits();
137+
var id = identify.userId();
138+
var user = {};
139+
if (id) user.userId = id;
140+
if (traits) user.traits = traits;
141+
var props = { user: user };
142+
this.addons(props, identify);
143+
this.client.setGlobalProperties(function() {
144+
// Clone the props so the Keen Client can't manipulate the ref
145+
return clone(props);
146+
});
147+
};
148+
149+
/**
150+
* Track.
151+
*
152+
* @api public
153+
* @param {Track} track
154+
*/
155+
156+
Keen.prototype.track = function(track) {
157+
var props = track.properties();
158+
this.addons(props, track);
159+
this.client.addEvent(track.event(), props);
160+
};
161+
162+
/**
163+
* Attach addons to `obj` with `msg`.
164+
*
165+
* @api private
166+
* @param {Object} obj
167+
* @param {Facade} msg
168+
*/
169+
170+
Keen.prototype.addons = function(obj, msg) {
171+
var options = this.options;
172+
var addons = [];
173+
174+
if (options.ipAddon) {
175+
addons.push({
176+
name: 'keen:ip_to_geo',
177+
input: { ip: 'ip_address' },
178+
output: 'ip_geo_info'
179+
});
180+
obj.ip_address = '${keen.ip}';
181+
}
182+
183+
if (options.uaAddon) {
184+
addons.push({
185+
name: 'keen:ua_parser',
186+
input: { ua_string: 'user_agent' },
187+
output: 'parsed_user_agent'
188+
});
189+
obj.user_agent = '${keen.user_agent}';
190+
}
191+
192+
if (options.urlAddon) {
193+
addons.push({
194+
name: 'keen:url_parser',
195+
input: { url: 'page_url' },
196+
output: 'parsed_page_url'
197+
});
198+
obj.page_url = document.location.href;
199+
}
200+
201+
if (options.referrerAddon) {
202+
addons.push({
203+
name: 'keen:referrer_parser',
204+
input: {
205+
referrer_url: 'referrer_url',
206+
page_url: 'page_url'
207+
},
208+
output: 'referrer_info'
209+
});
210+
obj.referrer_url = document.referrer;
211+
obj.page_url = document.location.href;
212+
}
213+
214+
obj.keen = {
215+
timestamp: msg.timestamp(),
216+
addons: addons
217+
};
218+
};

integrations/keen-io/package.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "@segment/analytics.js-integration-keen-io",
3+
"description": "The Keen Io analytics.js integration.",
4+
"version": "2.1.2",
5+
"keywords": [
6+
"analytics.js",
7+
"analytics.js-integration",
8+
"segment",
9+
"keen-io"
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/keen-io#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/clone": "^1.0.0",
27+
"@segment/analytics.js-integration": "^2.1.0"
28+
},
29+
"devDependencies": {
30+
"@segment/analytics.js-core": "^3.0.0",
31+
"@segment/analytics.js-integration-tester": "^3.1.0",
32+
"@segment/clear-env": "^2.0.0",
33+
"@segment/eslint-config": "^3.1.1",
34+
"browserify": "^13.0.0",
35+
"browserify-istanbul": "^2.0.0",
36+
"eslint": "^2.9.0",
37+
"eslint-plugin-mocha": "^2.2.0",
38+
"eslint-plugin-require-path-exists": "^1.1.5",
39+
"istanbul": "^0.4.3",
40+
"karma": "1.3.0",
41+
"karma-browserify": "^5.0.4",
42+
"karma-chrome-launcher": "^1.0.1",
43+
"karma-coverage": "^1.0.0",
44+
"karma-junit-reporter": "^1.0.0",
45+
"karma-mocha": "1.0.1",
46+
"karma-phantomjs-launcher": "^1.0.0",
47+
"karma-sauce-launcher": "^1.0.0",
48+
"karma-spec-reporter": "0.0.26",
49+
"mocha": "^2.2.5",
50+
"npm-check": "^5.2.1",
51+
"phantomjs-prebuilt": "^2.1.7",
52+
"watchify": "^3.7.0"
53+
}
54+
}

integrations/keen-io/test/.eslintrc

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)