我正在寻找一种方法将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错误的功能?
答案 0 :(得分:1)
我找到了解决方案。这是我现在在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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
答案 1 :(得分:0)
自定义维度与互动点击一起发送。您只能在跟踪网页浏览后设置维度(并且不发送其他交互),因此永远不会发送自定义维度。在创建跟踪器之后但在查看网页浏览之前放置自定义代码,这应该可以正常工作。