简体   繁体   English

如何在Rails中的Google Universal Analytics跟踪代码中设置USER_ID?

[英]How do I set USER_ID in Google Universal Analytics tracking code in Rails?

Here's the Universal Analytics tracking code for my Rails-based app. 以下是基于Rails的应用的通用分析跟踪代码。 (It can be found in _analytics.html.erb , which is loaded as a partial in application.html.erb ) (它可以在_analytics.html.erb中找到,它在application.html.erb中作为部分加载)

 <script>
   (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
   (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
   m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
   })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); 

   ga('create', 'UA-44709438-1', 'auto');
   ga('set', '&uid', {{USER_ID}}); // Set the user ID using signed-in user_id.
   ga('send', 'pageview');
 </script>

Question: how do I set USER_ID using Ruby? 问题:如何使用Ruby设置USER_ID? Is the following right? 以下是对的吗?

 ga('set', '&uid', <% current_user.id %>); // Set the user ID using signed-in user_id.

If not, how can I do this? 如果没有,我该怎么做?

As per directive given in google analytics 根据谷歌分析中的指示

You should do like this 你应该这样做

ga('create', 'UA-XXXX-Y', { 'userId': <%= current_user.id %> })

and link it in application.html.erb exactly before closing of head tag 并在关闭head标签之前准确地将它链接到application.html.erb中

use this link for reference https://developers.google.com/analytics/devguides/collection/analyticsjs/user-id 使用此链接参考https://developers.google.com/analytics/devguides/collection/analyticsjs/user-id

Also you may need to add condition to decide whether the user is logged in or not 此外,您可能需要添加条件以确定用户是否已登录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby / Rails:如何插入post_id w / user_id? - Ruby/Rails: How do I insert the post_id w/user_id? 如何将我的session [:user_id]参数以及表单输入参数传递给Rails中的模型? - How do I pass my session[:user_id] parameter along with form input parameters to the model in rails? 保持Google Analytics(分析)跟踪ID的机密性Rails 4 - Keep google analytics tracking id confidential Rails 4 如何使用Rails和Postgresql将匹配的记录另存为新记录并更新user_id - How do I save a matched record as a new one and update user_id, using rails and postgresql Rails-如何在帖子评论中插入user_id? - Rails - How can I insert the user_id to a comment on a post? 如何在模型上引用“ user_id”,但命名为“ poster_id”和“ receiver_id”两个不同的属性? -滑轨3 - How do I reference a 'user_id' on a model, but named as two different attributes 'poster_id' and 'receiver_id'? - Rails 3 如何将user_id和journal_id分配给从excel导入的交易模型实例(Rails) - How do I assign user_id and journal_id to the Deal model instance imported from excel (Rails) 如何为FactoryGirl版本创建user_id? - How do I create an user_id for a FactoryGirl build? 如何为种子分配User_ID - How do I assign a User_ID to the seed 在Google Analytics(分析)跟踪代码中设置安全参数 - Set secure param in Google Analytics Tracking Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM