简体   繁体   中英

How do I use mixpanel-ruby gem to track events?

I'm integrating Mixpanel into my Ruby on Rails app. I have the Javascript tags correctly integrated for client side view tracking.

I also put Mixpanel tracking tags for server side events in my models and controllers in various way without success. Here's an example of one of my attempts:

I have installed the mixpanel-ruby gem and added the following code:

/initializers/mixpanel.rb

require 'mixpanel-ruby'

if Rails.env == "development"
  tracker = Mixpanel::Tracker.new(ENV['MIXPANEL_DEV_TOKEN'])
end

if Rails.env == "production"
  tracker = Mixpanel::Tracker.new(ENV['MIXPANEL_PROD_TOKEN'])
end

I have a model Review where I would like to track when a review is added.

models/review.rb

class Review < ActiveRecord::Base
after_create :track_review

def track_review
  tracker.track('User', 'Added Review')
end

The Ruby instructions from Mixpanel (which aren't specific to Rails apps) say at least these pieces are required:

require 'mixpanel-ruby'

tracker = Mixpanel::Tracker.new(PROJECT_TOKEN)

# Tracks an event, 'Sent Message',
# with distinct_id user_id
tracker.track(user_id, 'Sent Message')

I have each of these but get no errors and no events showing up in Mixpanel (it correctly shows my client side view events though). So what am I doing incorrectly? Thanks in advance!

I solved this with 3 fixes:

1: The variable declared in initializers/mixpanel.rb needed to be a global variable written as $variable. I changed it to a global variable by adding a dollar sign and then moved my environment variables to my application.yml file (I'm using Figaro).

2: In development, I was running into a rare SSL error documented in Mixpanel's Git issues . A friend helped me use a debugger to find out that I was getting a connection error when calling Mixpanel's servers from my development environment. To fix this I added to my initializers/mixpanel.rb a code snippet suggested in the issue discussion.

initializers/mixpanel.rb

require 'mixpanel-ruby'
$tracker = Mixpanel::Tracker.new(ENV['MIXPANEL_TOKEN'])

if Rails.env.development? 
  #silence local SSL errors
  Mixpanel.config_http do |http|
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
end

3: In production, I still couldn't get things to work. It turned out that I needed to set my Mixpanel config token in Heroku's environment variables. I used the following in my command line to do that

heroku config:set MIXPANEL_TOKEN=my_token_string_goes_here

After doing all of those things, I had a properly declared global variable for the Mixpanel tracker, an environment file with the development and product tokens, an SSL error patch, and an updated Heroku config with my Mixpanel token. All of these and now Mixpanel is tracking my serverside events properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM