• I would like to know when visitors click on hastag anchor on my site. I am using the one page theme Zerif Lite.

    When a visitor on https://example.com click #aboutus it wont load a new page, but I still want it registered in Google Analytics as a virtual pageview.

    A similar question was asked here, but the suggested code doesn’t seem to do anything for me:

    <!-- Track anchor links automatically by Rob Marlbrough, https://presswizards.com/ -->
    <script type="text/javascript">
    jQuery(document).ready(function () {
    	jQuery('.main-menu ul li a').click(function(){
    		var match = jQuery(this).attr('href').match(/#\S+/);
    		__gaTracker('send', 'pageview', location.pathname + match[0]);
    	});
    });
    </script>

    Do you know how I can have GA register hashtag anchor links as page views?

    https://www.ads-software.com/plugins/google-analytics-for-wordpress/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Did you edit the CSS “.main-menu ul li a” as instructed? The CSS class needs to match your theme’s CSS structure, so when it’s clicked, it fires the jQuery event.

    If you don’t match it, it won’t work.

    Also, please do not include the top comment, that was copied from one of my client sites, so they know who added the code down the road if there are any issues, and I mistakenly copied it in.

    (Mods, please remove the top comment.)

    Thread Starter niemion

    (@niemion)

    Thank you Rob for responding so quickly.

    What would you change “.main-menu ul li a” to in the example below?

    <nav class="navbar-collapse bs-navbar-collapse collapse" id=
      "site-navigation">
        <ul class="nav navbar-nav navbar-right responsive-nav main-nav-list" id=
        "menu-httpwww-example-comfocus">
          <li class=
          "menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-62"
          id="menu-item-62">
            <a href="#aboutus">Om os</a>
          </li>
          <li class=
          "menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-64"
          id="menu-item-64">
            <a href="#contact">Kontakt os</a>
          </li>
        </ul>
      </nav>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to track hastag anchor clicks like #aboutus’ is closed to new replies.