How to add Google Analytics to your WordPress themeJuly 7, 2018
Where are your website’s visitors based? How do they find their way to you? How do they interact with your site? When are they visiting? How long are they hanging around?
Google Analytics is an incredible tool for website owners who want to know how their audience interacts with their site. Understanding your website traffic statistics is crucial for growing your visitor base and knowing how to best optimize your site for further growth down the road. And, perhaps best of all, Google Analytics is free to use! It has fast become a must-have for anyone invested in their little slice of the web.
In this article, we’ll go over how to set Google Analytics up on your WordPress theme.
Step 1. Sign up or Log in
First, sign up for a Google account: Create an account here.
If you have an existing Google account, you can log in using those credentials.
Step 2. Enroll in Google Analytics
After logging into your existing Google account or signing up for a Google account, enroll in Google Analytics.
Click the “Sign up” button to the right to get started.
Step 3. Website details and privacy options
On the next screen, you’ll be prompted to select what sort of tracking you’d like, to fill in your website information, and choose (if any) which data sharing settings you’d like to opt in or out of.
The first question you’ll see is “What would you like to track?”.
Be sure to select “Website“.
Below, enter your Account Name (the profile username that will be used for your website’s Google Analytics), your Website Name, and Website URL.
Additional (optional) information you can input are the Industry Genre of your site, your Country, and Time Zone.
Lastly is the Data Sharing Settings section. Here, you can check or un-check the boxes depending on how you want your website’s analytics to be used.
When finished, click the “Get Tracking ID” button.
Step 4. Terms of Service Agreement
A pop-up will appear with Google Analytics’ Terms of Service and Data Protection Terms. If you have a lot of time on your hands and enjoy reading that sort of stuff, dig in! Otherwise, click the 2 check boxes to agree with the terms and conditions. Then, click the “I Accept” button.
Step 5. Your tracking code
And voila! Now you have your tracking code.
We’ll need to copy this tracking code and insert it into your WordPress theme so that the tool can begin monitoring your traffic stats.
Copy the block of code provided. Open up a new browser tab and log into your WordPress Dashboard.
Step 6. Inserting your tracking code
You have a couple options for how you insert the Google Analytics tracking code into your WordPress theme — via plugin or via the theme’s “header.php”.
There are pros and cons to both options. Inserting the tracking code directly into your theme’s “header.php” file is quick, however if you were to update your theme in the future, you’d need to go back in and re-paste the tracking code once again. Using a plugin to insert the tracking code means that you won’t need to worry about it being erased if you update your theme. However some Google Analytics plugins can take a bit of time to setup.
We’ll go over a couple plugin options below.
Insert code via plugin
Google Analytics Dashboard for WP by ExactMetrics: This free plugin is one of the most popular Google Analytics plugins on the WordPress Plugin Directory. It not only helps you track your website traffic, but enables you to view the statistics right on your WP Dashboard! No need to check your Google Analytics account every day when the pertinent details are right at your fingertips in real time.
To insert your tracking code using the Google Analytics Dashboard for WP plugin, first install and activate the plugin. You can do this by navigating to WP Dashboard > Plugins > search for “Google Analytics Dashboard” in the upper-right field > click the “Install” button > and, lastly, click the “Activate” button.
Now let’s get it setup!
1. Navigate to WP Dashboard > Google Analytics (in left side menu) > General Settings.
2. Click the “Authorize Plugin” button. Then, click the red “Get Access Code” link.
3. You’ll be prompted to log into your Google account and then asked to confirm you’re ok with ExactMetrics pulling your Google Analytics data.
4. Copy the access code they provide and paste it into the “Access Code” field on your WP Dashboard. Save when finished.
5. Navigate to WP Dashboard > Google Analytics > Tracking Code.
6. Where it says “Tracking type“, it’s recommended to select the “Tracking” option and to leave the “gtag.js” option turned off. Save changes.
For more details on how to setup and use the Google Analytics Dashboard for WP by ExactMetrics, check out their documentation page.
Insert Headers and Footers: Developed by the talented WPBeginner crew, this plugin will take whatever code you provide it and automatically insert it into your “header.php” file. This applies the tracking code to all pages of your site. No frills, no fuss, and no need to re-paste the tracking code into your theme file if you were to update your theme down the line.
First, install and activate the plugin.
You can do so via WP Dashboard > Plugins > search for “Insert Headers and Footers” > click the “Install” button > and then click the “Activate” button.
To set the tracking up:
1. Navigate to WP Dashboard > Settings > Insert Headers and Footers.
2. Copy your Google Analytics tracking code and paste it into the field titled “Scripts in Header“.
3. Click the “Save Changes” button.
It’s that easy!
Insert code manually in “header.php” file
Would you prefer to skip the plugins and simply paste the code into the theme file yourself?
1. Navigate to WP Dashboard > Appearance > Editor > and open the file titled “header.php” from the list on the right.
2. Toward the top of the document, look for the bit of code that looks like “</head>“.
3. Directly ABOVE this </head> bit of code on the blank line, paste in your Google Analytics tracking code.
4. Save changes to the file when finished.
REMEMBER! Using this method of manually inserting the tracking code within your “header.php” file is fast and effective, though you’ll need to re-do this step if you ever change WordPress themes or update your theme.
Step 7. Let the data roll in!
You’ve done it! All that’s left to do is sit back and be wowed by your site traffic statistics.
If you’re using the Google Analytics website to view and track your data, log into your Google Analytics account dashboard.
This is your Google Analytics dashboard area. The menu is on the left and a variety of graphs, tables, and maps will be on the right.
Shortly after adding the tracking code to your site, these graphs and charts will begin to populate with information.
Your menu offers a number of helpful reports:
Audience: The Audience report will give you insight into who is visiting your site such as demographics, behavior, browsers, etc.
Acquisition: The Acquisition report sheds light on where these visitors are coming from and how they reach your site.
Behavior: The Behavior report reveals what visitors do while they’re on your site.
Conversions: Conversions are like goals you’ve defined, such as making a sale. The Conversion report will help you track how you’re doing meeting things goals.
Be sure to check out Google Analytics’ documentation for more in depth details on how to utilize this awesome and powerful tool to its full potential!