【问题标题】:trouble tracking a session in rails在 Rails 中跟踪会话时遇到问题
【发布时间】:2016-02-24 20:00:11
【问题描述】:

我在为我的 Rails 应用程序计算 Mixpanel 中的会话时遇到问题。基本上,每次有人从外部 URL(不是 mydomain.com)访问页面时,我都会触发“会话”混合面板跟踪事件。

所以我在钩子之前的 application_controller.rb 中有这样的东西:

def count_session
    referral = request.referer
    root_url = Figaro.env.root_uri

    ### If there's a referral and it includes the root domain, then don't 
    ### track because it's the same session

    if referral && referral.include?(root_url)  
      puts "!!!! NOT TRACKED !!!!!"
    else
      puts "!!!!! TRACKED #{id}  !!!!!!"
      mixpanel.track("Session", "ID"  => current_user.id, "Version" => Figaro.env.ab_version )
    end 
  end

为了调试,我可以看到“!!!!! TRACKED 4 !!!!!!”从我的帐户(用户 ID:4)访问页面时在我的控制台中。但是当我访问 MixPanel 时,仪表板中没有显示任何事件。

如果我在隐身浏览器中访问(ID:nil),或者如果我访问隐身,那么我可以让会话事件记录下来,然后显式登录到我的帐户。但目标是在用户从外部 url 访问的任何时候注册事件。

我的其他混合面板事件运行良好。知道为什么在这种情况下不会触发会话事件吗?我应该如何进一步调试呢?

谢谢!

编辑:我正在使用 Mengpaneel 进行 ruby​​ 实现,这是我的设置:

initializers/mengpaneel.rb:

Mengpaneel.configure do |config|
  config.token = Figaro.env.mixpanel_token
end

application_controller.rb:

before_action :setup_mixpanel
before_action :count_session

 def setup_mixpanel
    return unless user_signed_in?

    mengpaneel.setup do
        mixpanel.identify(current_user.id)

        mixpanel.people.set(
          "ID"              => current_user.id,
          "$email"          => current_user.email,
          "$created"        => current_user.created_at,
          "$last_login"     => current_user.current_sign_in_at
        )
    end
  end

【问题讨论】:

  • 仅供参考:current_user ? id = current_user.id : id = nil 可以更好地表达id = current_user ? current_user.id : nil。更好的是,您可以摆脱三元运算符并使用mixpanel.track("Session", "ID" => current_user.id),因为track 似乎接受nil 值。
  • @Mohamad 但如果没有 current_user,那么 current_user.id 不会抛出错误吗?
  • 是的,它会——请忽略我评论的后半部分,但之前的建议仍然是关于简化三元运算符。 :D
  • @JacksonCunningham - 你是如何构建 mixpanel 对象的?我假设您正在使用 mixpanel 的 ruby​​ 客户端发送事件?在这种情况下,第一个参数是跟踪 id,然后是事件类型。
  • @BroiSatse 查看我的编辑。我正在使用 Mengpaneel,如果用户存在,它会自动将 mixpanel 事件附加到用户

标签: ruby-on-rails mixpanel


【解决方案1】:

知道为什么在这种情况下会话事件不会触发吗?我应该如何进一步调试呢?

为了回答您问题的调试方面,我首先要确保我以正确的方式发送此事件。

到处添加日志记录!在mengpaneel的track方法中:

def track(event, properties = {})
  return if @disable_all_events || @disabled_events.include?(event)

  properties = @properties.merge(properties)

  super(@distinct_id, event, properties)
end

我会检查 @disable_all_events@disabled_events 的设置,方法是更新我的本地 gem 以记录这些属性。我还会记录调用super 的结果,以防它返回某种错误响应。或者使用红宝石debugger

我知道你说其他电话正在工作,所以也许只是这个电话被静默掉线了。

另一种选择是弄清楚如何在 HTTP 请求中间获取代理(即 Charles)。

【讨论】:

    猜你喜欢
    • 2016-05-19
    • 2014-10-16
    • 2013-12-19
    • 1970-01-01
    • 2012-10-24
    • 2020-03-13
    • 2013-12-17
    • 2011-11-28
    • 1970-01-01
    相关资源
    最近更新 更多