[英]How to set custom dimensions in Google Analytics
I am trying to get custom dimension to be set for and reported in Google Analytics. 我正在尝试为Google Analytics(分析)设置和报告自定义维度。 I'm unable to see any passed values in any report.
我无法在任何报告中看到任何传递的值。
In Google Analytics, for my property, in the settings, under 'Custom dimensions', I've added a custom dimension called 'userId' with index 1 (and scope 'User'). 在Google Analytics(分析)中,对于我的媒体资源,在设置中的“自定义维度”下,我添加了一个名为“ userId”的自定义维度,索引为1(范围为“用户”)。
Based on this page , my tracking script in my site's pages header is this: 根据此页面 ,我网站页面标题中的跟踪脚本是这样的:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXX', {
'custom_map': {
'dimension1': 'userId'
}
});
var dimensionValue = <?php print $userId; ?>;
gtag('event', 'userId_dimension', {'userId': dimensionValue});
($userId is an integer.) ($ userId是整数。)
In a report like 'Behaviour' -> 'Site content' -> 'Pages', I'm able to select a secondary dimension, including 'userId'. 在“行为”->“站点内容”->“页面”之类的报告中,我可以选择一个辅助维度,包括“ userId”。 When selecting this secondary dimension, the resulting table has no rows.
选择此次级维度时,结果表没有行。 Even though, when inspecting my pages, the tracking code appears to pass the userId, and Google Analytics records visits that pass the userId.
即使在检查我的页面时,跟踪代码似乎传递了userId,并且Google Analytics(分析)记录了传递了userId的访问。
What am I doing wrong? 我究竟做错了什么?
It seems this resolved itself after a while. 似乎过了一会儿,问题解决了。
However, I found that creating multiple events for different custom dimensions did not seem to register those values. 但是,我发现为不同的自定义维度创建多个事件似乎并没有注册这些值。
So, this is what I ended up with: 因此,这就是我最终得到的结果:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXX-1', {
'custom_map': {
'dimension1': 'userId',
'dimension2': 'userName',
'dimension3': 'clientId',
'dimension4': 'clientName'
}
});
<?php
if (isset($_SESSION["user"])) {
$gt["userId"] = $_SESSION["user"]["iID"];
$gt["userName"] = addslashes($_SESSION["user"]["tFirstName"]." ".$_SESSION["user"]["tLastName"]);
$gt["clientId"] = 0;
$gt["clientName"] = '';
if (isset($_SESSION["user"]["clients"])) {
if (isset($_SESSION["user"]["clients"][0])) {
$gt["clientId"] = $_SESSION["user"]["clients"][0]["iID"];
$gt["clientName"] = addslashes($_SESSION["user"]["clients"][0]["tName"]);
}
}
?>
gtag('event', 'add_dimensions', {
'userId' : <?php print $gt["userId"]; ?>,
'userName' : '<?php print $gt["userName"]; ?>',
'clientId' : <?php print $gt["clientId"]; ?>,
'clientName' : '<?php print $gt["clientName"]; ?>'
});
<?php
}
?>
This now appears to work as expected. 现在,这似乎可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.