Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Google Analytics event tracking wont work on mailto

I'm using the latest Google Analytics code:

(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', 'XXXXXXXXX', 'XXXXXXXX');   
      ga('send', 'pageview');

I've got event tracking to work on a download link using the following:

<a href="#" onclick="ga('send', 'event', 'Download', 'PDF', 'FILE NAME');">

However, it's not working on a mailto link - when I look in the console it says the request has been cancelled. This is what I'm using:

<a href="mailto:[email protected]" onclick="ga('send', 'event', 'Contact', 'Email', 'Name here');">

When I remove the "mailto" it will then track.

Am I setting it up wrong?

EDIT: It appears if I put a target="_blank" it will work - however it then opens up another window which isn't ideal.

Second Edit: It appears it's something to do with Chrome - I tested it in Firefox and IE and it worked when I did that - anyone else experienced this?

like image 330
user1788364 Avatar asked Dec 12 '25 03:12

user1788364


1 Answers

I found a relevant thread here: Google Analytics Event Tracking not firing for multiple accounts on Chrome ONLY

So in the end I got it working with chrome - this is how it looks now for those interested:

<a onclick="setTimeout(function(){ga('send', 'event', 'Email', 'Person Name');}, 1500);" href="mailto:[email protected]" >

A timeout function had to be added.

As Eduardo pointed out above another option that worked was having a mousedown function:

<a onmousedown="ga('send', 'event', 'Email', 'Person Name');" href="mailto:[email protected]" >
like image 128
user1788364 Avatar answered Dec 13 '25 15:12

user1788364



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!