我运行了gem install mixpanel-ruby,我还在the文件中添加了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。
我希望能够跟踪每个用户的行为。
发布于 2016-12-06 05:06:12
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发布于 2016-12-06 05:05:50
一般的想法是,当用户执行某些动作时,您可以跟踪它。没有一种正确的方法可以做到这一点。如果它使用了他们的JS库,您将把代码放在视图层。
但是,由于您是在服务器端执行此操作,所以将代码放在控制器中的逻辑位置。
当用户喜欢一个帖子时,你可以将跟踪代码放入控制器操作中,这是合适的。您将需要用户对象传递用户id,以及您希望与mixpanel共享的任何其他细节。
显然,我并不是说控制器是您放置跟踪代码的唯一地方,在某些情况下,您将使用任务运行程序Que在后台委托它。
https://stackoverflow.com/questions/40988083
复制相似问题