Writing Custom Plugin for Google Analytics in WordPress

WordPress Google Analytics tracking plugin

So, the time has come to link your WordPress site with your Google Analytics Global Site Tag (gtag.js) tracking code. This is most commonly achieved by injecting the Global Site Tag (gtag.js) generated in Google Analytics into a HEAD section on your website.

There might be situations where you don’t really want to download yet another WordPress plugin to achieve that. On the other hand, you probably don’t want to simply paste the Tracking Code directly into your header.php as it’ll probably get wiped out when you update your theme in the not so distant future.

So what can we do in such case? Answer is simple, write your own lightweight plugin that will always persist your gtag.js code no matter what happens to your WordPress site. And best of all, it can be achieved in four simple steps, as defined below.


Step 1 – Pre-Requisites

Before we jump into the code, we need to check one setting on your WordPress installation. This is important as it’ll allow to execute your plugin and this is normally configured in WordPress by default. But let’s check this just in case…

  1. Navigate to the Administration side of your WordPress site.
  2. Click Appearance => Theme Editor.
  3. A list of Theme Files will open on the right side of the screen.
  4. Find Theme Header (header.php) file and click it.
  5. Confirm wp_head hook is referenced inside of the file, as per picture below.

WordPress header.php


Step 2 – Writing Code

OK, now the pre-requisite is done, let’s move our attention to the actual Google Analytics Global Site Tag (gtag.js) plugin for the tracking.

To write the plugin, you’ll need an editor, such as Visual Studio Code or Notepad++ where we’ll be creating a PHP file.

  1. Open the editor and create new PHP file.
  2. Paste the following code snippet into your file.
<?php
/*
Plugin Name: My Google Analytics Plugin
Plugin URI: https://mysite.com
Description: Add the Google Analytics tracking code to your site header.
Author: Me
Version: 1.0
*/

 function qu_google_analytics() { ?>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=XXXXXXXXXXXXX"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'XXXXXXXXXXXXX');
    </script>
  <?php
  }

add_action( 'wp_head', 'qu_google_analytics', 10 );
  1. Replace comments at the top with your information and most importantly, paste your Google Analytics tracking code where the XXXXXXXXXXXXX placeholders are.
  2. Give the file a name my-google-analytics-plugin.php and save it.

Step 3 – Installing your Plugin

Once you have your my-google-analytics-plugin.php saved, you’ll need to install it into WordPress plugins store. Let’s do this next.

If you host your site on Windows IIS the chances are you will find the following folder structure the root of your site’s file system (picture below).

WordPress webconfig

  1. Navigate to your site’s file system using your preferred FTP client, such as FileZilla.
  2. Find wp-content folder, then inside go into plugins.
  3. Inside plugins create a folder for your Google Analytics plugin, maybe something like this my-google-analytics-plugin.
  4. Copy your plugin my-google-analytics-plugin.php file into that folder.

Step 4 – Activating and Using your Plugin

Finally, it’s time to start tracking visits to your WordPress site with Google Analytics. All we have to do now it to activate your plugin.

  1. Login to your WordPress Administration suite.
  2. Navigate to Plugins => Installed Plugins.
  3. Find your plugin My Google Analytics Plugin and click Activate.
  4. Now you site is ready to sent traffic information to Google Analytics.

If you are serious about Search Engine Optimisation (SEO) on your WordPress site then you’d also want to make sure your WordPress site is always serving content over HTTPS protocol.

Read one of my other posts explaining How to redirect HTTP to HTTPS on WordPress.

Marcin Narloch

Marcin Narloch

Creative and out-of-the-box thinker with strong interests and knowledge in technology and innovation.
WordPress MySQL Config Previous post Changing MySQL Database Settings in WordPress
WordPress PFX Certificate Key Vault Powershell Next post Retrieve PFX Certificate from Azure Key Vault with Powershell

Leave a Reply

Your email address will not be published.