Integrating Google Analytics Tracking Events in WordPress Plugin
Posted on 20th June 2023
Google Analytics is a powerful tool for understanding how people use your website. By default, it tracks pageviews, unique visitors, and other broad metrics. However, if you want to get more granular data about how people are using your website, you can use Google Analytics tracking events.
In this article, we’ll show you how to use Google Analytics tracking events in your WordPress plugins. We’ll also cover some common use cases for tracking events and how to get the most out of them.
What are Tracking Events?
Tracking events are actions that users take on your website that you want to track in Google Analytics. For example, if you have a button on your website that people can click to download a file, you can track how many people click that button by using a tracking event.
Tracking events can be used to track anything that you want to know more about. For example, you can use them to track how many people:
click a button
watch a video
fill out a form
send a message
The sky’s the limit when it comes to tracking events. You can use them to track just about anything that you want to know more about.
Why Use Tracking Events?
Tracking events are a great way to get more data about how people are using your website. By default, Google Analytics only tracks broad metrics like pageviews and unique visitors. However, tracking events can give you a more detailed picture of how people are using your website.
For example, let’s say you have a button on your website that people can click to download a PDF. By default, Google Analytics would only track how many people visit the page that contains the button. However, if you use a tracking event, you can also track how many people actually click the button.
This is just one example of how tracking events can be useful. You can use them to track just about anything that you want to know more about.
How to Use Tracking Events in WordPress
There are two parts to using tracking events in WordPress:
Adding the tracking code to your website
Configuring the tracking code
We’ll cover both of these parts in detail below.
Adding the Tracking Code to Your Website
The first thing you need to do is add the tracking code to your website. The tracking code is a piece of JavaScript that tells Google Analytics to track an event.
You can add the tracking code to your website in two ways:
Manually: You can add the tracking code to your website’s code yourself. This is the recommended method if you’re comfortable with code.
Using a plugin: You can use a plugin to add the tracking code to your website. This is the recommended method if you’re not comfortable with code.
We’ll cover both of these methods in detail below.
Method 1: Manually Adding the Tracking Code
If you’re comfortable with code, you can add the tracking code to your website yourself. To do this, you’ll need to edit your website’s code.
If you’re using a self-hosted WordPress site, you can edit your code using a code editor like Atom or Sublime Text. If you’re using WordPress.com, you can edit your code using the Code Editor.
Once you’ve opened your code editor, you’ll need to edit the header.php file. This file controls the code that appears at the top of your website.
To edit the header.php file, you’ll need to login to your website’s hosting account and go to the File Manager. Once you’re in the File Manager, you’ll need to find the header.php file and open it for editing.
Once you’ve opened the header.php file, you’ll need to add the following code to it:
ga(‘send’, ‘event’, ‘category’, ‘action’, ‘label’);
Replace category, action, and label with your own values. For example, if you’re tracking how many people click a button, you might use the following values:
ga(‘send’, ‘event’, ‘button’, ‘click’, ‘download pdf’);
Once you’ve added the tracking code, you’ll need to save the header.php file and upload it to your website.
Method 2: Using a Plugin to Add the Tracking Code
If you don’t feel comfortable adding code to your website, you can use a plugin to add the tracking code for you.
There are a few different plugins that you can use, but we recommend using Google Analytics for WordPress by Yoast. It’s a free plugin that’s easy to use.
To install the plugin, login to your WordPress site and go to the Plugins page. Then, click the “Add New” button at the top of the page.
In the search field, enter “Google Analytics for WordPress by Yoast.” The plugin should be the first one that appears in the results. Click the “Install Now” button and then activate the plugin.
Once the plugin is activated, you’ll need to configure it. To do this, go to the Settings page and click on the “Google Analytics” tab.
First, you’ll need to enter your Google Analytics tracking ID. You can find this ID by logging in to your Google Analytics account and going to the Admin page. In the “Property” column, click on “Tracking Info” and then “Tracking Code.” Your tracking ID will be listed at the top of the page.
Next, you’ll need to choose what type of website you’re tracking. If you’re tracking a WordPress site, you should choose the “Universal Analytics” option.
Then, you’ll need to choose how you want to track your website. The recommended setting is to use the “JavaScript tracking code (analytics.js).”
Once you’ve entered your tracking ID and configured the plugin, you’ll need to add the tracking code to your website. To do this, go to the “Advanced Settings” page and click on the “Global Site Tag (gtag.js)” tab.
Copy the code that’s listed on this page and paste it into your website’s code. You can do this using a code editor like Atom or Sublime Text.
If you’re using WordPress.com, you can edit your code using the Code Editor. Once you’ve added the code, you’ll need to save your changes and upload the file to your website.
Configuring the Tracking Code
Once you’ve added the tracking code to your website, you’ll need to configure it. The tracking code is a piece of JavaScript that tells Google Analytics to track an event.
To configure the tracking code, you’ll need to edit the code. You can do this using a code editor like Atom or Sublime Text.
Once you’ve opened your code editor, you’ll need to find the tracking code. It should look something like this:
ga(‘send’, ‘event’, ‘category’, ‘action’, ‘label’);
Replace category, action, and label with your own values. For example, if you’re tracking how many people click a button, you might use the following values:
ga(‘send’, ‘event’, ‘button’, ‘click’, ‘download pdf’);
Once you’ve configured the tracking code, you’ll need to save your changes and upload the file to your website.
Common Use Cases for Tracking Events
Now that you know how to use tracking events, let’s take a look at some common use cases for them.
Tracking Button Clicks
One common use case for tracking events is to track button clicks. This can be useful if you want to know how many people are taking an action on your website.
To track button clicks, you’ll need to add the following code to your website:
ga(‘send’, ‘event’, ‘button’, ‘click’, ‘download pdf’);
Replace “download pdf” with the text that’s displayed on the button. For example, if the button says “Download eBook,” you would use the following code:
ga(‘send’, ‘event’, ‘button’, ‘click’, ‘download eBook’);
Tracking Form Submissions
Another common use case for tracking events is to track form submissions. This can be useful if you want to know how many people are filling out a form on your website.
To track form submissions, you’ll need to add the following code to your website:
ga(‘send’, ‘event’, ‘form’, ‘submit’, ‘contact form’);
Replace “contact form” with the text that’s displayed on the form. For example, if the form says “Subscribe to our newsletter,” you would use the following code: