Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Dimensions with gtag.js

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!

like image 334
Robba Avatar asked Jan 31 '18 10:01

Robba


People also ask

What is the difference between GTAG js and analytics js?

Unlike analytics. js, gtag. js doesn't use trackers to send data to Google Analytics. It sends data to Google Analytics properties identified by their IDs set by the config command.


2 Answers

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.

like image 64
alexion Avatar answered Oct 12 '22 13:10

alexion


I experienced a similar issue. It was related to not re-sending the custom dimensions again in custom_map when tracking page views. You can view my answer here on how I solved the problem.

like image 27
raman Avatar answered Oct 12 '22 12:10

raman