How to Add Google Analytics to a WordPress site

Here I’m going to explain the simplest way to add Google Analytics to your WordPress site.
Following are the prerequisites,

  1. A live WordPress installation (Your WordPress site).
  2. Access to your website files (cpanel access).


Step 1: Creating a new plugin.

Plugins insert code into your site by attaching code to a hook which is already written in your theme files. So, in order to attach code to the head section of your code, you need to have a wp_head hook in the header.php file of your theme. Access the wp-content folder of your WordPress installation and navigate to the folder of your theme. For example, if your theme is Twenty Seventeen, you have to navigate to the folder wp-content/themes/twentyseventeen to find the header.php file.

Open header.php. This file includes all of the code at the beginning of each page, including the <head> section of the page. You will find the wp_head hook at the end of the <head> section.

Now your theme is ready for adding the Google Analytics code.

Note: A well-written theme always include this. It is not recommended to edit any of your theme files as you will lose all the changes in case if you update the theme in future.

Step 2: Getting the Tracking code.

Create a Google Analytics account using your existing google/gmail account and add your website to it.
You can follow the below steps to add your website to Google Analytics.

  1. Sign in to your Google Analytics account.
  2. Click Admin.
  3. Select Create new account from the menu.
  4. Click Website or Mobile app.
  5. Under Setting up your Account, enter an Account Name.
  6. Under Setting up your Property, enter the Website or App name.
  7. Enter the website url.
  8. Select an Industry Category.
  9. Select Reporting Time Zone.
  10. Under the Data Sharing Settings, select the settings you want.
  11. Click Get Tracking Id.

You’ll get the script by clicking the Tracking Code tab. Copy the entire script.

Step 3: Creating the Plugin

Next step is to create a plugin to which we need to add the tracking code.

Navigate to your sites wp-content/plugins folder and create a new file. Give it a relevant filename.

For Example I’m giving the name cod-google-analytics.php

Open the file in the editor and add the following code into it.

Plugin Name: Simple Google Analytics Plugin
Plugin URI:
Description: Adds a Google analytics tracking code to the <head> of your theme, by hooking to wp_head.
Author: Sona Krishnan
Version: 1.0

Note: Edit the above details of the plugin as you need.

Once it is done, below that add a function that contains the Google Analytics tracking code and attach the function to the wp_hook as shown below.

function cupofdreams_google_analytics() { ?>
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

      ga('create', 'UA-XXXXXXXX-X', 'auto');
      ga('send', 'pageview');
<?php }
add_action( 'wp_head', 'cupofdreams_google_analytics', 10 );

Save the file and your Plugin is ready.

Step 4: Activating the plugin.

Go to WordPress Admin Dashboard. Open the plugins screen and you will find your new plugin listed. Activate the plugin and you are done!

Note: You might have to wait for a short while untill Google picks up the new code.

Add Your Comment