无法在rails中跟踪会话

时间:2022-12-08 23:17:37

I'm having an issue counting sessions in Mixpanel for my Rails app. Basically, I'm firing a "Session" mixpanel tracking event every time someone visits a page from an external URL (not mydomain.com).

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

So I have something like this in my application_controller.rb before hook:

所以我在我的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

For debugging, I'm able to see "!!!!! TRACKED 4 !!!!!!" in my console when visiting the page from my account (user id: 4). But when I visit MixPanel, there's no event that shows up in the dashboard.

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

I can get the session event to log if I visit in an incognito browser (with id: nil) OR if I visit incognito, then explicitly login to my account. But the goal is to have the event register any time a user visits from an external url.

如果我在隐身浏览器中访问(ID为nil),或者如果我访问隐身,然后明确登录到我的帐户,我可以记录会话事件。但目标是在用户从外部URL访问时随时注册事件。

My other mixpanel events are working fine. Any idea why the session event would not be firing in this instance? How should I go about further debugging this?

我的其他mixpanel活动正常。知道为什么会话事件不会在这个实例中触发?我该如何进一步调试呢?

Thanks!

Edit: I'm using Mengpaneel for ruby implementation, here's my setup:

编辑:我正在使用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

1 个解决方案

#1


0  

Any idea why the session event would not be firing in this instance? How should I go about further debugging this?

知道为什么会话事件不会在这个实例中触发?我该如何进一步调试呢?

To answer the debugging side of your question, I'd start by ensuring I'm sending this event in the right way.

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

Add logging everywhere! In mengpaneel's track method:

到处添加日志!在mengpaneel的跟踪方法中:

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

  properties = @properties.merge(properties)

  super(@distinct_id, event, properties)
end

I'd check what @disable_all_events and @disabled_events are set to, by updating my local gem to log those attributes. I'd also log the result of calling super, in case it returns some kind of error response. Or using the ruby debugger.

我将通过更新本地gem来记录这些属性来检查@disable_all_events和@disabled_events设置的内容。我还会记录调用super的结果,以防它返回某种错误响应。或者使用ruby调试器。

I know you said other calls were working, so perhaps it's just this call which is getting dropped silently.

我知道你说其他电话正在工作,所以也许只是这个电话会被悄悄地丢掉。

Another option would be to figure out how to get a proxy in the middle of the HTTP requests (i.e. Charles).

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

#1


0  

Any idea why the session event would not be firing in this instance? How should I go about further debugging this?

知道为什么会话事件不会在这个实例中触发?我该如何进一步调试呢?

To answer the debugging side of your question, I'd start by ensuring I'm sending this event in the right way.

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

Add logging everywhere! In mengpaneel's track method:

到处添加日志!在mengpaneel的跟踪方法中:

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

  properties = @properties.merge(properties)

  super(@distinct_id, event, properties)
end

I'd check what @disable_all_events and @disabled_events are set to, by updating my local gem to log those attributes. I'd also log the result of calling super, in case it returns some kind of error response. Or using the ruby debugger.

我将通过更新本地gem来记录这些属性来检查@disable_all_events和@disabled_events设置的内容。我还会记录调用super的结果,以防它返回某种错误响应。或者使用ruby调试器。

I know you said other calls were working, so perhaps it's just this call which is getting dropped silently.

我知道你说其他电话正在工作,所以也许只是这个电话会被悄悄地丢掉。

Another option would be to figure out how to get a proxy in the middle of the HTTP requests (i.e. Charles).

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