0

I am attempting to use Event tracking in Google Analytics. In order to achieve this I need to add an onclick attribute to certain links (The ones I was asked to track) like so:

<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com');  return false;">Check out example.com</a>

The CMS I am using does not have an option to add onclick events to the menu and I don't want to track every link in the main navigation. I only have a beginner's level understanding of javascript, but I have been looking for a way to use it to insert the onclick status above to only certain href values.

Any assistance would be greatly appreciated.

6
  • developer.mozilla.org/en-US/docs/Web/API/… Commented Oct 22, 2014 at 15:09
  • You cannot use such curly quotes - replace to " and ' accordingly. Commented Oct 22, 2014 at 15:30
  • What CMS are you using? Commented Oct 22, 2014 at 15:36
  • Roko: Thanks for the note, edit made. Commented Oct 22, 2014 at 15:37
  • Would you prefer a pure JavaScript solution, or is jQuery available to your CMS? Commented Oct 22, 2014 at 15:37

3 Answers 3

1

Alternatively, if you would like to use your existing trackOutboundLink function, this would attach a click event to all outboud links going to http://www.example.com:

$(function() {
  // adding onclick event to this particular link
  $('a[href="http://www.example.com"]').click(function(event) {
    // preventing the user from navigating away from the page, temporarily
    event.preventDefault();
    // GA tracking
    trackOutboundLink('http://www.example.com');
    // continue default behavior for the click
    window.location = 'http://www.example.com';
  }); // end click event
}); // end document ready

You may want to consider redirecting the user using the callback hitCallback:

ga('send', {
            'hitType': 'event',
            'eventCategory': '...',
            'eventAction': '...',
            'eventLabel': '...',
            'hitCallback': function(){
                // redirect:
                window.location = 'http://www.example.com';
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

0

You can embed the GA send method into the onclick:

onclick="ga('send','event','outbound','click','http://www.example.com');"

This would likely do what the trackOutboundLink function does (assuming you were basing your example from this https://support.google.com/analytics/answer/1136920?hl=en).

1 Comment

That is the source material for what I am trying to do, but the issue is that I am trying to fetch an HREF out of a navigation created by the CMS and then add the above onclick (including the URL) to specific links.
0

There seems nothing wrong in your syntax, its probably the quotes that are causing the problem.

<a href="http://www.example.com"  onclick=" trackOutboundLink('http://www.example.com'); return false; ">Check out example.com</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.