Skip to content

Instantly share code, notes, and snippets.

@Rendez
Created January 5, 2014 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rendez/8269991 to your computer and use it in GitHub Desktop.
Save Rendez/8269991 to your computer and use it in GitHub Desktop.
Snooper Class: creates fake methods for the Mixpanel JS API. It uses sinon.js and sandboxing. Capybara Poltergeist: use injectJS to attach this file before the page loads, and use it to test whether your app is calling the right mixpanel method in your integration tests.
//
// Copyright (C) 2014 Proudsugar.com
// Author Luis Merino <luis@proudsugar.com>
//
function Snooper() {
// used to keep tab on the sync (push-array) mixpanel fakes'
window.__mixpanel = {};
// fake methods lists
this.stubs = 'track alias name_tag people.set people.set_once people.increment people.append people.track_charge people.clear_charges people.delete_user'.split(' ');
this.spies = 'track_links track_forms register register_once unregister identify set_config'.split(' ');
}
Snooper.prototype.setup = function() {
if (this.sandbox) {
this.sandbox.restore();
} else {
this.sandbox = sinon.sandbox.create();
}
// Snoop on this context for async mixpanel tracking
var loaded = function(mixpanel) {
this.sinonize(mixpanel);
}.bind(this)
// Snoop on this context for sync mixpanel tracking
this.sinonize(window.__mixpanel);
window.mixpanel.set_config({
loaded: loaded
});
};
Snooper.prototype.sinonize = function(scope) {
var len = 'people.'.length;
var sandbox = this.sandbox;
// very often, track_pageview is called internally because is default behavior,
// thus we have to stub it in advance to make sure it doesn't slide
if (scope.ua) {
// minified version method is 'ua' and we don't want to miss it
scope.track_pageview = scope.ua = sandbox.stub(window.mixpanel, 'track_pageview');
} else {
scope.track_pageview = sandbox.stub(window.mixpanel, 'track_pageview');
}
this.stubs.forEach(function(key) {
if (key.indexOf('people') != -1) {
scope[key] = sandbox.stub(window.mixpanel.people, key.substr(len));
} else {
scope[key] = sandbox.stub(window.mixpanel, key);
}
});
this.spies.forEach(function(key) {
scope[key] = sandbox.spy(window.mixpanel, key);
});
};
var host = document.location.search.substr(1);
if (document.location.host == host) {
console.log('Snooper Starting... Sniff, Sniff... Bark, Bark!');
var snooper = new Snooper();
document.addEventListener('DOMContentLoaded', snooper.setup.bind(snooper));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment