开发者

Calling Mixpanel API only in production Rails environment

开发者 https://www.devze.com 2023-02-14 03:28 出处:网络
I am currently using mixpanel with the default \'mixpanel\' gem for analytics and have some calls set up in my controllers.I would like those calls to only run in the production environment.Is the bes

I am currently using mixpanel with the default 'mixpanel' gem for analytics and have some calls set up in my controllers. I would like those calls to only run in the production environment. Is the best way to do this something like this for every call:

@mixpanel.track_event("Job Accepted", {:user=> current_user.id}) if RAIL开发者_Python百科S_ENV == 'production'

Seems like overkill, but I'm struggling to think of a better solution. Any help would be great!


You've got the right idea. Though you might want to use:

Rails.env.production?

instead. ( See Rails.env vs RAILS_ENV )


Older question, but here is my method. Replace Mixpanel's default consumer(which sends to their API) with something more useful in testing. In our case, we just log out. This is in an initializer like mixpanel.rb.

if Rails.env.production?
  MIXPANEL = Mixpanel::Tracker.new(ENV['MIXPANEL_TOKEN'])
else
  MIXPANEL = Mixpanel::Tracker.new(ENV['MIXPANEL_TOKEN']) do |type, message|
     Rails.logger.debug("Mixpanel Request: #{type}, #{message}")
  end
end


This is what you are looking for, found it in the Gem's wiki:

https://github.com/zevarito/mixpanel/wiki/Disable-Mixpanel-tracking-in-desired-environments-with-Rails

0

精彩评论

暂无评论...
验证码 换一张
取 消