javascript - 如何在 Google Analytics 中设置自定义维度
问题描述
我正在尝试在 Google Analytics 中设置和报告自定义维度。我无法在任何报告中看到任何传递的值。
在 Google Analytics(分析)中,对于我的属性,在设置中的“自定义维度”下,我添加了一个名为“userId”的自定义维度,索引为 1(范围为“用户”)。
基于此页面,我网站页面标题中的跟踪脚本是这样的:
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 是一个整数。)
在“行为”->“网站内容”->“页面”之类的报告中,我可以选择二级维度,包括“用户 ID”。选择此二级维度时,生成的表没有行。即使在检查我的页面时,跟踪代码似乎传递了 userId,并且 Google Analytics 记录了传递 userId 的访问。
我究竟做错了什么?
解决方案
似乎这在一段时间后自行解决了。
但是,我发现为不同的自定义维度创建多个事件似乎并没有注册这些值。
所以,这就是我最终的结果:
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
}
?>
这现在似乎按预期工作。
推荐阅读
- angular - 跨项目共享 Angular 环境作为 npm 包
- angular - 在主应用程序组件中的对象的材质对话框中显示数据?
- javascript - 关键事件上的 event.preventDefault 在 Android 上的 Chrome 中不起作用
- c# - 使用异步/等待的彗星式 API 消耗
- java - 在 Java JSch Shell 中获得两次 shell 提示
- python - ExponentialDecay 位于具有嵌入权重的不同图中
- scala - Scala Map 上是否明确定义了 take 函数?
- spring - 过滤器(在 Spring Boot Web 应用程序中)能否根据所有定义的 RequestMapping 验证请求 URL?
- javascript - HTML文件中的Javascript不会输出变量
- r - 我无法在 R 中拼接降价,接收和错误;我该如何解决这个问题?更新: