1

I'm trying to setup a link tracking on WordPress. There are several links to the same site, they all share the same class:

<a class="gaClickTrack" href="https://www.amazon.es/Heromask-HeroMask-Aprende-idiomas-jugando/dp/B06XWHZ5Q4" target="_blank" rel="noopener">Buy Now Securely On Amazon!</a>

<img class="gaClickTrack wp-image-3372 size-medium" src="https://www.edinventa.com/wp-content/uploads/2018/07/buy-now-300x207.png" alt="Buy Now" width="300" height="207" />

We are using the plugin "Per page head", which allows you to add content into the section for a specific page, like custom JS or custom HTML. This is the custom code added:

<script>
jQuery('.gaClickTrack').on('click', function() {
    ga('send', 'event', 'button', 'click', 'amazon-button-clicked');
});
</script>

Note: if we use $ instead JQuery, it doesn't seem to work: the console returns an error.

We are using the Chrome extension Google Analytics Debugger, but either we don't know how to use it or it detects nothing.

I can see on Google Analytics - Real time - General Vision, that it detects there is someone on the page, but I don't see the click event anywhere (I guess it should appear on Real time - Events.

Where is our mistake??

9
  • 1
    Is the content (ie, the links) loaded regularly or via AJAX after the initial payload? Can you try jQuery(document).ready(function(){ jQuery('.gaClickTrack')... }); (aka wrapping your current code in .ready())?
    – kero
    Aug 28 '18 at 10:32
  • You may find it easier to use Google Tag Manager to manage all the tracking. You would add just a short couple of code blocks to your site, then manage any jQuery/JS in GTM itself. It even has a preview mode so you can test whether it's working before you publish.
    – WebElaine
    Aug 28 '18 at 13:37
  • @kero, after wrapping it, the console returns the error: Uncaught ReferenceError: ga is not defined. How can I know if the function ga actually exists or it's a code syntax problem? Thanks so much!
    – chelder
    Aug 30 '18 at 10:13
  • @chelder Mhm that is difficult to say. Have you looked at GTM as WebElaine suggested? Would be a much smoother solution
    – kero
    Aug 30 '18 at 11:04
  • @WebElaine I'm using the plugin WooCommerce Google Analytics Pro plugin. Support just answered they don't support Google Tag Manager yet. I'm trying to get a solution to use it together with that plugin anyway.
    – chelder
    Aug 30 '18 at 15:04
0

This is working in my WordPress site. Remember we are using the plugin "Per page head", which allows you to add content into the section for a specific page, like custom JS or custom HTML. This is the custom code added:

<script>// <![CDATA[
jQuery(document).ready(function($){ 

$('.class-name-of-the-button').click(function() {  

  ga('send', 'event', {
    eventCategory: 'Outbound Link',
    eventAction: 'click',
    eventLabel: event.target.href,
    transport: 'beacon'
  });

});

});
// ]]></script>

Explanation:

Finally, notice I have seen in real time in Google Analytics (on Real time - Events) when I have pushed the button to test it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.