Check this Out!Visit our social network pages for recent updates!

Lesson 20: Create Custom Google Analytics WordPress Plugin

Students lesson 20 assignment: How to you track WordPress website user traffic?

For students who want to know how many people are visiting their websites may need to install website traffic analytics plugins or codes. These plugins and codes capture web traffic and record them providing overview of the website performance and popularity.

One of the most widely used analytics codes is Google Analytics. If your website isn’t using Google Analytics, then you’re probably doing something wrong.

Most Google Analytics implementations are done by simply copying the tracking code and pasting it onto your web pages. This method is the most commonly used.

The thing about the above method is, everytime you update your theme, you’ll have to re-add the tracking codes, since updating the plugins remove custom changes.

This brief tutorial is going to show users how to create a simple Analytics plugin for WordPress. When this plugin is used, changing or updating WordPress themes won’t affect your website tracking as it’s installed as a plugin.

Prerequisites

Your first task is to have WordPress installed. To learn how to install WordPress, please follow the link about installing WordPress below:

How To Install WordPress On Ubuntu Servers

Method #1: copy and paste codes

The most popular method to use Google Analytics as I said earlier is to copy and paste the tracking codes to your website pages.

This tutorial assumes that you already have registered for a free account of Google Analytics. If not, go and register from this link

https://analytics.google.com/

Next, copy the tracking codes from your analytics account and go to your WordPress website. Then navigate to Appearance –> Editor and select Theme Header (header.php) templates as shown below. Paste the analytics codes in the highlighted box, just before the closing tag for </head>

Google Analytics WordPress plugin

Save the header.php template and you’re done.

That’s the easiest way to add Google Analytics to your student websites.

Method #2: Create custom Analytics Plugin

The second method shows you how to create custom Analytics plugin to use on WordPress.

Method 2 assumes that you have administrative rights to your WordPress host. When you’re ready, navigate to your WordPress install directory, usually at /var/www/html/.

Then go to /…./wp-content/plugins  directory.

In the plugin directory, create a file named google-analytics.php  The name can be anything you like to call it.

sudo nano google-analytics.php

The basic setup for any WordPress plugins are these lines below:

<?php
/*
Plugin Name: Google Analytics Plugin
Plugin URI: https://websiteforstudents.com
Description: Adds Google analytics tracking code to your theme using wp_head.
Author: Richard
Version: 1.0
*/
?>

Now that the basic parameters have been set in the page, the next lines are the actual codes to make the plugin work.

<?php
function 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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-83166777-22', 'auto'); ga('send', 'pageview');
</script>
<?php }
add_action( 'wp_head', 'google_analytics', 10 );
?>

Replace the highlighted section of the code with your Google Analytics codes and save the file. You may have to reload your website depending how your site is setup.

The entire codes for the plugin should be as shown below:

<?php
/*
Plugin Name: Google Analytics Plugin
Plugin URI: https://websiteforstudents.com
Description: Add Google analytics tracking code to your themes using wp_head.
Author: Richard
Version: 1.0
/
?>
<?php
function 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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-83166777-22', 'auto'); ga('send', 'pageview');
</script>
<?php }
add_action( 'wp_head', 'google_analytics', 10 );
?>

Finally, go to your WordPress plugin page on your site and you should see the custom plugin there. All you have to do now is activate to begin tracking your website traffic.

That’s it!

Enjoy!