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

javascript - Adding onclick to all tel links - Stack Overflow

programmeradmin3浏览0评论

I have a wordpress site. Everything is working perfect and no issues. The only thing i am trying to figure out how to do is track whenever someone clicks on a tel link with the following:

<a href="tel:8002221111" onclick="_gaq.push(['_trackEvent', 'Mobile', 'Click to Call'])">Click here to call us now at 1-800-222-1111.</a>

The problem is that i can't add that in my call to action button. I can only use tel:8002221111. I know that i can use global javascript but i have no idea how i would be able to change all tel links to add the onclick option.

Any one have any ideas on how to do this or has anyone already done something like this before?

I have a wordpress site. Everything is working perfect and no issues. The only thing i am trying to figure out how to do is track whenever someone clicks on a tel link with the following:

<a href="tel:8002221111" onclick="_gaq.push(['_trackEvent', 'Mobile', 'Click to Call'])">Click here to call us now at 1-800-222-1111.</a>

The problem is that i can't add that in my call to action button. I can only use tel:8002221111. I know that i can use global javascript but i have no idea how i would be able to change all tel links to add the onclick option.

Any one have any ideas on how to do this or has anyone already done something like this before?

Share Improve this question asked Nov 19, 2015 at 16:37 Brad HazelnutBrad Hazelnut 1,6215 gold badges24 silver badges33 bronze badges 2
  • Are you trying to find all the number and identify them as a phone number ? or you have some wrapper around tel number with class ? – Vasimkhan Commented Nov 19, 2015 at 16:39
  • this is the actual code it shows when it shows the tel link with the number: <a class="button button_js kill_the_icon" href="tel:8002221111"> <span class="button_icon"> <span class="button_label">8002221111</span> </a> – Brad Hazelnut Commented Nov 19, 2015 at 16:41
Add a ment  | 

3 Answers 3

Reset to default 8

This will add the onclick event to every a tag whose href starts with tel.

$("a[href^='tel']").on("click",function(){
    _gap.push(['_trackEvent', 'Mobile', 'Click to Call']);
});

You can do this with plain JavaScript:

document.addEventListener("DOMContentLoaded", function () {
    var elements = document.querySelectorAll("a[href^='tel:']"),
        l = elements.length;
    for (var i = 0; i < l; ++i) {
        elements[i].addEventListener("click", function () {
            _gaq.push(['_trackEvent', 'Mobile', 'Click to Call']);
        });
    }
}, false);

Compatible with IE9+ and all other modern browsers.

Please try this :

jQuery('body').on('click', 'a[href^="tel:"]', function() {
       _gaq.push(['_trackEvent', 'ClickToCall', 'CallRequest', 'Mobile', undefined, false]);
});

If this works please let me know, Thanks.

发布评论

评论列表(0)

  1. 暂无评论