如何使用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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++更多推荐
发布评论