Mixpanel Mock
Mixpanel is great for tracking and analyzing user behavior within your application. Here, I will show how to mock out the JavaScript agent to keep from sending events during local development.
When using Mixpanel it’s best not to send events while in development, staging, demo, or QA environments. I created a class to load a mock that will simply log to the console for tracking events.
class MixpanelMock
track: () ->
console.log("mixpanel.track", arguments)
window.mixpanel = new MixpanelMock()
You can get the updated source of MixpanelMock on GitHub.
Using in Rails
I usually save the Mixpanel JavaScript Library to a file in vendor/assets/javascripts/mixpanel.js
.
Then I place MixpanelMock in vendor/assets/javascripts/mixpanel-mock.coffee
.
I use the code below to conditionally load the real or fake Mixpanel library.
- if Rails.env.production?
= javascript_include_tag "mixpanel"
- else
= javascript_include_tag "mixpanel-mock"
This way, when not in production, all of your Mixpanel tracking events will simply be printed to the console. This also makes it super simple, as a developer, to know when certain events are being sent to Mixpanel.