首页 > 解决方案 > 如何在 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 的访问。

我究竟做错了什么?

标签: javascriptgoogle-analyticscustom-dimensions

解决方案


似乎这在一段时间后自行解决了。

但是,我发现为不同的自定义维度创建多个事件似乎并没有注册这些值。

所以,这就是我最终的结果:

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       
    }
?>

这现在似乎按预期工作。


推荐阅读