Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

update integration to use keen-tracking v2 and parsers #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 77 additions & 55 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,49 @@

var integration = require('@segment/analytics.js-integration');
var clone = require('@ndhoule/clone');
var extend = require('@ndhoule/extend');

/**
* Expose `Keen IO` integration.
*/

var Keen = module.exports = integration('Keen IO')
.global('KeenSegment')
.option('ipAddon', false)

// project config
.option('projectId', '')
.option('writeKey', '')
.option('readKey', '')

// add-ons
.option('ipAddon', false)
.option('referrerAddon', false)
.option('uaAddon', false)
.option('urlAddon', false)
.option('datetimeAddon', false)

// track
.option('trackAllPages', false)
.option('trackCategorizedPages', true)
.option('trackNamedPages', true)
.option('uaAddon', false)
.option('urlAddon', false)
.option('writeKey', '')
.tag('<script src="//d26b395fwzu5fz.cloudfront.net/3.4.0/{{ lib }}.min.js">');

// library
.tag('<script src="https://d26b395fwzu5fz.cloudfront.net/{{ lib }}.min.js">');

/**
* Initialize.
*
* https://github.com/keen/keen-js#installation
* https://keen.io/docs/
*
* @api public
*/

Keen.prototype.initialize = function() {
var lib = this.options.readKey ? 'keen' : 'keen-tracker';
var lib = this.options.readKey ? '5.0.1/keen.bundle' : 'keen-tracking-2.0.1';
var options = this.options;
var previousKeen = window.Keen || null;
var self = this;

// Skip this step if keen-js@3.4.0 is already available
if (!window.Keen || window.Keen.version !== '3.4.0') {
// Force-undefine `Keen` (saved as `previousKeen`)
window.Keen = undefined;
/**
* Shim out the Keen client library.
*
* To update the library, grab the most up-to-date embed code from Keen's
* JS library readme (https://github.com/keen/keen-js) and remove any of the
* script loading/appending business. Next, update the script tag above with
* the new client library URL.
*/
/* eslint-disable */
!(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);
/* eslint-enable */
// keen-js@3.4.0 will be installed once `.load()` is called
}

// Define a safe namespace (stub)
window.KeenSegment = window.Keen;

// Define client (stub)
this.client = new window.KeenSegment({
projectId: options.projectId,
readKey: options.readKey,
writeKey: options.writeKey
});

this.load({ lib: lib }, function() {
// Redefine safe namespace with full library
window.KeenSegment = window.Keen;
Expand All @@ -75,6 +57,12 @@ Keen.prototype.initialize = function() {
window.Keen = previousKeen;
previousKeen = undefined;
}
self.client = extend(self.client ? self.client : {},
new window.KeenSegment({
projectId: options.projectId,
readKey: options.readKey,
writeKey: options.writeKey
}));
self.ready();
});
};
Expand Down Expand Up @@ -140,9 +128,11 @@ Keen.prototype.identify = function(identify) {
if (traits) user.traits = traits;
var props = { user: user };
this.addons(props, identify);
this.client.setGlobalProperties(function() {
// Clone the props so the Keen Client can't manipulate the ref
return clone(props);
this.client = extend(this.client ? this.client : {}, {
extendEvents: function() {
// Clone the props so the Keen Client can't manipulate the ref
return clone(props);
}
});
};

Expand All @@ -156,7 +146,7 @@ Keen.prototype.identify = function(identify) {
Keen.prototype.track = function(track) {
var props = track.properties();
this.addons(props, track);
this.client.addEvent(track.event(), props);
this.client.recordEvent(track.event(), props);
};

/**
Expand All @@ -167,52 +157,84 @@ Keen.prototype.track = function(track) {
* @param {Facade} msg
*/

Keen.prototype.addons = function(obj, msg) {
Keen.prototype.addons = function(obj) {
var options = this.options;
var addons = [];

if (options.ipAddon) {
addons.push({
name: 'keen:ip_to_geo',
input: { ip: 'ip_address' },
output: 'ip_geo_info'
input: {
ip: 'geo.ip_address'
},
output : 'geo.info'
});
obj.ip_address = '${keen.ip}';
obj.geo = {
info: {},
ip_address: '${keen.ip}'
};
}

if (options.uaAddon) {
obj.tech = {
info: { /* Enriched */ },
user_agent: '${keen.user_agent}'
};
addons.push({
name: 'keen:ua_parser',
input: { ua_string: 'user_agent' },
output: 'parsed_user_agent'
input: {
ua_string: 'tech.user_agent'
},
output: 'tech.info'
});
obj.user_agent = '${keen.user_agent}';
}

if (options.urlAddon) {
obj.page = {
info: { /* Enriched */ },
title: document.title,
url: document.location.href
};
addons.push({
name: 'keen:url_parser',
input: { url: 'page_url' },
output: 'parsed_page_url'
input: {
url: 'page.url'
},
output: 'page.info'
});
obj.page_url = document.location.href;
}

if (options.referrerAddon) {
obj.referrer = {
info: { /* Enriched */ },
url: document.referrer
};
addons.push({
name: 'keen:referrer_parser',
input: {
referrer_url: 'referrer_url',
page_url: 'page_url'
referrer_url: 'referrer.url',
page_url: 'page.url'
},
output: 'referrer.info'
});
}

if (options.datetimeAddon) {
obj.referrer = {
info: { /* Enriched */ },
url: document.referrer
};
addons.push({
name: 'keen:date_time_parser',
input: {
date_time: 'keen.timestamp'
},
output: 'referrer_info'
output: 'timestamp_info'
});
obj.referrer_url = document.referrer;
obj.page_url = document.location.href;
}

obj.keen = {
timestamp: msg.timestamp(),
timestamp: new Date().toISOString(),
addons: addons
};
};
Loading