WordPress and Google Analytics

How to add Google Analytics to WordPress

Add your Google Analytics tracking code to every page on your WordPress in 4 simple steps.


  1. Check your theme
  2. Prepare your Google Analyics
  3. Create plugin
  4. Activate plugin


  • Time needed: 20-40min.
  • Programs you need: A running WordPress, a FTP client like FileZilla (free) and a text editor or a coding tool of your choice.
  • Access you need: Administration rights to the FTP server and a Google Analytics account.

In some themes you can simply include the Google tracking code snippet Google Analytics provides to you to your websites <head></head> under ‘Appearance > Theme Options’ (label may vary). You may also be required to activate custom header codes and save the changes, before the code is included to each page on your site.

If there is no such option however, you have different possibilities to add the tracking script to your WordPress. You can install a plugin like Analytics+ that gets the job done for you and also gives you access to your analytics dashboard within your WordPress. If you want to keep things separated however, this tutorial is for you.

This tutorial is a shorter recreation of Rachel McCollins work.

Let’s get started:

1. Check your theme

Log into your FTP client and find the folder where the running theme is installed to. (‘/websites/wordpress/wp-content/themes/theme_name/’) Drag the header.php file to your desktop and open it. Make sure it contains <?php wp_head(); ?> in its head.

Themes you downloaded from WordPress directly, or WordPress default themes should have the wp_head hook included. You should consider choosing a different theme, if wp_head is not included.

Note: Do not include the tracking script here already. If you’d do, you’ll firstly lose your changes whenever the theme is updated and secondly, the very same tracking code would be used in EVERY page using this theme. So in case you manage multiple sites with one WordPress using the same theme, Google Analytics will merge the clicks from different sites.

If your theme header.php contains the marked line, you're good.
If your theme header.php contains the marked line, you’re good to go.

2. Prepare your Google Analytics

Login to your Google Analytics and get your tracking code.

Create a new account: Go to ‘Admin’ and click ‘Account > Create new account’, fill out the form and hit Get Tracking ID. Copy the shown website tracking script. This works similar, if you want to create a new property within an existing. Click ‘Property > Create new property’ and follow the description.

create new google analytics account
Create a new account on Google Analytics
create new google analytics account 1
Fill out the forms with your information.

Use an existing account: Go to ‘Admin’ choose the account you want to use. Click ‘Tracking Info’ within the property column and hit Tracking code. Copy the code.

You tracking-ID and website tracking code
You tracking-ID and website tracking code.

3. Create plugin

Create a text editor file on your desktop via right click ‘New > editor’ and paste the tracking code. Next, copy

Plugin Name: My Google Analytics Plugin
Plugin URI: http://autario.com
author: autario.com
Description: This plugin adds your Google Analytics tracking code to the head of any theme you use, as long as it allows wp_head hooking.
Version: 1.0 */

function my_google_analytics() { ?>


add_action( 'wp_head', 'my_google_analytics', 10 );

and paste it to the very beginning of the document you copied your tracking code to. Now cut your tracking code, and put it INSTEAD of the line saying ‘Delete this line and copy your Google Analytics tracking code here!’.

Save the file as my_google_analytics.php and upload it to the folder ‘/websites/wordpress/wp-content/plugins‘ on your server.

Your code should look similar to this now.

google analytics plugin

4. Activate plugin

Activate the plugin under ‘Plugins’, and you’re done. Check if it worked, by clicking Sent test traffic on your Google Analytics account. You find it on the same page where you get your tracking code. Go to Home, choose your account and adjust the time frame of your overview (you have to include the actual date, otherwise it will take a day to show up) or click on ‘Real Time > Overview’, if you just visited your site.

Note: If you excluded your own IPs from your analysis, which I strongly recommend, since you do not want to measure your own behavior on your site, and your traffic will highly manipulate the data (unless you have millions of clicks per day), you might need to use devices with different IPs. F.e. your smartphone via mobile web.

Share this

Leave a Reply

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