My google analytics script is like this
< !--Global site tag(gtag.js) - Google Analytics-- >
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x"></ script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxxxxx-x', { 'page_path': curpath });
</script>
Thiw works fine except Avg. Page Load Time
is not getting logged in analytics (always shows as zero). What am I missing here?
Note: My site works on ajax calls with single URL
You are missing the necessary information from the browser, since you use AJAX calls.
Google Analytics uses the Navigation Timing API, which is quite reliable, since the actual measurement is done by the browser and the values can be read as properties of the window.performance.timing object.
Calculate the total page load time
const perfData = window.performance.timing;
const pageLoadTime = perfData.loadEventEnd - perfData.navigationStart;
Your Ajax call does not populate those values (which in a way makes sense, since the corresponding DOM events are not populated in an AJAX call), so GA cannot log a page load time.
You can do custom user timings in GA. They will be limited to a sample of 1% of your calls max, and your averages will be extrapolated from there. You would pass in the delta between the start of your Ajax call and the point at which the response is rendered.