How get client id in Google Analytics 4

Viewed 2013
2 Answers

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() {
  console.log(client_id);
});

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

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