title: Getting client ID with gtag.js
body: <p>Google released <a href="https://developers.google.com/analytics/devguides/collection/gtagjs/" rel="noreferrer">gtag.js</a> a couple of months ago as the new way of tracking with Google Analytics, eventually replacing analytics.js as far as I understood. gtag.js is the default when setting up a new Google Analytics account, so the code snippet went from this:</p>
  
  <pre><code>&lt;script&gt;
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  
    ga('create', 'UA-12345678-1', 'auto');
    ga('send', 'pageview');
  &lt;/script&gt;
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>&lt;script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456789-1"&gt;&lt;/script&gt;
  &lt;script&gt;
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
  
    gtag('config', 'UA-123456789-1');
  &lt;/script&gt;
  </code></pre>
  
  <p>What I need to do, is to get the client ID with gtag.js. With the old script, I could do as follows.</p>
  
  <pre><code>ga(function(tracker) {
    let clientId = tracker.get('clientId');
  });
  </code></pre>
  
  <p>By the looks of it, gtag.js loads the same analytics.js script through Google Tag Manager, because the <code>ga</code> variable is indeed available. However, there is some difference because the <code>tracker</code> parameter is <code>undefined</code> when the callback is invoked, <a href="https://developers.google.com/analytics/devguides/collection/gtagjs/migration" rel="noreferrer">because gtag.js does not use trackers</a>, so this approach clear won't work.</p>
  
  <p>I looked through <a href="https://developers.google.com/analytics/devguides/collection/gtagjs/" rel="noreferrer">the documentation for gtag.js</a>, but I was not able to find any information on how to obtain the client ID. The documentation for analytics.js <a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie" rel="noreferrer">states not to access the cookie directly</a> to obtain the client ID, which makes sense. But is there any way to get it through the JavaScript API with gtag.js, or do I have to resort to reading the cookie for now?</p>