Arjun
Arjun

Reputation: 5

Multiple google analytics to track AJAX request

I am updating a page on my website using AJAX but for some reason Google Analytics is not registering the page visit. What I need to do so that Google Analytics update the same?

I have checked in stackoverflow for same but all of them are using single tracking ids. But I am not getting how can I use this for multiple tracking?

I have 7 tracking ids: UA-XXXXXXXX-(1-7) which I need to update depending on a value returned by my AJAX query.

I am using latest GA code:

<script>
  (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','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-XXXXXXXX-X', 'auto');
  ga('send', 'pageview');
</script>

What I need to do to track this properly?

Upvotes: 0

Views: 391

Answers (1)

nyuen
nyuen

Reputation: 8907

If you are implementing multiple trackers on the same site, on the same pages, then you need to make sure that you distinguish each tracker and the functions you are calling.

cf. https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers#working_with_multiple_trackers

// example
ga('create', 'UA-XXXXX-1', 'auto'); // main tracker
ga('send', 'pageview');

// example
ga('create', 'UA-XXXXX-2', 'auto', 'tracker2'); // secondary tracker
ga('tracker2.send', 'pageview');
ga('tracker2.set', 'page', 'some/other/page');

// example
ga('create', 'UA-XXXXX-2', 'auto', 'tracker3'); // tertiary tracker
ga('tracker3.send', 'pageview');
ga('tracker3.send','event', 'blah, 'bla');

// etc.

So for each tracker, you need to make sure you specify a name (or none for the default), and then when you call the specific method (eg. 'send', 'set', etc.), then you need to include that tracker name.

Upvotes: 1

Related Questions