Search code examples
ruby-on-railsrubyherokutracking

How to Get Started with mixed_panel?


I ran gem install mixpanel-ruby I also added mixpanel-ruby in the gemfile.

I'm confused as to what to do next?

initializers/mixed_panel.rb

require 'mixpanel-ruby'

tracker = Mixpanel::Tracker.new(ENV['YOUR_MIXPANEL_TOKEN'])

user = User.find([:id])

# Track an event on behalf of user "User1"
tracker.track(user.id, 'A Mixpanel User')

# Send an update to User1's profile
tracker.people.set(user.id, {
    '$name' => user.name,
    '$last_name' => user.last_name,
})

I'm not sure if I put the above code in an initializer or application.rb or in the user model.

When I try to push the changes to heroku I get the error: ActiveRecord::RecordNotFound: Couldn't find User with 'id'=id.

I want to be able to track the behavior of each user.

REFERENCE: https://mixpanel.com/help/reference/ruby


Solution

  • I think you can do like: 
    put `$tracker = Mixpanel::Tracker.new(ENV['YOUR_MIXPANEL_TOKEN'])` 
    in initializers/mixed_panel.rb and on application controller you can put rest of your code with a after_filter and when current_user present or can put code on any action where you need to track and get find user there
    
    def track_details
     # your code and use $tracker
    end