Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1242871
    ichibanj
    Participant

    Hi, how can I add event tracking to outbound links?

    There are plugins like this https://wordpress.org/plugins/wp-google-analytics-events/ but I don’t think they’ll integrate with Cornerstone as Cornerstone only asks for the href url.

    And this is how Google’s suggests its done and this would work for standard text links but it requires editing the href and it doesn’t look like we can do that with Cornerstone https://support.google.com/analytics/answer/1136920?hl=en

    Is there a solution you’re aware of?

    Many thanks
    Julian

    #1243243
    Rad
    Moderator

    Hi there,

    Thanks for posting in.

    This https://support.google.com/analytics/answer/1136920?hl=en only provides you the general instruction but it’s not effective if you’ll just copy it as is. Example, we can enhance it like this.

    var trackOutboundLink = function(url) {
       ga('send', 'event', 'outbound', 'click', url, {
         'transport': 'beacon',
         'hitCallback': function(){document.location = url;}
       });
    }
    
    jQuery ( function( $ ) {
    
    $('.trackOutboundLink').on('click', function( e ) {
    
    e.preventDefault(); //let's prevent page loading while the GA is sending the track request.
    
    trackOutboundLink( $(this).attr('href') );
    
    } );
    
    } );

    Assuming the GA library is already loaded or embedded to your site, then it should work. Then all you have to do is edit your link in cornerstone and add trackOutboundLink to its class name.

    Example,

    <a href="http://example.com" class="trackOutboundLink">Track Me</a>

    or if you’re using the button, then simply add trackOutboundLink to its class input.

    Hope this helps.

    #1259820
    bttrym8
    Participant

    How long after implementation would this take to show up on Google Analytics? I’ve got the script installed via the customizer and added the class to a couple different places on the site. However, they still don’t show as events in Google Analytics.

    #1260291
    Rad
    Moderator

    Hi there,

    Would you mind providing your site’s URL where this is implemented? I can’t really provide a complete solution, but I can give you some recommendation.

    Thanks!

    #1262602
    bttrym8
    Participant

    I actually ended up using tag manager and forgoing this solution. Would be a nice one to implement however if it became universal

    #1262689
    Rahul
    Moderator

    Glad you’ve fixed it!

    Let us know if we can help with anything else.

    Thanks!

  • <script> jQuery(function($){ $("#no-reply-1242871 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>