Skip to content

Instantly share code, notes, and snippets.

@greylurk
Created December 2, 2015 20:51
Show Gist options
  • Save greylurk/a8cf17743e1ad053602c to your computer and use it in GitHub Desktop.
Save greylurk/a8cf17743e1ad053602c to your computer and use it in GitHub Desktop.
/**
* @license Angulartics v0.19.2
* (c) 2013 Luis Farzati http://luisfarzati.github.io/angulartics
* Universal Analytics update contributed by http://github.com/willmcclellan
* License: MIT
*/
(function(angular) {
'use strict';
/**
* @ngdoc overview
* @name angulartics.intercom
* Enables analytics support for Intercom (https://www.intercom.io/)
*/
angular
.module('angulartics.intercom', ['angulartics', 'tnc.constants'])
.constant("Intercom", window.Intercom)
.config(ConfigAngularticsIntercom);
ConfigAngularticsIntercom.$inject = ['$analyticsProvider', 'Constants'];
function ConfigAngularticsIntercom($analyticsProvider, Constants) {
/*jshint newcap: false */
var booted = false;
if( window.Intercom ) {
$analyticsProvider.registerSetUsername(function (userId) {
window.Intercom('update', {
user_id: userId
});
});
/**
* Track SetUserProperties in Intercom
* @name eventTrack
*
* @param {string} action Required 'action' (string) associated with the event
* @param {object} properties = metadata
*
* @link http://doc.intercom.io/api/?javascript#submitting-events
*
* @example
* Intercom('trackEvent', 'invited-friend');
*/
$analyticsProvider.registerSetUserProperties(function (user) {
var intercomSettings = {
app_id: Constants.INTERCOM_APP_ID,
user_id: user.user_id,
email: user.email,
created_at: user.created_at
}
if( !booted ) {
window.intercomSettings = intercomSettings;
console.log("Booting user in Intercom")
console.log(intercomSettings);
window.Intercom('boot', intercomSettings);
booted = true;
} else {
console.log("Updating user in Intercom")
console.log(intercomSettings);
window.Intercom('update', intercomSettings);
}
});
/**
* Track Event in Intercom
* @name eventTrack
*
* @param {string} action Required 'action' (string) associated with the event
* @param {object} properties = metadata
*
* @link http://doc.intercom.io/api/?javascript#submitting-events
*
* @example
* Intercom('trackEvent', 'invited-friend');
*/
$analyticsProvider.registerEventTrack(function (action, properties) {
window.Intercom('trackEvent', action, properties);
});
}
}
})(angular);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment