1
votes

I am trying to get custom dimension to be set for and reported in 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').

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.)

In a report like 'Behaviour' -> 'Site content' -> 'Pages', I'm able to select a secondary dimension, including '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.

What am I doing wrong?

1

1 Answers

0
votes

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.