Custom Dimensions with gtag.js

Robba picture Robba · Jan 31, 2018 · Viewed 7.9k times · Source

I'm trying to get custom dimensions to work in Google Analytics using the 'new' gtag library. I've added a new custom dimension to the property in the GA administration section:

Username image

Next I've configured a mapping as per the (specs) by adding this snippet:

gtag('config', '<My GA tag here>', {
    'send_page_view': false,
    'custom_map': {
        'dimension1': 'my_username'
    }
});

Next when the user logs in I set the my_username property:

gtag('set', 'my_username', '<username of logged in user>');

And expect it to be pushed for every event:

gtag('event', 'login');
gtag('event', 'page_view', {'page_path': calculated_current_spa_location});
etc

Unfortunately, while the events show up in Analytics when setting the secondary dimension to Username none of the events seem to have the value.

I've also tried setting the property directly when registering the event:

gtag('event', 'page_view', {
    'page_path': calculated_current_spa_location,
    'my_username', '<username of logged in user>'
});

But to no avail :(

Any help would be greatly appreciated, thanks!

Answer

alexion picture alexion · Nov 22, 2018

Try this:

gtag('config', 'GA_TRACKING_ID', {
    'custom_map': {'dimension1': 'my_username'}
});
gtag('event', 'my_username_event', {'my_username': '<username of logged in user>'});

Should work fine and should give you Secondary dimension data.