最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - New Google Analytics event tracking wont work on mailto - Stack Overflow

programmeradmin6浏览0评论

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/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?

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./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?

Share Improve this question edited Jan 8, 2020 at 7:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 17, 2014 at 16:33 user1788364user1788364 1,1513 gold badges19 silver badges33 bronze badges 2
  • 1 have you tried using onmousedown instead of onclick? – Eduardo Commented Feb 17, 2014 at 17:26
  • 1 Hi Eduardo - I tried onmousedown and it worked - thank you! This seems like the best solution to me as you don't need the extra markup! – user1788364 Commented Feb 18, 2014 at 9:26
Add a ment  | 

2 Answers 2

Reset to default 8

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]" >

Universal Analytics has built-in functionality for delaying sends called hitCallback.

ga('send', 'event', 'Contact', 'Email', 'Name here', {
  'hitCallback': function() {
    document.location.href = this.href
  }
});

Haven't tested it, but should be pretty close to working. Google-around for other ideas.

发布评论

评论列表(0)

  1. 暂无评论