How to Install Google Analytics in WordPress

Installing Google Analytics on a self hosted WordPress website

A question that I see often on the Google Analytics Community, is how to install Google Analytics in WordPress.

Usually when installing Google Analytics to a website, the tracking code gets added to each page before the closing </head> tag. When using WordPress, the tracking code usually only need be added once to the correct file within your theme. This will then populate all existing and future pages and posts with tracking code before the closing </head> tag, removing any need to manually add it to the code of each page/post.

This article covers the different methods most used by site owners, to install Google Analytics on WordPress.

Method 1: Adding the tracking code to the active theme’s header.php file

Not Recommended
Manually editing and hard coding changes into core WordPress theme files goes against WordPress “best practices”.

  • If the theme receives an update, any code edits will likely be overwritten and removed.
  • Each time a theme is changed, the tracking code will need to be reinstalled into the header.php of the new active theme.
  • Someone unfamiliar with WordPress and/or coding can easily break their website if a coding error occurs or a file is uploaded incorrectly to the server.

Method 2: Adding the tracking code via a provided theme setting


  • Requires no editing of core theme files
  • Tracking wont get overwritten if the theme receives an update
  • Tracking code will need to be reinstalled if the theme is changed

Whether this method is available, is entirely dependant on the theme you are using. Genesis by Studiopress is a commercial theme that gives the user the ability to add scripts to either the <head></head>​ or the footer of the site just before the closing </body> via the theme settings.

Genesis theme setting to add scripts to wp_head

Divi by Elegant Themes is another theme that also has an option for adding script to the <head></head> section

Divi theme setting to add scripts to wp_head
Check your theme documentation to see if it offers a similar option for adding scripts.

Method 3: Using a child theme to add the tracking code

A child theme is dependant on it’s parent theme being installed, and will inherit most of it’s functionality from the parent theme. If your active theme doesn’t have a theme settings option to add scripts to the head, then you may want to consider creating a child theme to make code related changes to your site. Using a child theme follows WordPress “best practices”.

  • Child themes remove the need to edit core theme files.
  • If coding errors are made, a child theme can usually be easily fixed, or deactivated without breaking the parent theme.
  • Parent theme updates will not overwrite the edits and changes made in the child theme.
  • Unfortunately, each time you change your theme, you’ll need to create a new child theme.

You can find information about how to create a child theme in the WordPress Codex.

There are two different ways you can add the tracking code … take your pick:

Copy the header.php file from the parent theme directory into your child theme directory and edit it to include the tracking code before the closing </head> tag,

or Create a php function that includes your tracking code (such as the following example), and put it in the child theme’s function.php file

Without going into a huge, long explanation, I’ve called the function bronv_ga_tracking_code. I’ve used add_action, in this instance, it hooks my function with the tracking code to the wp_head action hook, which gets fired in the <head></head> of my site and outputs my tracking code within those tags. I’ve given it a priority of 200 which means my tracking code should pretty much be the last thing to be output just before the closing </head> tag

– Always clearly comment your code, (so you or someone else working with your code can easily recognize what the code does).
– Always prefix your functions to avoid possible naming conflicts with theme/plugin functions, (I usually prefix my functions with bronv).

Method 4: Using a plugin

My Preferred and Recommended Method
Using a plugin, separates non theme related functionality from the theme. This helps to ensure the functionality provided by the plugin, will keep working if the theme is changed or updated. Google Analytics tracking has zero to do with the theme’s appearance or the workings of the theme, so it makes sense to have the tracking installed via a plugin.

  • No code edits/changes are manually made to core theme files.
  • If the theme changes, tracking will continue to work and wont need to be reinstalled.
  • For the most part, theme updates should not affect the plugin.
  • Sometimes plugin updates will cause something to break (this is not something happens often – if ever).

There are a number of free plugins available on for installing Google Analytics. Two of the most widely installed are: Google Analytics Dashboard for WP and Google Analytics by Yoast.

When considering plugins, make sure to check out the plugin details

Google Analytics WordPress Plugin Description Details

The easiest method for installing the plugin, is directly from the WordPress plugin repository via the plugin interface.

Installing directly from the WordPress Plugin Repository:
Click on the Plugins link then the Add New option and then type Google Analytics into the search box.
Search WordPress Plugin Repository

From the Search Results page, find the plugin you want to install and ensure it is up to date and compatible with your version of WordPress, then click the Install Now button:

Check plugin details

Once installed, activate and follow the setup/configuration instructions for the particular plugin. Each plugin will be different.

Activate WordPress plugin

Take the time to read the Installation, FAQ and Other Notes pages (if available). Sometimes they will contain links to videos with helpful configuration walkthrus and tips etc.

If you have problems with a plugin not working as expected, contact the plugin creator via the plugin’s support page on  (unless the plugin creator has stated there is an alternative and preferred method for getting help).

If you need help with Google Analytics, visit the Google Analytics Community to ask your questions!