I'm very happy with this website design. The design isn't my work, but the backend, based on Umbraco is.

I decided to add Google Analytics, which normally works just fine, but because this site loads the content via AJAX, the change of page doesn't get recorded.

Thankfully, there is a way to solve this. In the script where I call the AJAX request to get the content, I just add the following code in:-

var analyticspage = '/' + url;
ga('set', 'page', analyticspage);
ga('send', 'pageview');

This takes the URL (e.g. Home) places a slash in front to match the normal URL and then this sends the details to Google Analytics. Easy. Done.

blog. ABOUT EVERYTHING

Tracking Ajax Calls with Google Analytics

I'm very happy with this website design. The design isn't my work, but the backend, based on Umbraco is.

I decided to add Google Analytics, which normally works just fine, but because this site loads the content via AJAX, the change of page doesn't get recorded.

Thankfully, there is a way to solve this. In the script where I call the AJAX request to get the content, I just add the following code in:-

var analyticspage = '/' + url;
ga('set', 'page', analyticspage);
ga('send', 'pageview');

This takes the URL (e.g. Home) places a slash in front to match the normal URL and then this sends the details to Google Analytics. Easy. Done.

About author

Tim Almond is a software consultant specialising in web applications for the ASP.NET stack.