1

I have code straight from the google analytics docs. For some reason get an error, only sometimes undefined is not an object (evaluating 'tracker.get') what does this mean? and what is the difference between this type of error and an error that says property 'get' does not exist on undefined ?

private hasDefaultTracker(): boolean {    
 var clientId;
    window.ga(function(tracker) {
        clientId = tracker.get('clientId');
    });
    return clientId != null;
}
David SS
  • 43
  • 1
  • 6
  • 2
    Are you sure this code is straight from the google analytics code? Can you provide a reference to the relevant document? – Alon Eitan Sep 08 '19 at 09:53
  • Possible duplicate of [how to get the Google analytics client ID](https://stackoverflow.com/questions/20053949/how-to-get-the-google-analytics-client-id) – Alon Eitan Sep 08 '19 at 09:56
  • This look like standard, [how to return from asynchronous function](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323) – jcubic Sep 08 '19 at 10:00
  • Maybe you can find answer here: [trackingid-and-clientid-with-google-tag-manager](https://stackoverflow.com/questions/27189563/trackingid-and-clientid-with-google-tag-manager) – Software Developer Sep 08 '19 at 10:00
  • from the docs `ga(function(tracker) { // Logs the client ID for the current user. console.log(tracker.get('clientId')); });` – David SS Sep 08 '19 at 10:26
  • @jcubic can you elaborate? – David SS Sep 08 '19 at 10:35
  • `ga(function(tracker) {` is async function you can't assign the value that's inside and check outside, the value is only inside the function, because it runs after the check. it's like `setTimeout`. – jcubic Sep 08 '19 at 10:37
  • also your code need to be inside of the function, otherwise it will be syntax error. You can't have return at top level. – jcubic Sep 08 '19 at 10:38
  • @jcubic while debugging, the `clientId = tracker.get('clientId');` line is called before the return – David SS Sep 08 '19 at 12:50
  • Maybe you proved wrong data when you initialized GA. – jcubic Sep 08 '19 at 14:55
  • I'm facing the same issue, inside my callback function i'm getting the tracker as undefined, any idea how to solve it? – Alfredo Esteban Hernndez Dvalo Apr 30 '20 at 18:00

0 Answers0