July 27, 2017 admin 0Comment

Analytics are an important part of a system to keep an eye on insights. Google analytics for websites and web-apps has become a key player in this race. It gives you tools you need to analyze data for a deeper understanding of the customer experience.

Google has made the integration of their analytics software quite easy, that it’s just a matter of copy and pasting the code in the pages of your website. Generally, you have to add the code in every page, you want to track (or analyze, in other words).

The code usually looks like:

As I mentioned above that you’ll have to add this code (or the actual code from your GA account), to the every page you want to track.

This scenario is an easy go with a templating system, which allows you to reuse a certain part of the code or content. In a WordPress system you have this opportunity. You can do this using different approaches. For example:

  1. Enqueuing the GA code directly in the theme
  2. Creating a plugin as a more robust solution

In the first option, it looks quite easy and straightforward. You can use  wp_enqueue_scripts action in your active theme’s  functions.php file. But what happens when you switch to another theme? Or, there’s an update to the current theme? In both cases, you will simply loose the GA integration. And, ‘ll have to add the code again.

However, the other method of creating a plugin, is much stable and highly maintainable. Creating a WordPress plugin isn’t a rocket science, but is all about fun and dedication.

In this article, we are not going to discuss much of the details about writing a plugin. But I’ll go straight and show you a simple plugin creation for the said purpose.

Let’s go step by step.

Step 1: Create a plugin file

As a first thing, create a simple .php file in plugins folder of your WordPress installation. For sake of understanding, let’s call it my-google-analytics-plugin.php

Step 2: Add basic plugin scaffold

WordPress recognizes plugin files by reading some particular markers in the beginning of a PHP file. Below is the simple and basic scaffold to make it recognizable by WordPress. Open my-google-analytics-plugin.php file in a code or text editor, and add following lines to it:

Notice some of the tags in above commented code, like Plugin Name, Plugin URI, Description, Author and Version. Feel free to change the text in-front of these to your choice.

Step 3: Create a hookable function (a.k.a. Callback function)

A hookable or callback function is executed when a particular event occurs. In a WordPress system there are two types of events; Actions and Filters. Here, we’ll use an action, which we will discuss in next step. However, this action requires a callback function. So when this action is fired, we can inject our GA code in a proper place.

Add following lines to your plugin file, after the previously added comment block:

Step 4: Spice up the callback

Now it’s time to grab your GA code from your Google analytics account and add it to the callback function. Simply, copy the Javascript code from your account and paste between the two lines (you added above). So the code in your file will look like followings:

Step 5: Hook it up

Finally, we need to hookup the callback function with the appropriate WordPress event. We are going to use  wp_head action for this purpose. As a last line to your file, add  add_action( 'wp_head', 'wpme_google_analytics', 10 );. Final code will look like following:

You’re done. Your plugin is ready…!

Simply save the file, go to the plugins page, activate the plugin and check your website. You should see page source to confirm the presence of your Google analytics tracking code. Visit different pages and posts to ensure the implementation.

As an added point, you can involve more logics in your plugin. For example, where you want to include this and where you don’t. I recommend reading about WordPress conditional tags.

Leave a Reply

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