0

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']
  }
});

1 Answer 1

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});

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.