The complete guide to google-analytics for WordPress

July 6th , 2020 by BlazeThemes

Google Analytics is a web analytics service that helps you understand your visitors, analyze the effectiveness of your marketing efforts and optimize your store for conversions and sales. 

This service is provided by Google for completely free.

The detailed statistics from Google Analytics provide answers to your following questions about:

  1. Traffic Source:- From which source – Search Engine, Social Media, Referral links from other websites or by directly entering your website address – do they reach your site? 
  1. A Timestamp on your Visitors’ visits:- When exactly – hour, day, week or month-  do the visitors come to your site?
  1. Visitors’ Behaviours and Actions:- Which pages do they look at, your bounce rate (the percentage rate of visitors who leave after just one page), how long do they stay on your site, what are they searching for, and have they been converting or not?
  1. Information about your visitors:- Where do your visitors live, what devices do they use to access your site, which age category and gender visit your site the most?
  1. Comparison of two time Periods: How much your traffic increased or decreased in a period of time in comparison to the traffic of another period of time?
  1. Keep track of your content and site:- How many views and shares each of your blog posts receive; which content is performing best; how much time it takes to load your pages?

You can also assign different types of goals- like subscribing to newsletters, filling a form and providing email address, etc,- to keep track of.

When you get these answers, you no longer need to make decisions on intuition. There won’t be any need for trials and errors because you have all the facts and hard data required to make a good decision and implementation. 

Then, you can work on improving your visitors’ experience, improve your website’s technical aspects, improve your content, and optimize traffic sources.

But are you worried that you know nothing about how to add Google Analytics? 

Don’t worry! I’ll hold your hand and take you through it. 

By the end of this blog, you’ll have sufficient knowledge to add Google Analytics to your website yourself.

How to get started with Google Analytics

  1. If you have a Google account and are not signed in, sign in to your Google Account. If you do not have, create a Google Account and sign in.
  1. Go to  google.com/analytics. Click the ‘Sign in to Analytics’ or ‘Start for free.’

3. Sign up to Analytics account.

4. You’ll be navigated to ‘Create Account’ page.Enter Account Name and select the options that are provided. Click next.

  • Select ‘Web’ and click next.
  • Under Property setup, 

enter your website name, website URL, industry category and reporting time zone, then click Create.

  • ‘Google Analytics Terms of Service Agreement’ box will pop up. Select your country/region from the drop-down menu. If you fall under the scope of GDPR, select the checkbox stating that you will accept the Data Processing Terms as required by GDPR. 

Select the next checkbox to accept the Measurement Controller-Controller Data Protection Terms. You can know more about the data sharing settings on Google support. If you don’t want to accept, you’ll have to turn off data sharing.  Click ‘I accept.’

  • You will be then be directed to your Google Analytics dashboard.

Under Admin section > User Management, select the Proper Account and Proper property. If you only have one account, then that will be the default account and property.

Once you’ve selected the Proper Account and Proper Name, click on Tracking info > Tracking Code. There, you’ll find your UA Tracking ID. 

  • Copy the whole script and add the Tracking ID to your site.

Step 3. Add Google Analytics to WordPress

Option A: Insert the Code Into header.php

For Google Analytics to track visitors all across your site, the code needs to be placed on every page of your site that you want it to work on. It can be easily done on WordPress sites by adding the tracking code into the header section of your website. 

Most standard themes have a header.php file where you can simply add the Google Analytics code.

With this small step, the code will load on every page and google analytics will track visitors.

Now let’s begin.

When you make changes to theme files, be sure to use a child theme, The child theme is relatively easier to modify and it will also make sure that customizations do not get affected when you update your parent theme. 

We have also covered about child theme in our previous blog. Check out the blog if you need in-depth knowledge regarding it.

After you have created your child theme, copy the header.php from the main theme into it.

Copy the entire Tracking code. 

Go to the header.php file.

Paste the tracking code from Google Analytics right before the closing </head> bracket.

Save it and upload the file back to your server. Voila! The job is done.

Note:

  1. Google recommends that we place the script in the <head> section of our site because we want the script to load earlier before any other scripts. This way, we can accurately record statistics.
  1. It is not imperative that we place the code right before the </head> bracket though. 

Many websites place it before the wp_head. But, it does not make any difference in performance. It’s totally your choice.

Option B: Use functions.php

You can also use the functions file to add Google Analytics without a plugin. It is the same process as we did above. 

But instead of adding the tracking code directly to header.php, insert it into the header area (above the </head> tag to be more specific) in the functions.php file to your child theme.

Save changes and upload the file back to your server.

Option C: Use a Custom Plugin

A WordPress plugin is a PHP file with a WordPress plugin header comment. You can create your own plugin to manually add the tracking code to your site.

The benefits of a custom plugin are:

  1. It gives you the ability to switch tracking on and off from the WordPress back end.
  2. Analytics tracking code stays on your site even if you make changes to your themes.

Do not fear at the thought of creating your plugin. It’s not complicated. 

To get started, choose the text editor that you feel comfortable using.

 If you don’t have any, notepad works fine. 

Create a new notepad file, and name it something like  ourwebsitename-google-analytics.php.

Copy all of the following code and paste it in your notepad file. Edit plugin and author details to match yours.

<?php
/**
Plugin Name: My own Google analytics plugin ( or something that you like)
Plugin URI: https://blazethemes.com/plugins/custom-google-analytics-code-plugin
Description: Insert Google Analytics code to your site header
Version: 1.0
Author: Bidur Maharjan
Author URI: http://blazethemes.com/
*/
 
function ns_google_analytics() { ?>
 <script async src="https://www.googletagmanager.com/gtag/js?id=UA-121810903-1"></script>
 <script>
   window.dataLayer = window.dataLayer || []; [our own blazethemes analytics code]
   function gtag(){dataLayer.push(arguments);}
   gtag('js', new Date());
 
   gtag('config', 'UA-121810903-1');
 </script>
 <?php
 }

add_action( 'wp_head', 'ns_google_analytics', 10 );

Replace all of the code from <scrip> to  </script> with your own Tracking Code.

Then upload the file to your site’s plugin folder (wp-content>plugins). When it’s done, activate it from the WordPress back-end.

Note: Be sure to check that there is the wp_head hook inside header.php on your active theme. This hook allows you to change how WordPress behaves without editing any core file. If you don’t have this hook, it will not work. It is usually implemented by default so do not worry.

Option D: Using plugins

The above methods are not so difficult. But if you do not want to take any hassle, you can easily do it with the help of plugins that are available on the WordPress Plugin Directory. (Difference between directory and repository?)

There are a lot of plugins in WordPress Directory to add Google Analytics to your website.

However, in this blog, I’m going to choose ‘MonsterInsights’ and ‘Insert headers and footer plugin” for the tutorial. 

  1. MonsterInsights

MonsterInsights is one of the most popular plugins available with over two million installs. This plugin also lets you quickly access a post, page, site or network performance overview without leaving your WordPress dashboard.

Steps to install the plugin:

  1. Go to Plugins > Add New.
  2.  Search for  ‘MonsterInsights’. 
  3. Click “Install Now” on the MonsterInsights label / box?. The plugin will then be downloaded to your site.
  4. Activate it when the download is complete.
  5. After the plugin is activated, a new menu item called ‘Insights’ will appear on your site’s dashboard. 
  6. Click on it to go to “Settings.”
  7. Click on – “Authenticate with your Google account”. You will be redirected to the Google Analytics page. 
  • If you are not signed in, you will be asked to sign in.

Or,

  • If you are already signed in, you will be asked to select the account you want to use for tracking.
  1. Once you’re logged in, Google Analytics will ask you to give permission for the WordPress plugin to access your data.
  2. Click “Allow”.
  3. Select the profile that you created earlier and complete the authentication.
  4. After its completion, you will be directed back to your site.

And, the process is complete.

 Note: It will take Google Analytics sometime before it starts showing your statistics.

  1.  Insert headers and footer plugin

This plugin lets you insert code to your WordPress site header and footer without needing to edit your theme files.  You can simply add codes on its interface without needing to go through all those PHP files and sections.

With this plugin, you can use it to easily add the Google Analytics code to your website.

Steps to add Google Analytics Tracking:

  1. Go to plugins -> Add new-> 
  2. Search for “Insert headers and footers”. 
  3. Install the plugin.
  4. After installation is complete, Activate this plugin.
  5. Then go to settings -> Insert header and footer

<img> 

  1. You will see options to add header and footer script. Paste your google analytics code in the Scripts in Header field and save it.

And, your work is done.

Last Few Words

You are ready to add Google Analytics to your WordPress site.

Google Analytics is your ally to grow your WordPress website. With its help,  you can unlock many windows of opportunities that you never even knew that they existed.

And, there are so many options to install it. None of the options is complicated.

I hope that you, even if you are new to analytics, learned everything you need to know about Google Analytics and methods to add it.

We’re wrapping it up for this blog but we’ll be back with more informational blogs. 

Bye..

Leave a Reply

Your email address will not be published. Required fields are marked *

ALSO ON BLAZETHEME

Google Analytics is a web analytics service that helps you understand your visitors, analyze the effectiveness of your marketing efforts and optimize your store for conversions and sales.  This service is provided by Google for completely free. The detailed statistics from Google Analytics provide answers to your following questions about: Traffic Source:- From which source […]

We Developers, all around the world no matter which programming language or CMS platform we use, we usually get this word from client  “Hey can you do this change on my website? It is a small change.. it won’t take long. Can you do it right now?“. This is especially true for WordPress as it […]

Are you ready to get started? It's never been this easy.

Check out our newest themes.