How to add Google Analytics to WordPress without plugin

How to add Google Analytics to WordPress without plugin

Google Analytics is the best free tool to track every visitor coming to your website. In this article, I will show you how to add Google Analytics to WordPress without a plugin.

What is Google Analytics? Why should you use it?

If you don’t already know what Google Analytics is, let me tell the basic information about it. Google Analytics is a free analytic tool that is backed by Google. It helps you to track almost everything of your website such as visitors’ location, search queries that brought them into your website, the best performing web pages, etc. Simply search in Google, you will find much more information about it.

Almost every website on this planet uses Google Analytics for these benefits, why should you not utilize the best tool?

Why should you not use any plugin?

There is a lot of plugins available for free that do the job easily. Well, you can use them. But you should also know that more plugins can increase server load time and that will affect your ranking on search engines. So it’s a wise decision not to use any plugin for such a small job.

Add Google Analytics to WordPress without plugin

Let’s jump into the main part of the article. I will show the procedure of adding Google Analytics to WordPress without plugin step by step so that you can understand easily.

Step 1: Get your Google Analytics Tracking code

Log in to https://analytics.google.com/analytics. Navigate to your website that you want to track. For this, click on “All website data” in the top bar, then select Analytics Accounts > Properties & Apps > Views. After this, go to the Admin section (it is in the bottom of the left sidebar with a cogwheel or gear icon).

In the admin screen, there are three sections. Find <> Tracking info in the middle section and click on it. A list will appear. Click on Tracking code. Now you will find your code under the Global Site Tag (gtag.js) heading. It looks like-

Sample Google Analytics Tracking code
Sample Google Analytics Tracking code

Copy the code, we are going to use it in the next step.

Step 2: Place the tracking code in WordPress

CAUTION: Make a backup before doing this step so that you can revert it back if anything goes wrong.

You can do it in two different ways. I will show you both of these.

  1. Either you can place the code in your theme’s header.php file (simple but not recommended)
  2. Or you can add a hook in your theme’s functions.php file (little complex for those who don’t want to touch codes!)

Placing the code in header.php file

This process is not recommended because if you add the tracking code in the header.php file, the code will get deleted once you update the theme.

Log into your WordPress dashboard. Go to Appearance > Theme Editor. Now search for a file named header.php in the right side of the screen. Click on it to open the file in the editor. Now search for the closing head (</head>) tag. Paste the tracking code that you copied in the previous step just before the closing head tag. Then save the change by clicking on the “Update file” button.

Placing the code in functions.php file

This process is recommended because if you add the code in child theme function.php file, it will not get deleted when you update the original theme. If you don’t know what a child theme is, it’s like a copy of your original theme but anything you modify or change in the child theme will not affect the original theme. You can learn more about child theme here. I recommend you to use a child theme and add the tracking code in the child theme function.php file. Otherwise, the effect will be the same as it was in case of placing the code in header.php which means, your tracking code will be deleted once you update the theme.

Log into your WordPress dashboard. Go to Appearance > Theme Editor. Now search for a file named functions.php in the right side of the screen. Click on it to open the file in the editor. Add the below code at the bottom of the file.

function insert_ga_tracking_code_devslib() {
    ?>
	<!-- Place the tracking code just below this line -->
    <?php
    
}
add_action('wp_head', 'insert_ga_tracking_code_devslib');

Now, copy the tracking code and paste it just under the line <!-- Place the tracking code just below this line -->.

Then save the change by clicking on the “Update file” button.

Check if Google gets any hits from your site

If you have done the two processes successfully, Google will track your site from now on. To check everything is working fine, follow these steps-

  1. Open Google Analytics dashboard in a browser tab, navigate to your site and click on “Home” at the top of the left sidebar.
  2. Open your website in another tab.
  3. Go to Google Analytics tab, you will find on the right side of the screen that shows “Active users right now”. The counter should be at least 1 meaning that there is one visitor currently browsing your site. If it’s zero, there may be any problem or the tracking code was not implemented correctly. Check the processes again if there was any mistake.

That’s it. I hope you have done it successfully. Let me know if you need more clarification in the comment section.

Thank you.

David Moss

Developer | Writer | Coffee lover

Leave a Reply