[英]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.