Skip to content

Disable Mixpanel tracking in desired environments with Rails

zevarito edited this page Jun 20, 2011 · 2 revisions

This is a work around to avoid track events on certain environments, like tests or development.

Replace your current Mixpanel Initializer with this

if ["production"].include?(Rails.env)
  MIXPANEL_TOKEN = "your_token_here"
  YourApplication::Application.config.middleware.use "Mixpanel::Tracker::Middleware", MIXPANEL_TOKEN
else
  class DummyMixpanel
    def method_missing(m, *args, &block)
      true
    end
  end
end

In your Application Controller add this...

before_filter :initialize_mixpanel

def initialize_mixpanel
  if defined?(MIXPANEL_TOKEN)
    @mixpanel = Mixpanel.new(MIXPANEL_TOKEN, request.env)
  else
    @mixpanel = DummyMixpanel.new
  end
end