TheInventor
TheInventor

Reputation: 43

Script sending events to Google Analytics

Hello,

I´m trying to track events on a great deal of websites, all made in Wordpress.

Previously I've been using GTM to track events, but I am looking to make a script to automate the process of sending events directly to Google Analytics.

I want to create and send an event every time a click happens on: 'tel:' - 'mailto:' - and external links.

<button onclick="ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);">phonenumber</button> 

I have used a modified version of this script, but I want to automate the process, so I dont have to scan the site for phonenumbers etc:

This is what I got so far, but I cant get it to work - I'm not getting any events in GA:

<script type="text/javascript">

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');

  $(document).ready(function(){
    $('tel:').on('click', function (){
        ga('send', 'event', 'download', 'click');
    });
  });

</script> 

(I'm applying the script to the 'Tracking Code' area of my themes)

Any help regarding such script would be very much appreciated!

Upvotes: 2

Views: 349

Answers (1)

Michele Pisani
Michele Pisani

Reputation: 14179

Try this:

$(document).ready(function(){
  $("a[href^='tel']").on('click', function (){
      ga('send', 'event', 'download', 'click');
  });
});

Upvotes: 1

Related Questions