简体   繁体   English

将 onclick 添加到所有电话链接

[英]Adding onclick to all tel links

I have a wordpress site.我有一个 wordpress 网站。 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 .我只能使用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.我知道我可以使用全局 javascript,但我不知道如何更改所有电话链接以添加 onclick 选项。

Any one have any ideas on how to do this or has anyone already done something like this before?任何人都对如何做到这一点有任何想法,或者以前有人已经做过这样的事情吗?

This will add the onclick event to every a tag whose href starts with tel .这会将onclick事件添加到每个hreftel开头的a标签中。

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

You can do this with plain JavaScript:您可以使用纯 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.与 IE9+ 和所有其他现代浏览器兼容。

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.如果这有效,请告诉我,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM