Skip to content

Commit

Permalink
📊 Add Keen ua/geo addons, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed May 26, 2015
1 parent 613f24d commit 6f5f42a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
7 changes: 2 additions & 5 deletions src/app.jsx
@@ -1,4 +1,3 @@
/*eslint camelcase: 0 */
// Array.from polyfill
import 'core-js/fn/array/from';
// Object.assign polyfill
Expand Down Expand Up @@ -27,10 +26,8 @@ const Bootstrapper = {
ReactRouter.run(appInstance, ReactRouter.HistoryLocation, function(Handler, state) {
React.render(<Handler />, document.getElementById('mainContainer'));
analytics.addEvent('pageviews', {
path : state.path,
action : state.action || 'pageload',
ip_address : '${keen.ip}',
user_agent : '${keen.user_agent}'
path : state.path,
action : state.action || 'pageload',
});
});
},
Expand Down
30 changes: 23 additions & 7 deletions src/helpers/analytics.js
@@ -1,3 +1,4 @@
/*eslint camelcase: 0 */
let Keen;
if (window && window.Keen) {
Keen = window.Keen;
Expand All @@ -7,7 +8,26 @@ if (window && window.Keen) {
Keen.prototype.addEvents = function() {};
}

const appUrl = window && window.location ? window.location.href : '';
const app_url = window && window.location ? window.location.href : '';
const commonEventParams = {
app_url,
ip_address : '${keen.ip}',
user_agent : '${keen.user_agent}',
keen : {
addons : [
{
name : 'keen:ua_parser',
input : { ua_string : 'user_agent' },
output : 'parsed_user_agent'
},
{
name : 'keen:ip_to_geo',
input : { ip : 'ip_address' },
output : 'ip_geo_info'
}
],
}
};

const client = new Keen({
projectId : '555a93d090e4bd76add99312',
Expand All @@ -16,17 +36,13 @@ const client = new Keen({

const analytics = {
addEvent: function(collection, payload, callback, async) {
if (!payload.appUrl) {
payload.appUrl = appUrl;
}
payload = Object.assign(commonEventParams, payload);
client.addEvent(collection, payload, callback, async);
},
addEvents: function(payload, callback) {
for (let collection in payload) {
if (payload.hasOwnProperty(collection)) {
if (!collection[payload].appUrl) {
collection[payload].appUrl = appUrl;
}
collection[payload] = Object.assign(commonEventParams, collection[payload]);
}
}
client.addEvents(payload, callback);
Expand Down

0 comments on commit 6f5f42a

Please sign in to comment.