Feed client id into your Google Analytics data


Google Analytics identifies sessions through the use of an anonymous tracker code (cookie) that lives in the end-user's browser. You can feed this tracker code (called clientId) back into Google Analytics via a custom dimension. When Needl extracts data from Analytics, it can make use of the clientId in a number of ways:

  • Hit-level data can be leveraged (has user visited certain pages)
  • More dimensions can be extracted to data-warehousing than would otherwise be possible
  • Other custom dimensions can be extracted
  • Sessions can be linked together via clientId (think attribution modelling)

And that's not to mention machine learning!

Step 1 - setup custom dimensions in your Google Analytics account

While we're here, let's also add in a timestamp to aid hit-level data analysis.

Login and go to the Admin section, make sure your account and property is set correctly then click on Custom definitions and then Custom dimensions.

Add two new custom dimensions and save - so it looks like this:

Step 2 - feed in the custom dimensions

You will need to modify your site's JavaScript that fires the Google Analytics tracking. This depends on whether you use the new gtag.js or the older analytics.js:

gtag.js


            gtag('config', 'UA-YOURTRACKING-ID', {
                'custom_map': {
                    'dimension1': 'client_id',
                    'dimension2': 'hit_time'
                },
                'hit_time': Date.now()
            });
        

Thanks to Simo Ahava: https://www.simoahava.com/analytics/add-clientid-to-custom-dimension-gtag-js/

Google Analytic's gtag reference: https://developers.google.com/analytics/devguides/collection/gtagjs/

anayltics.js

You will probably have something like this:


            ga('create', 'UA-YOURTRACKING-ID', 'auto');
            ga('send', 'pageview');
        

Change the above to the following:


            ga('create', 'UA-YOURTRACKING-ID', 'auto');
            ga(function(tracker) {
                var clientId = tracker.get('clientId');
                if (!clientId)
                    clientId = '(not set)';
                var hitTime = Date.now();
                tracker.send('pageview', {
                    'dimension1': clientId,
                    'dimension2': hitTime
                });
            });
        

Further steps

Interested in more custom dimensions? Checkout Simo Ahava's excellent blog: https://www.simoahava.com/analytics/13-useful-custom-dimensions-for-google-analytics/

Book a demo for free!