How to Add Google Analytics Tracking Code in WordPress


Google Analytics is definitely the most popular tool used to track the visitors that come to your website. With this free service, you can monitor your website traffic, user behavior, conversions, and check tons of data like page views, sessions, traffic source, bounce rates and much more.

This allows you to perform A/B split tests, decrease bounce rates, increase page views and optimize your website in any way you want, and that’s really important for all website owners who want to increase the income generated by their online businesses. You can install Google Analytics on your WordPress website following this three simple steps, so let’s check it out!

Step 1: Register on Google Analytics

Before installing Google Analytics on your website, you have to create your own Google Account. If you already have it then you can use it to complete the signup process, otherwise, you’ll have to create a Google Account here.

Step 2: Set up your Account

When you are correctly logged in into the Google Account, you have to fill up all the necessary pieces of information like account name, website name and URL, time-zone, country etc.

After that, click on the “Get Tracking ID” button. You’ll also need to agree to their terms and conditions.

When all registration process is completed, you can get your Google Analytics tracking code. Copy it and save for a while, because we’ll need this code to install Google Analytics on your WordPress website.

Step 3: Install the tracking code on your WordPress website

Google Analytics tracking code can be installed in a few different ways, however, I’ll describe only the two most popular methods in this article.

a. MonsterInsights plugin

How to Add Google Analytics Tracking Code in WordPress

This method is recommended for those who don’t want to fidget with any code and just want a safe and simple method to install Google Analytics on their website. MonsterInsights plugin, formerly known as “Google Analytics for WordPress by Yoast” is available in both free and paid version, so it’s great if you’ll need some more advanced features. Here are the steps you can follow to install the plugin:

  1. Download the MonsterInsights plugin, upload it to your WordPress (check this tutorial if you’ll need to use FTP to upload plugins to WordPress) and activate it.
  2. When you already have this plugin correctly installed and activated on your website, go to “Insights” – “Settings” and click on the “Authenticate with your Google account” button. Select your Google account and allow the necessary permissions to MonsterInsights plugin.
  3. After that, select your website and click on the “Complete Authentication” button. You’ll be automatically redirected back to your WordPress website, and the installation process is finished! 🙂

If you’ll need to find out some more about that, check this documentation article in MonsterInsights plugin docs.

Your Google Analytics data will be available to preview in “Insights” – “Reports” tab in your admin panel after a few hours, so you wouldn’t need to login to Google Analytics service all the time when you want to check your stats, it will be available in your WordPress admin panel. Isn’t it comfortable? 🙂

b. Insert the code directly to your WordPress Theme

This method can be difficult for people with no prior experience of coding and thus it is not recommended. It’s also not a good practice to modify theme files, because all changes will be lost in next theme update (your files might be overwritten). Anyway, if you really want to use this method, here are the two possible ways about how to add Google Analytics tracking code to your WordPress website with it:

  • Edit the “header.php” file of your WordPress theme. Copy your Google Analytics tracking code and paste it right after the tag in this file.
  • Edit your “footer.php” file. As same as before, copy the tracking code but paste it right before the closing tag.

Be sure to save all the changes after editing those files to apply the tracking code on your website.

c. Use custom plugin for that

Much better solution than the second one, and much more lightweight than the first one is to use this simple custom plugin to display the Google Analytics tracking code on your WordPress website.

  • Go to “wp-content/plugins” directory in your WordPress directory,
  • Add new directory, for example, “my-google-analytics-code”, and new file inside it: “my-example-analytics-code.php”
  • Put this code into your plugin file:

  • Replace the Google Analytics tracking code placeholder (UA-XXXXXXXX-X) with your own code and save changes,
  • Activate the plugin in your WordPress admin panel – “Plugins” page.

With this simple solution, you are free to update your theme with no doubts that you’ll lose the custom changes.

Hope it helps!

Hope this article helps you to find a way about adding Google Analytics tracking code to your WordPress website! If you have any questions, feel free to add your comments to this article.