Skip to content

Instantly share code, notes, and snippets.

@lovesh
Created April 23, 2014 07:46
Show Gist options
  • Save lovesh/11205938 to your computer and use it in GitHub Desktop.
Save lovesh/11205938 to your computer and use it in GitHub Desktop.
code to integrate multiple services like kissmetrics, intercom, etc easily
/*
An array of services that you want to integrate. I didnt make it an object because these services are started and stopped in the order they appear in the array.
Here i have taken 3 examples- kissmetrcs, intercom and vero
Service properties:
enabled- Setting it to false doesnt start or track events for this service
start- This function is initialises the servcie. In this function i place the initialization code snipplet that the service provides me.
Takes an optional array which has the service names i want to start. If this parameter is ommited all services are started
stop- Some services like intercom have a shutdown function, so here goes that code
track- This is the tracking function that is used to track events with the service. Takes 2 parameters eventName and eventData
validate: Sometimes you dont want to send tracking for some accounts, maybe they are test accounts or you are logging into some account as admin.
So this function can be used to allow/disallow tracking. When this function returns true an event is tracked by this service otherwise not.
*/
var services = [
{
name: 'kissmetrics',
enabled: true,
start: ksf,
stop: ksf1,
track: ktf,
validate: kvf
},
{
name: 'intercom',
enabled: true,
start: isf,
stop: isf1,
track: itf,
validate: ivf
},
{
name: 'vero',
enabled: true,
start: vsf,
stop: vsf1,
track: vtf,
validate: vvf
}
];
/*
The services that dont have a starting/stopping/tracking/validating function need to be passed null for that corresponding function
*/
function __trackingService__(name, startingFunction, stoppingFunction, trackingFunction, validatingFunction, enable) {
this.name = name;
this.enabled = enable;
this.running = false;
this.start = startingFunction;
this.track = trackingFunction;
this.stop = stoppingFunction;
this.validate = validatingFunction;
}
/*
Tracker properties:
services: An array of services to deal with
gloabalValidator: A function that if returns true would allow the tracker to start
*/
__tracker__ = function(services, globalValidator) {
this.start = function(servicesToStart) { // An array of names of services to start
this.trackingServices = [];
if (globalValidator && !globalValidator()) // if this function returns false, dont start any service and return
return;
services.forEach(function(service) {
if (!servicesToStart || servicesToStart.indexOf(service.name) > -1) // if servicesToStart is passed, only start the services that are present in servicesToStart
this.trackingServices.push({
new __trackingService__(service.name, service.start, service.stop, service.track, service.validate, service.enabled)
});
});
this.trackingServices.forEach(function(service) {
if (service.enabled && service.validate() && !service.running) {
if (service.start != null)
service.start();
service.running = true; // this is set to true irrespective of whether service.start is null or not because some services might not have a initialisation code
}
});
};
this.stop = function() {
this.trackingServices.forEach(function(service) {
if (service.enabled && service.running) {
if (service.stop != null)
service.stop();
service.running = false; // this is set to true irrespective of whether service.stop is null or not because some services might not have a cleanup code
}
});
};
this.track = function(eventName, eventData, services) {
if (!services) { // if no service names are passed, track this event for all started services
this.trackingServices.forEach(function(service) {
if (service.enabled && service.running && service.track !== null) {
service.track(eventName, eventData);
}
});
}
else {
if (services instanceof Array) {
this.trackingServices.forEach(function(service) {
if (services.indexOf(service.name) > -1 && service.enabled && service.running && service.track !== null) {
service.track(eventName, eventData);
}
});
}
else {
console.log('Services should be an array');
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment