I previously tracked clicks to open tabs / modals etc in my google analytics. I have since had to change to the new gTag code and js links are now not being tracked. How can I fix this?
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxx-5"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
// line below previously pushed js links events
gtag('event', 'pageview', { 'page': location.pathname + location.search + location.hash});
gtag('config', 'UA-xxxxxxx-5', {
'linker': {
'domains': ['example.org.au']
}
});
asked Jan 15, 2018 at 15:22
Will
4,7423 gold badges40 silver badges71 bronze badges
1 Answer 1
I fixed this.
To get the full path including the anchor on page load:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxxxxx-5', {
'page_path': location.pathname + location.search + location.hash,
'linker': {
'domains': ['example.org.au']
}
});
</script>
then to track an event triggered in JS (eg tab opening)
link = location.pathname.replace(/^[\/]/, '') + location.search + location.hash
gtag('event', 'page_view', {'page_path': link});
answered Jan 15, 2018 at 17:17
Will
4,7423 gold badges40 silver badges71 bronze badges
Sign up to request clarification or add additional context in comments.