Question

I'm trying to track the anchors on links using Google Analytics Universal Tracking. Most of the posts I've seen are related to the old tracking system, so I'm trying to figure out the best method to track URL's that have anchors on them as such:

<a href="http://mediacause.org/#about">About</a>

What would I need to change in the Analytics code in order to track the anchor tags when they're clicked?

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXX-X', 'website.com');
  ga('send', 'pageview');

</script>

I've looked into the following posts on SO as well but don't think their solutions are related or they're outdated:

This post is similar, but when using a Wordpress site, we don't have the option to use "onclick" -- is there an option to enable tracking from GA directly? How to track Google Analytics anchor clicks using Universal Analytics

Tracking Hashtags & Querystrings in GA

Was it helpful?

Solution

Well I was able to do a work around and it seems to be tracking it now.

I added the following code to my footer and it works well:

jQuery(document).ready(function () {    
    var hashtag = /#\S+/
    jQuery('.menu li a').click(function(){
        var match = jQuery(this).attr('href').match(hashtag);
        ga('send', 'pageview', '/' + match[0]);
    })
});

If anyone has any better suggestions, I'm still willing to try it out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top