Add Google Analytics to WordPress

Categories SEO, WordPress
Add Google Analytics to WordPress

In this article I will explain you how you can add your Google Analytics property to WordPress. Furthermore I will guide to through the implementation of your very first WordPress plugin (in case you never coded one before). Don´t be plunged! The plugin we will code is very simple and actually only includes the Google Analytics tracking code. So let´s start, let me explain you how to add Google Analytics to your WordPress blog.

Requirements of Google Analytics for WordPress

Well before we can start I will show you the few requirements you need to add Analytics to WordPress.

  • First of all you need a running WordPress installation. Google Analytics does not track traffic on a localhost installation.
  • Then you need access to your WordPress files via FTP.
  • And of course you need a Google Analytics property assigned to your website and it´s tracking code.
  • The last thing you need is a code editor 🙂

Get the Google Analytics tracking code

I assume you already signed in to GA and also created the property for your website or blog. To get the tracking code you have to go to
Admin > Tracking Info > Tracking Code

Add Google Analytics to WordPress
Google Analytics Tracking Code

It is actually pretty simple, no? So now that you have your tracking code we can continue. The next step is the plugin! But don´t worry it is only one file that we have to create and I will give you the whole code for it.

Create your own Google Analytics Plugin

Now you have to access your WordPress files via FTP. This step is not possible without access to your files. To create the plugin go to the WordPress Plugin directory


and create a new file called something like my-google-analytics-plugin.php. Insert the following code into your new file:

WordPress Google Analytics Plugin
Head of your first WordPress GA Plugin

This is the required syntax of WordPress plugins. Actually there is no functionality in it, it is only there to tell WordPress that this file is a Plugin. Of course you can change the Plugin Name, Description, Author and Version.

Now let’s get some functionality to it.

How to add Google Analytics to WprdPress
The functionality of your Google Analytics Plugin

Paste this line of code below the first code snippet. After you copied it you should replace the part between the script tags with your own Google Analytics tracking code. And that´s it, you just created your very first WordPress plugin. Congratulations.

There is only one thing left. Like every other WordPress plugin you have to activate yours as well. After you activated it, you can test if everything works properly by checking the Real Time Report in Google Analytics.

Add Google Analytics to WordPress
Rate this post

Leave a Reply

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