Toll Free: 1-877-695-7388

GTA: (647) 699-2838

Search Engine People
  • SEO
  • SEM
  • CRO
  • Display
  • Blog
  • Why Us
  • Contact
  • Join Our Team
  • Get A Quote

Toll Free: 1-877-695-7388

GTA: (647) 699-2838

How To Track Clicks On #Anchors in Google Analytics

Shockley Au | December 7th, 2010
Tweet89
Share32
Share
Pin
121 Shares

Last week, a question from a client landed in my inbox, about whether or not it was possible to track clicks on anchors/fragments. Good question! While the vanilla out-of-the-box Google Analytics doesn't do this, a little bit of customization with JavaScript can do the trick.

What Are #Anchors?

In case you're not excited yet, let's do a quick #Anchors 101.

Many websites use named anchors (referenced by using a hashtag, #) to quickly link to specific areas on a webpage. Examples of named anchors you may have seen, are those "Back to Top" links, which link to the top of the page you're currently on.

The code to do this is normally <a href="index.htm#top">Back to Top</a>.

In Analytics, this will trigger a pageview for index.htm by default - not for index.htm#top.

Anchors can also be used for simple navigation to lower sections of a page. Here's one that links to the screenshot section of this blog post.

JavaScript Code

The key to capturing the hashtag is to use the location.hash property.

Old traditional tracking code:

var pageTracker = _gat._getTracker("UA-XXXXX-X");
pageTracker._trackPageview(location.pathname + location.search + location.hash);

New asynchronous tracking code:

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview', location.pathname + location.search + location.hash]);

#Anchors in Google Analytics

You should now be able to see clicks on named anchors in the Content Drilldown report.

Anchors in Content Drilldown

Without the customized code, we would see 33 pageviews for index.htm (18+8+4+3), and no #section anchors anywhere.

Tracking clicks on anchors allows you to: better understand visitor behaviour; see how they interact with your site; and see what content works.

Tweet89
Share32
Share
Pin
121 Shares
Posted in PRTagged analytics, google analytics, How To

About the Author: Shockley Au

Shockley combines digital marketing and innovative technologies to help businesses succeed. He is certified in Google AdWords, Google Analytics, and Bing Ads. Follow him on Twitter @ShockleyAu.

Shockley Au

14 thoughts on “How To Track Clicks On #Anchors in Google Analytics”

  1. Tiggerito says:
    December 7, 2010 at 9:21 am

    Nice trick.

    This would track when the hash is in a link or directly typed in to a browser.

    It would not pick up proper internal page anchor links (like href=”#top”) or javascript that causes hash changes to the browsers URL. This is because they don’t cause the page to reload and trigger your code.

    In those cases you would want to add javascript into an onclick attribute or the dynamic code, that included some _trackPageview code.

  2. Shockley Au says:
    December 7, 2010 at 11:38 am

    @Tiggerito: Thanks, and yes good point. This was a recommendation we gave when the anchor was linked to from another internal page, which would work.

  3. amanda schreiner says:
    December 8, 2010 at 2:34 pm

    Hello,

    I added this code to our google analytics and it is not tracking the anchor tag links.

    Could the error be because we are using a CMS that makes links with wild URLS? how does this actually work calling the location.hash?

    I wish i could explain this better. We are using Red Dot/Open Text.

  4. Nick Thomas says:
    December 9, 2010 at 4:01 am

    Hii tried many a times but still cldn’t track clicks on the same do we have any other way too….?????

  5. Nuvenus Chovendus says:
    December 9, 2010 at 5:20 am

    Shockley, I do not know if the question is timely, but what is the direct influence of the anchor on the placements? Is there any special treatment for the search engines? If so, how this influences the data in analytics?

    Thanks,
    Nuvenus Chovendus
    .-= Nuvenus Chovendus recently posted: Fab comenta sobre o Nuvenus e os Voos =-.

  6. Tiggerito says:
    December 9, 2010 at 3:06 pm

    I new that would happen!

    See if this works

    <a href=”#somewhere” onclick=”_gaq.push([‘_trackPageview’, location.pathname + location.search + ‘#somewhere’]);”>somewhere</a>

    Or you may want to track it as an event.

    <a href=”#somewhere” onclick=”_gaq.push([‘_trackEvent’, ‘anchor’, ‘click’, ‘somewhere’]);”>somewhere</a>
    .-= Tiggerito recently posted: The Most Popular SEO &amp Website Promotion Articles =-.

  7. Shockley Au says:
    December 13, 2010 at 2:02 pm

    @Amanda, @Nick: Has the issue been resolved? If not, please post a link to your website and I can take a quick look.

    @Nuvenus: I’m not sure I understand your question, but Google seems to simply ignore anchors and index the page normally as if it didn’t have any.

    @Tiggerito: Thanks! Also remember to be careful with inflating pageviews in some instances. You should look at the way you’re using #anchors and decide what makes sense for your site.

  8. Tiggerito says:
    December 13, 2010 at 5:48 pm

    @Shockley: I agree about the pageview inflation issue. That’s why I would track them as events.

    Google Analytics ignores anchor links because it does not see them. In reality a browser makes a request without the anchor (#) part then locally moves to its position when the page is returned. Google code tracks the URL in the request, which is anchor free.

    Also, when you have an in page anchor link, it will not trigger a new request. It just locally moves to that new position. Your browsers Address bar also gets updated but Google’s code is not re-run to capture the click.

    Hence the code to manually trigger a page hit or event.
    .-= Tiggerito recently posted: The Most Popular SEO &amp Website Promotion Articles =-.

  9. Shockley Au says:
    December 15, 2010 at 4:10 pm

    @Tiggerito: Good info, thanks! 🙂

  10. joe limon says:
    April 19, 2011 at 10:23 am

    I think I may have found my answer in the threadCan a url contain both a page anchor and campaign tracking codes? at the Google Help forum

  11. Dave says:
    February 12, 2013 at 9:14 am

    Trying to understand the # anchor and Google analytics

    I have the following at the top of my index.html file

    var _gaq = _gaq || [];
    _gaq.push([‘_setAccount’, ‘UA-XXXXXXXX-X’]);
    _gaq.push([‘_trackPageview’]);
    (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
    var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
    })();

    Then on the intro page I have a menu bar which goes to different pages i.e.

    ‘
    ‘
    ‘
    ‘
    ‘HOME PAGE
    ‘
    ‘
    ‘ABOUT US
    ‘
    ‘
    ‘
    ‘Future Use

    ‘
    ‘
    ‘RESULTS
    ‘
    ‘

    Then I have the individual pages on the main index.html file i.e.

    ‘
    ‘
    ‘
    ‘
    ‘
    ‘Test Website

    Where and what do I use as the code to track these

    ‘#!/page_HOME

    ‘#!/page_SERVICES

  12. Jeremy says:
    November 15, 2013 at 2:53 am

    I’m still trying to understand this. The ‘_setAllowAnchor’, true seems to be about replacing the delimiter for campaigns. What I want to do is track internal references to #anchors on the site.

    For that, I need + location.hash

    Is this right?

Comments are closed.

Recent Posts

  • Maximizing Your E-Commerce Sales:
    A CRO Audit Guide
  • Movin’ On Up! Why Migrating to Google Analytics 4 (GA4) Should be a Priority
  • A Year in Review: The Digital Marketing Trends That Defined 2021
  • The Basics of Video Marketing
  • Just How Much Do Google Reviews Impact Your SEO Ranking?

Categories

  • Analytics & ROI Analysis
  • Company News
  • Content
  • Conversion Optimization
  • CRO
  • Display Advertising/RTB
  • Email Marketing
  • En Español
  • En Français
  • Inbound Marketing
  • Lead Nurture & Marketing Automation
  • Local Search
  • Marketing
  • Mobile
  • Partnership Marketing
  • PPC
  • PR
  • SEO
  • Social Media Marketing
  • Web Design

Additional Posts

Web Writing and Writing for Print: It’s Not the Same Thing

December 7th, 2010 | by Gabriella Sannino

Sure You Can Fake It In Social Media – So Don’t

December 6th, 2010 | by Ruud Hein
New Education and Work Section

The Great Facebook Facelift of 2010

December 6th, 2010 | by Caitlin Melvin

LET'S TALK

Need more information or want to get in touch?

Get in touch!
  • SEO
  • SEM
  • Display
  • Blog
  • Why Us
  • Join Our Team
  • Contact Us
  • Local SEO
  • Small Business SEO
  • Enterprise SEO
  • International SEO

LOCATION

1305 Pickering Parkway,
5th Floor Pickering, L1V 3P2

PHONE

Toll Free: 1-877-695-7388
Greater Toronto Area: (647) 699-2838

Social

© Search Engine People Inc. 2023 – Canada’s Top Digital Agency
© SEP 2023 – A Search Engine People Company | Privacy Policy

Search Engine People