title: Google Tag Manager is returning undefined from Google Analytics Legacy Client ID that exists
body: <p>I am working on a website for a client that uses Google Analytics Classic. They are not upgrading at this time. They are using Google Tag Manager and want me to get the client ID from Google Analytics Classic through GTM (which I have been able to accomplish) and then send that to analytics using GTM. I have it so that it fires a tag when it detects that ID, but when I go to send that ID using a variable I created it returns undefined 95%. </p>
  
  <p>The Tag that gets fired is my SendToAnalytics. It is Classic Google Analytics tag type. It is triggered by WindowLoadedWithValidID which fires when my variable CheckForClientID code does not return false. The code for variable CheckForClientID is:</p>
  
  <pre><code>    function() {
  
          var getClientId = function () {
              try {
                  ga(function (tracker) {
                      if (tracker != undefined) {
                           var clientId = tracker.get('clientId');
                           return clientId;
                      } else {
                          return false;
                      }
                  });
              } catch (e) {
                  if (e) {
                      return false;
                  }
              }
          }
        getClientId();
      }
  </code></pre>
  
  <p>My tag SendToAnalytics is where I am having the issue. It is a page view track type and it creates a custom variable to send to GA. The value of that variable is returning undefined 95% of the time. I have tried numerous ways to get that value. I have used code that is similar to the above, I have used:</p>
  
  <pre><code>function(){
  
   var clientID = ga.getAll()[0].get('clientId');
   return clientID;
  
  }
  </code></pre>
  
  <p>and anything else I can think of. It almost always returns undefined even though it is firing after the other trigger fires which depends on the GA script being loaded.</p>
  
  <p>I know that the GA code is asynchronous, but once this tag fires  it is because the GA script has already loaded otherwise it would continue to return false. The variable that returns undefined is confusing me because it shouldn't initialize until after this tag has been fired and therefore the GA script should have already been loaded, or is that wrong? Does GTM initialize the variable as soon it sees that the tag exists whether or not it has been fired yet? If that is the case, how would I get the value of that variable to return the clientID and not undefined?</p>