Learn Wordpress

Initially built as Blogging Application, Wordpress quickly evolved into the most used CMS today!

• Last Updated: 07/07/2020
• ( 6 minutes reading )

How to manually integrate Google Analytics into WordPress

Introduction

Google Analytics is a free web traffic analytics service supported by Google that allows you to monitor your site data and user activity. It gathers a wide range of information and provides detailed audits on website performance. It presents ready reports, which, when properly analyzed, can be used as a base for improving marketing and advertising strategies, user segmentation, website design, content, and functionalities.

Google Analytics is integrable with a JavaScript code snippet inserted into the header of all website pages. Generally, there are two main approaches for integrating Google Analytics into WordPress - manually and with a plugin.

In this tutorial, we will step-by-step guide you on the process of manually adding the required code to your WordPress-based website. Although this might sound complicated, the way WordPress is set up converts it into a relatively simple task.

Most WordPress themes have a header.php file, which outputs the header section of the website. Therefore, by editing it, the change will be applied across all website pages.

Anyhow, you should be aware that when you perform changes to the theme files, they will be lost if the theme gets updated or you decide to change it. With this in mind, in such cases, it is recommended to perform the required code customization in a child theme. If you are not familiar with the child theme concept, you can find a thorough explanation in our guide on What is a Child Theme and how to create one in WordPress.

In case you would prefer to proceed with the plugin integration, we have prepared comprehensive tutorials on three of the most popular ones.

Now that we got it all clear let's jump straight to the point!

 

Getting Started

First thing first, you need to set up your Google Analytics account. This is a free service, and all you need is a Google account. If you have not done that yet, please find detailed instructions in our tutorial on How to set up your Google Analytics account.

In order to add Google Analytics to WordPress, you will need the "Global Site Tag (gtag.js)" presented during the setup of your account.

In case you have not saved it, please log in to your Google Analytics account and click on the "Admin" option at the bottom of the left menu.

 

Access Google Analytics Admin section

 

Afterward, please click on "Tracking Info" positioned in the middle "Property" column. Upon doing that, you will be presented with a dropdown with additional options - please select "Tracking Code".

 

Google Analytics Tracking Code

 

Once this is done, you will see the required code displayed in the "Global Site Tag (gtag.js)" content box.

Please make sure to either keep the page open or copy the code. Also, note that this is sensitive data, therefore you need to keep it secure and not share it with anyone.

 

Google Analytics Global Site Tag (gtag.js)

 

Next, before you step into making any changes to the code of your WordPress website, it is always recommended to create a backup copy first. You will be editing the code which outputs your site, and any mistake as insignificant as it may seem can interrupt its proper functionality.

If you are a HostArmada customer, we already got you covered in this aspect, as our shared solutions come bundled with a free backup feature. Furthermore, they are stored on remote servers for enhanced security.

If you need guidance on the process of creating a website backup, please refer to our tutorial on How to use the Backup feature in cPanel.

 

Inserting the Tracking Code in the header.php file

Now you are completely ready to step into actually adding the Google Analytics Tracking code you have copied earlier into your header.php file. There are a few ways you can do that - through the File Manager feature integrated into cPanel, via your favorite FTP client, or directly through the WordPress Backend.

Whichever way to decide to follow, note that the code has to be inserted right after the opening <body> tag of the header.php file (wordpress_root/wp-content/themes/theme_name/header.php). Also, you need to make sure to save the file after you apply the change.

For the sake of our example, we will perform the change through the WordPress Backend as this is the most beginner-friendly approach. If you are going along with us, please log into your WordPress Admin Dashboard.

You can do that by accessing your domain followed by "/wp-admin", i.e., "yourdomain.com/wp-admin", where "yourdomain.com" stands for your actual domain name.

Once logged, please navigate through the left-hand side menu bar to Appearance Theme Editor.

 

Access WordPress Theme Editor

 

Upon doing that, you will be redirected to a new page divided into two sections. The left one displays the content of the selected file, while the right "Theme Files" panel lists all theme files.

Please navigate through the list of files and select the "Theme Header (header.php)" file.

When the content is loaded in the left section, please insert the Google Analytics Tracking code as shown in the screenshot example below (right after the <body> tag).

 

Add Google Analytics Tracking Code to WordPress header.php file

 

When ready, make sure to click on the "Update File" button to save the changes. Upon doing so, you will receive a notification indicating the file was saved successfully.

With this done, you have integrated Google Analytics into your WordPress-based website. Now you are ready to start monitoring and analyzing your web traffic by going to your Google Analytics Dashboard.

 

Google Analytics Dashboard Overview

 

Congratulations! In this tutorial, you have learned how to manually integrate Google Analytics to your WordPress-based website without the need of a plugin. Now you can easily analyze your traffic and use the reports to improve your website. We hope this information was useful to you, and our guide helped you to complete this task with ease.

 

 

...
Sebahat
Content Marketing Specialist

Sebahat is a young and bright woman who has become an invaluable part of our team. She started as a Customer Care Representative, mastering that role and, along the way, growing into a tech-savvy individual who is well acquainted with every support layer of the company. Driven by her aim to improve our customers’ experience constantly, she is committed to enhancing the extraordinary support we deliver.