如何使用ClientId将Google Analytics跟踪代码实施到PHP页脚?(How to implement Google Analytics tracking code with Clie

编程入门 行业动态 更新时间:2024-10-27 15:19:59
如何使用ClientId将Google Analytics跟踪代码实施到PHP页脚?(How to implement Google Analytics tracking code with ClientId into PHP footer?)

我正在寻找一种方法将Google创建的ClientId集成到Google Analytics中的自定义维度中。 我找到了说明,但它对我不起作用。 我不确定如何正确安装跟踪代码。

以下是要实现的代码:

ga(function(tracker) { var clientId = tracker.get('clientId'); ga('set', 'dimension2', clientId); });

以下是我当前的Google Analytics跟踪代码:

<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-XXXXXXXXXXX', 'auto'); ga('send', 'pageview'); ga(function(tracker) { var clientId = tracker.get('clientId'); ga('set', 'dimension2', clientId); }); </script>

知道什么是错的吗? 我是否实现了ClientId错误的功能?

I am looking for a way to integrate ClientId created by Google into custom dimension in Google Analytics. I have found the instructions, but it is not working for me. I am not sure about how to install the tracking code correctly.

Here is the code to implement:

ga(function(tracker) { var clientId = tracker.get('clientId'); ga('set', 'dimension2', clientId); });

Here is my current Google Analytics tracking code:

<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-XXXXXXXXXXX', 'auto'); ga('send', 'pageview'); ga(function(tracker) { var clientId = tracker.get('clientId'); ga('set', 'dimension2', clientId); }); </script>

Any idea what is wrong? Did I implement the function which gets ClientId wrong?

最满意答案

我找到了解决方案。 这是我现在在Wordpress自定义functions.php文件中使用的代码。

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ GOOGLE ANALYTICS START ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // google analytics userid variable function google_analytics_tracking_code(){ global $userId; $userId = get_current_user_id(); if (!isset($userId)) { $userId = $_SESSION['xxx']; } ?> <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'); <?php // New Google Analytics code to set UserId and ClientId. if (isset($userId)) { $gacode = "ga('create', 'xxx', 'auto', {'userId': $userId});"; echo sprintf($gacode, $userId); } else { $gacode = "ga('create', 'xxx', 'auto');"; echo sprintf($gacode); }?> ga('set', 'dimension1', '<?php echo $userId; ?>'); // Get google client id var ga_client_id = null; ga(function(tracker) { ga_client_id = tracker.get('clientId'); ga('set', 'dimension2', ga_client_id); }); // Set ramdom session id var rand_session_id = null; rand_session_id = new Date().getTime() + '.' + Math.random().toString(36).substring(5); ga('set', 'dimension3', rand_session_id); // Get local time as ISO string with offset at the end var hit_timestamp = null; var now = new Date(); var tzo = -now.getTimezoneOffset(); var dif = tzo >= 0 ? '+' : '-'; var pad = function(num) { var norm = Math.abs(Math.floor(num)); return (norm < 10 ? '0' : '') + norm; }; hit_timestamp = now.getFullYear() + '-' + pad(now.getMonth()+1) + '-' + pad(now.getDate()) + 'T' + pad(now.getHours()) + ':' + pad(now.getMinutes()) + ':' + pad(now.getSeconds()) + '.' + pad(now.getMilliseconds()) + dif + pad(tzo / 60) + ':' + pad(tzo % 60); ga('set', 'dimension4', hit_timestamp); ga('send', 'pageview'); </script> <?php } //add_action('wp_login', 'google_analytics_tracking_code', 10, 2); // include GA tracking code before the closing body tag add_action('wp_footer', 'google_analytics_tracking_code', 10, 2); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ GOOGLE ANALYTICS END ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I found a solution. Here is the code I now use in my Wordpress custom functions.php file.

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ GOOGLE ANALYTICS START ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // google analytics userid variable function google_analytics_tracking_code(){ global $userId; $userId = get_current_user_id(); if (!isset($userId)) { $userId = $_SESSION['xxx']; } ?> <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'); <?php // New Google Analytics code to set UserId and ClientId. if (isset($userId)) { $gacode = "ga('create', 'xxx', 'auto', {'userId': $userId});"; echo sprintf($gacode, $userId); } else { $gacode = "ga('create', 'xxx', 'auto');"; echo sprintf($gacode); }?> ga('set', 'dimension1', '<?php echo $userId; ?>'); // Get google client id var ga_client_id = null; ga(function(tracker) { ga_client_id = tracker.get('clientId'); ga('set', 'dimension2', ga_client_id); }); // Set ramdom session id var rand_session_id = null; rand_session_id = new Date().getTime() + '.' + Math.random().toString(36).substring(5); ga('set', 'dimension3', rand_session_id); // Get local time as ISO string with offset at the end var hit_timestamp = null; var now = new Date(); var tzo = -now.getTimezoneOffset(); var dif = tzo >= 0 ? '+' : '-'; var pad = function(num) { var norm = Math.abs(Math.floor(num)); return (norm < 10 ? '0' : '') + norm; }; hit_timestamp = now.getFullYear() + '-' + pad(now.getMonth()+1) + '-' + pad(now.getDate()) + 'T' + pad(now.getHours()) + ':' + pad(now.getMinutes()) + ':' + pad(now.getSeconds()) + '.' + pad(now.getMilliseconds()) + dif + pad(tzo / 60) + ':' + pad(tzo % 60); ga('set', 'dimension4', hit_timestamp); ga('send', 'pageview'); </script> <?php } //add_action('wp_login', 'google_analytics_tracking_code', 10, 2); // include GA tracking code before the closing body tag add_action('wp_footer', 'google_analytics_tracking_code', 10, 2); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ GOOGLE ANALYTICS END ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

更多推荐

本文发布于:2023-07-29 21:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319755.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   代码   Analytics   Google   ClientId

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!