如何开始 mixed_panel?

How to Get Started with mixed_panel?

我运行gem install mixpanel-ruby 我还在 gemfile 中添加了 mixpanel-ruby

我对下一步该做什么感到困惑?

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,
})

我不确定我是把上面的代码放在初始化器或 application.rb 还是用户模型中。

当我尝试将更改推送到 heroku 时,出现错误:ActiveRecord::RecordNotFound: Couldn't find User with 'id'=id

我希望能够跟踪每个用户的行为。

参考: https://mixpanel.com/help/reference/ruby

一般的想法是,当用户执行某些操作时,您会跟踪它。 没有一种正确的方法可以做到这一点。如果它完成了他们的 JS 库,您可以将代码放在视图层中。

但是由于您是在服务器端执行此操作,因此放置代码的合乎逻辑的位置是在控制器中。

例如: 当用户喜欢 post 时,您将跟踪代码放入适当的控制器操作中。您需要用户对象来传递用户 ID,以及您想与混合面板共享的任何其他详细信息。

显然,我并不是说控制器是放置跟踪代码的唯一地方,在某些情况下,您会使用 Task Runner,Que 在后台委托它。

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