1

All the pages on my website are loaded on ajax request, so they don't have any URLs attached to them other than the domain URL. I want to track each page view in Google analytics. I have tried adding script like this

    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-1234567-1"</script>>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-1234567-1');
</script>

But this tracks only home page, How can I make google analytics track my other pages?

None
  • 5,582
  • 21
  • 85
  • 170
  • GA calculates pageviews based on this _location.pathname_ property. So, if there is no change in location.pathname values, all pages are recorded under a single record which is the the domain URL – dp.js Jul 29 '20 at 15:38
  • @dp.js Yap, is there a way to overcome this? – None Jul 29 '20 at 15:43
  • Does this answer your question? [Google Analytics: How to track pages in a single page application?](https://stackoverflow.com/questions/9628547/google-analytics-how-to-track-pages-in-a-single-page-application) – Vinay Jul 29 '20 at 17:06

1 Answers1

3

You can track that pages as virtual pageviews adding a code like this:

gtag('config', 'UA-XXXXXXXXX-1', {'page_path': path});

Where path will be the name you want to give to the page, like the example in the documentation:

https://developers.google.com/analytics/devguides/collection/gtagjs/single-page-applications

Josh Davenport-Smith
  • 5,456
  • 2
  • 29
  • 40
Michele Pisani
  • 13,567
  • 3
  • 25
  • 42