A click connected to a goal through a gtag event

Event Tracking with Google Analytics and WordPress

Note from 2026: this post shows Universal Analytics, the version of Google Analytics we used in 2019. It stopped processing data in July 2023 and was replaced by Google Analytics 4, where goals became key events (see how to add Google Analytics 4 to WordPress). The method still holds: decide what a conversion is, send an event when it happens and check it in real time.

Most paid campaigns that are just starting share the same problem: there are no numbers behind the investment. How do you know who is the tallest person in the room? You measure. Measuring conversions lets the campaign team improve based on real results instead of guesses.

This is the written version of the talk I gave on the technical track at WordCamp Guadalajara 2019: how to measure calls, emails, WhatsApp messages and forms from WordPress with Google Analytics.

What Google Analytics can tell you

  • How much traffic the site gets and how many people visit it.
  • Which devices and browsers they use, and where they are.
  • How many visitors come back.
  • Most important for a campaign: goals and conversions, such as sales, information requests, calls, newsletter sign-ups, video plays and downloads.

Add the Google Analytics code to WordPress

You can do it with a plugin or without one. Without a plugin, paste the global site tag (gtag.js) that Google Analytics gives you, and do it in a child theme so a theme update doesn’t erase it. A small function in the child theme’s functions.php is the cleanest way:

add_action( 'wp_head', function () {
	?>
	<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
	<script>
		window.dataLayer = window.dataLayer || [];
		function gtag(){dataLayer.push(arguments);}
		gtag('js', new Date());
		gtag('config', 'UA-XXXXXXXX-1');
	</script>
	<?php
} );

Then check it. The Tag Assistant extension for Chrome tells you whether the tag fires, and the Real-Time report in Google Analytics should show your own visit within seconds.

What a goal is and why it matters

A goal is any measurable interaction that matters to the business: a call, an email, a WhatsApp or Messenger message, a click on a key button, a form sent, a chat started, a page viewed, time on a page or a sequence of pages.

Two goal types cover almost everything:

  • Destination goal: counts a visit to a page, such as the thank-you page after a form.
  • Event goal: counts an event sent from the site, described by a category, an action, a label and an optional value.

Phone, email and WhatsApp clicks are all measured the same way: with events.

Track email, phone and WhatsApp clicks

A normal link sends nothing to Analytics. Add an onclick that sends an event before the browser opens the mail app, the dialer or WhatsApp:

<a href="mailto:hello@example.com"
   onclick="gtag('event', 'click', {'event_category': 'email', 'event_label': 'email'});">hello@example.com</a>

<a href="tel:+520000000000"
   onclick="gtag('event', 'click', {'event_category': 'phone', 'event_label': 'call'});">Call us</a>

<a href="https://wa.me/520000000000" target="_blank" rel="noopener"
   onclick="gtag('event', 'click', {'event_category': 'phone', 'event_label': 'whatsapp'});">WhatsApp</a>

Write the phone number with its country code in the tel: link, so it dials correctly from any phone.

Track every contact link at once

Editing every link by hand doesn’t scale, especially when phone numbers live in menus, widgets and page builder blocks. WordPress already loads jQuery, so a small script can catch every phone, email and WhatsApp link on the site:

jQuery(function ($) {
	function track(category, label) {
		if (typeof gtag === 'function') {
			gtag('event', 'click', { event_category: category, event_label: label });
		}
	}
	$(document).on('click', 'a[href^="tel:"]', function () { track('phone', 'call'); });
	$(document).on('click', 'a[href^="mailto:"]', function () { track('email', 'email'); });
	$(document).on('click', 'a[href*="wa.me"], a[href*="whatsapp.com"]', function () { track('phone', 'whatsapp'); });
});

The typeof check keeps the page working when an ad blocker stops Analytics. In the talk I also fired the Facebook pixel from the same click, so both platforms counted the same contact. There is a working WhatsApp example on CodePen. Chat tools such as Zendesk Chat have their own Google Analytics integration, so check your provider’s documentation first.

Track Contact Form 7 submissions

Contact Form 7 sends forms with AJAX, so there is no thank-you page to use as a destination goal. It fires DOM events instead. Listen to wpcf7mailsent, which only fires when the message was actually sent (wpcf7submit fires on every attempt, even with validation errors):

document.addEventListener('wpcf7mailsent', function (event) {
	if ('52' == event.detail.contactFormId) {
		gtag('event', 'submit', { event_category: 'form', event_label: 'contact' });
	}
}, false);

Replace 52 with your form’s ID, which appears in its shortcode.

Create the goal and verify it

  1. In Google Analytics, go to Admin › View › Goals and create a custom goal of type Event.
  2. Fill in the category (and the label, if you want one goal per link or form) exactly as it appears in your code.
  3. Open Real-Time › Events, click the link or send the form on your site and confirm the event shows up.
  4. Goals only count from the moment you create them; they are not retroactive. Set them up before the campaign starts, not after.

    Slides and video

    Update, November 2019: the video of the talk (in Spanish) is on WordPress.tv, and the slides are on the WordCamp site. I also wrote about the day itself in WordCamp Guadalajara 2019: my talk on event tracking.

    If you want your site’s calls, forms and messages measured properly, learn more about me or get in touch.

Scroll to Top