如何在Rails的Google Universal Analytics跟踪代码中设置USER_ID? [英] How do I set USER_ID in Google Universal Analytics tracking code in Rails?
问题描述
这是我基于Rails的应用程序的Universal Analytics跟踪代码. (可以在 _analytics.html.erb 中找到,该文件作为部分加载到 application.html.erb 中)
Here's the Universal Analytics tracking code for my Rails-based app. (It can be found in _analytics.html.erb, which is loaded as a partial in 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>
问题:如何使用Ruby设置USER_ID?以下是对的吗?
Question: how do I set USER_ID using Ruby? 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?
推荐答案
按照Google Analytics(分析)中给出的指令
As per directive given in google analytics
您应该这样做
ga('create', 'UA-XXXX-Y', { 'userId': <%= current_user.id %> })
并恰好在关闭head标签之前将其链接到application.html.erb
and link it in application.html.erb exactly before closing of head tag
使用此链接作为参考 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
这篇关于如何在Rails的Google Universal Analytics跟踪代码中设置USER_ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!