I'm following this:

But in my real code, I've used google tag manager to manage google analytics. So I have just one script in the head of my website which belongs to gtm:

new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],

ga('create', 'UA-124402662-1', 'auto'); // Uncaught ReferenceError: ga is not defined

As you can see, I've added ga('create', 'UA-124402662-1', 'auto') right after that google tag manager (the same as the tutorial I've linked in the first line of my question). And I have a ga('send', 'pageview', newURL); code which will be executed when the page changes using ajax.

The problem is it throws: (as I've commented in code above)

Uncaught ReferenceError: ga is not defined

How can I fix it? Someone told me no need to add ga('create', 'UA-124402662-1', 'auto'); at all, and gtm will handle it all. But in my tests:

  • When I open my website (homepage), and run ga('send', 'pageview', "/adv"); in the console, nothing changes in google analytics and it still points to the homepage.

  • When I open my website (homepage), and run ga('create', 'UA-124402662-1', 'auto'); and then run ga('send', 'pageview', "/adv");, then the google analytics will be changed to the /adv (not homepage anymore). So I believe ga('create', 'UA-124402662-1', 'auto'); is needed. But why it is undefined?

  • What triggers the page change? – Bronwyn V Oct 22 '18 at 3:11
  • You should be doing this via GTM because GTM is handling the GA tracking. Following articles are a good read and will possibly provide you with some ideas as to how to go about it. Virtual Pageviews in GTM lunametrics.com/blog/2014/09/10/… GTM History Listener simoahava.com/analytics/google-tag-manager-history-listener – Bronwyn V Oct 22 '18 at 3:18
  • I second Bronwyn - is there a reason you want to do this via ga() instead of using the GTM setup you already have? Or would doing it via GTM be fine if you had instructions? – Reve Oct 24 '18 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.