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
3 Answers
Reset to default 8This 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.