mixpanel_tracker
Provides ability to track Mixpanel events in Rails. Events can be tracked from controllers - and no funnel will break.
Installation
Add to your Gemfile
gem 'mixpanel_tracker'
And bundle it
$ bundle
Then, run the generator
$ rails generate mixpanel_tracker YOUR_ACCESS_TOKEN
This will generate initializer mixpanel_tracker.rb
.
If you omit YOUR_ACCESS_TOKEN
, initializer will try to fetch access token from environment variable ENV['MIXPANEL_ACCESS_TOKEN']
.
Place this in your layout before the closing </head>
tag:
<%= include_mixpanel %>
Usage
Track events in needed places in your controllers:
# landing_controller.rb
def index
mixpanel.track 'User arrived'
end
You can supply additional data to events:
# orders_controller.rb
def create
if @order.save
mixpanel.track 'Order created', items_count: @order.items.count
end
end
Additional notes
By default, mixpanel_tracker will fetch utm params and register them in events
# mixpanel_tracker.rb
config.register_utm_params = true
You may want to not track events in your development or test environment
# mixpanel_tracker.rb
if Rails.env.test? or Rails.env.development?
config.enabled = false
end