在网页上搜索答案!
我需要将谷歌分析的"用户ID"功能与我的wordpress网站集成。请参阅:https://support.google.com/analytics/answer/3123662?hl=en
我一直在寻找一个专用的插件,但我能找到的最接近的东西是https://en-gb.wordpress.org/plugins/google-analytics-adder/但由于缺乏指导,我没有解决办法。
目前已经安装了GA,并且使用Yoast的插件运行良好,但没有这个用户ID函数。所以我无法识别注册用户的交互。
我确实理解WP为每个用户生成一个ID,我的问题更多的是关于如何将其发送给GA(如果发送WP的用户ID是最佳做法?)
不确定wordpress是否能够"将您自己的身份验证系统生成的唯一ID作为用户ID的值发送到Analytics"。
感谢您的帮助!
使用这个。请注意,它只适用于登录用户,不确定这是否是你想要实现的:
ga('set', 'userId', <?php echo get_current_user_id(); ?>);
您可以使用JavaScript API中的_setCustomVar方法来提供当前用户的用户名。据我所知,Wordpress没有GA插件支持这一点,所以你需要将你的跟踪代码直接放入主题中,或者为它编写一个自定义插件。然后,自定义变量将在谷歌分析中显示为一个片段。要获取当前用户,可以使用wp_get_current_user API调用。
对于通用分析版本:
<?php
if (is_user_logged_in()) {
$user = wp_get_current_user();
$userName = $user->user_login;
}
?>
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXX-1', 'auto');
<?php if (isset($userName)) : ?>
ga('set', 'userId', <?php echo(json_encode($userName)); ?>); // Set the user ID using signed-in user_id.
<?php endif; ?>
ga('send', 'pageview');
</script>
Cross Posted from:16578410