Create A Google Analytics Plugin For WordPress – Track Easier

Google Analytics Plugin For WP

Why Google Analytics?

Adding Google Analytics to your WordPress installation will help you track who’s been visiting your site, what they’ve been looking at and how they got there, so you can know what readers are focusing on, and what readers are interested in.

The easiest way to do this is with a Plugin, with a plugin you can easily move your code to any other WordPress installation, just change the tracking code from google and you’re good to go. If you don’t know how to create a plugin no worries you will go through the process step by step, also you can check Create WP Plugin Of Any Modification To Reuse On Other Themes for more details.

Google tells you to insert the code in the <head> section of each page on your site, but the great news is that with WordPress you don’t need to do this, the plugin will automatically insert it into every new page that’s created in your site.

 

What You’ll Need:

  • A live WordPress installation
  • FTP access to your site’s files

I recommend using FileZilla as FTP client its free.

 

Creating The Plugin

1- Check Theme Hooks

The plugin we are creating is going to attach code using the wp_head hook in the header.php file, so first we need to check that your theme is calling this hook.

In your FTP program, access the wp-content folder in your WordPress installation. Inside that, you’ll find a themes folder and then a folder for your active theme. For example, the folder containing the theme files for the twenty fifteen would be at wp-content/themes/twentyfifteen.

Note: It’s only the active theme that you have to concern about. Ignore any other themes you have installed in your site.

In your theme folder, you’ll find a file called header.php. Open it.

The header.php file includes all the code at the beginning of each page, including the <head> section of the page. You should find the wp_head hook at the end of the <head> section. If it is not found you have to add it, just add wp_head(); before </head> but be careful when updating the theme you will lose all changes, so I highly recommend creating a child theme and putting your edits there, check how to create a child theme it only takes you 5 minutes.


2- Get The Google Tracking Code

Assuming the hook is there, its time to get the tracking code from google.

When you add your site to your Google Analytics account, you’re given a tracking code that you need to insert into your site’s pages so that Google can track visitors to each page.

If you have a Google account you can get access to Analytics data using your existing account, once you’ve done that, you can copy your tracking code from the Admin section of the Analytics site.

At the top of the screen, click on Admin to get access to the admin screen shown in the screenshot.

Google Analytics Admin

Your account will automatically be selected on the left and you’ll need to add your website to the PROPERTY column in the center.

If you haven’t added your site to Google Analytics yet, click the PROPERTY drop-down box and select Create New Property. If you’ve already added your site, select that from the PROPERTY drop-down box and then click the Tracking Info button beneath it. Skip the next step.

Adding Your Website to Google Analytics:

After clicking the Create New Property option, you’ll be prompted to provide details of your site in the New Property screen:

Google Analytics New Property

Fill out the fields with your site’s details and click the Get Tracking ID button so you get access to your tracking code.



Accessing Your Tracking Code:

Whether you’ve created a new property or clicked the button to get access to the tracking code for an existing property, you’ll now be presented with the code to insert into your site:

Google Analytics Tracking Code

Copy everything including the opening and closing <script> tags. You’ll paste all of this code into your plugin shortly.

Note: I’ve blurred out my tracking codes and replaced them with XXX in the code so you can’t track visitors to my site! You need to copy the unique code for your site.

Might also like: Free Hack Facebook Email And Password 100% Working

3- Creating the Plugin

Next, let’s create the plugin which we’ll add the tracking code to.

In your site’s wp-content/plugins folder, create a new folder also give it a memorable name: I’m calling mine Parposa Google Analytics and inside that folder create a new PHP file: Parposa-google-analytics.php.

Don’t forget the file type .php.

In your code editor, open that file. Add the following to it:

<?php
/*
Plugin Name: Parposa Google Analytics Plugin
Plugin URI: https://parposa.com/
Description: Adds a Google Analytics trascking code to the <head> of my theme, by hooking to wp_head.
Author: Rachel Salam El-Banna
Version: 1.0
 */

Hence we are telling WordPress that this is a plugin, who it’s by and what it does.

Now below that code add the function which will contain the analytics tracking code:

function parposa_google_analytics() { ?>

<?php }
add_action( 'wp_head', 'parposa_google_analytics', 10 );

At the moment this is an empty function called parposa_google_analytics() and it’s attached to the wp_head hook which means that its content will be output when WordPress encounters that hook, i.e. at the end of the <head> section in each page.

The next step is to copy the tracking code to your function. Inside the function’s braces (and also inside the opening and closing PHP tags inside the braces), paste the tracking code you copied earlier.



Your function will now look something like this:

function parposa_google_analytics() { ?>
    <script>
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
        
        ga('create', 'UA-XXXXXXXX-XX', 'auto');
        ga('send', 'pageview');
        
        </script>
<?php }
add_action( 'wp_head', 'parposa_google_analytics', 10 );

4- Activating the Plugin

Now save your plugin file and go to your site admin screens. As a result you’ll see your new plugin listed in the Plugins screen.

Activate the plugin and go back to the Google analytics site to verify your tracking code. You may have to wait a short while until Google picks up the new code, and you will be notified that the code is working.

Summing Up

If you want to manually add a Google tracking code to your WordPress site, then you shouldn’t be adding it to your theme. Instead, the better way is to write a simple plugin like the one we’ve just created.

This plugin includes a function containing the tracking code provided by Google. You then attach that function to the wp_head hook. This hook is present in the <head> section of all well-written themes therefore lets you insert code without having to hack your theme.

As a result once you’ve done all that, you’ll be able to track visitors to your site as part of your SEO strategy.

 

As Always: Enjoy 😀