Search code examples
google-analyticsgoogle-tag-manager

How get client id in Google Analytics 4


I use Google Tag Manager. In documentation Google https://developers.google.com/gtagjs/reference/api?hl=en have client_id field name, but I don't understand what needs to be added to the Google Tag Manager tag value in Fields To Set? Thanks


Solution

  • Here is the documentation: https://developers.google.com/gtagjs/reference/api?hl=en#set_examples

    Example 1 (Get):

    const clientIdPromise = new Promise(resolve => {
      gtag('get', 'DC-XXXXXXXX', 'client_id', resolve)
    });
    clientIdPromise.then((client_id) => {
      console.log(client_id);
    });
    

    Example 2 (Get):

    gtag('get', 'DC-XXXXXXXX', 'client_id', function(client_id) {
      console.log(client_id);
    });
    

    Example 3 (Set) (Please note that I haven't tested this):

    gtag('set', 'client_id', 'xxxxx.xxxxx');