Track Phone, Email and WhatsApp Link Clicks
Sends an event to Google Analytics 4 (or the GTM dataLayer) whenever a visitor clicks a phone number, email address or WhatsApp link, so you can count leads from these contacts.
Opens WPIDE on
Code
add_action( 'wp_footer', function () {
?>
<script>
document.addEventListener('click', function (e) {
var link = e.target.closest ? e.target.closest('a[href]') : null;
if (!link) { return; }
var href = link.getAttribute('href') || '';
var method = /^tel:/i.test(href) ? 'phone' : /^mailto:/i.test(href) ? 'email' : /(wa\.me|api\.whatsapp\.com|whatsapp:)/i.test(href) ? 'whatsapp' : '';
if (!method) { return; }
var params = { method: method, link_url: href.replace(/^(tel:|mailto:)/i, ''), link_text: (link.textContent || '').trim().slice(0, 100) };
if (typeof gtag === 'function') {
gtag('event', 'contact_click', params);
} else {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push(Object.assign({ event: 'contact_click' }, params));
}
}, true);
</script>
<?php
} );
Notes
Events are named contact_click with a method parameter (phone, email, whatsapp). Mark contact_click as a key event in GA4 to count it as a conversion.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.