Mixpanel mock

March 12, 2014

Key takeaway: Keep analytics events out of development and QA by loading a console-logging mock outside production.

Don’t send Mixpanel events from development, staging, demo, or QA. I load a mock that logs each tracking call to the console instead.

class MixpanelMock
  track: () ->
      console.log("mixpanel.track", arguments)

window.mixpanel = new MixpanelMock()

In Rails, I save the Mixpanel JavaScript library to vendor/assets/javascripts/mixpanel.js and the mock to vendor/assets/javascripts/mixpanel-mock.coffee. Then the layout picks one:

- if Rails.env.production?
  = javascript_include_tag "mixpanel"
- else
  = javascript_include_tag "mixpanel-mock"

Outside production, every tracking event prints to the console. Your analytics stay clean, and you can see exactly when an event would have gone to Mixpanel.