gpt4 book ai didi

ruby-on-rails - 如何使用Google Analytics(分析)追踪Rails中的使用者

转载 作者:行者123 更新时间:2023-12-03 17:25:30 25 4
gpt4 key购买 nike

我正在使用以下Google Analytics(分析)代码来跟踪我的Ruby on Rails 4应用中的用户

    // ga('create', 'UA-1234567-1', 'auto');
ga('create', 'UA-1234567-1', 'auto', { 'userId': <%= current_user.id %> });
// ga('set', '&uid', <%= current_user.id %>); // Set the user ID using signed-in user_id.
ga('send', 'pageview');


但我得到了错误

undefined method `id' for nil:NilClass


为线

ga('create', 'UA-1234567-1', 'auto', { 'userId': <%= current_user.id %> });


我正在跟踪该线程 How do I set USER_ID in Google Universal Analytics tracking code in Rails?和官方文档 https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id,但不确定出了什么问题,因为它几乎是相同的代码。

最佳答案

仅为登录用户添加检查条件-

<% if current_user.present? %>
<script>
//analytics stuffs

ga('create', 'UA-1234567-1', 'auto', { 'userId': <%= current_user.id %> }); // Set the user ID using signed-in user_id.
ga('send', 'pageview');
</script>
<% end %>


要么

 <script>
//analytics stuffs

ga('create', 'UA-1234567-1', 'auto', { 'userId': <%= current_user.present? ? current_user.id : nil %> }); // Set the user ID using signed-in user_id.
ga('send', 'pageview');
</script>

关于ruby-on-rails - 如何使用Google Analytics(分析)追踪Rails中的使用者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54984999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com