How to Add Google Analytics to a WordPress Website

There are many ways to add Google Analytics tracking code to your website. Whichever solution you choose, the objective is the same one: add some lines of code to your website’s header.

Method 1: Manually Add the Code to the header.php File

Probably the quickest and easiest way to do it, it requires a simple code edit.

Just go to Appearance > Editor > header.php and add your Google Analytics code right before the ending </head> tag or after the opening <body> tag.

Add Google Analytics to WordPress by Editing header.php
Add Google Analytics to WordPress by Editing header.php

Advantages of this method:

  • Easy and quick to accomplish;
  • If you add this code to header.php, you know for certain it will appear on your website.

Disadvantages of this method:

  • If you change your theme, you will have to re-apply this solution;
  • If you update your theme, chances are you will lose this code;
  • On some WordPress installations the Appearance > Editor page might be disabled for security purposes.

Method 2: Manually Add the Code to the functions.php File

Another method that requires file editing, many people advise to stay out of header.php and add this code to functions.php instead, via a custom function.

Go to Appearance > Editor > functions.php and use a function similar to this one:

<?php add_action('wp_head', 'add_google_analytics');
function add_google_analytics() { ?>
// Paste your Google Analytics code here
<?php } ?>

Add Google Analytics to WordPress by Editing header.php
Add Google Analytics to WordPress by Editing header.php

Advantages of this method:

  • Adding this code to functions.php will keep your header.php file lighter and easier to manage, without the code there.

Disadvantages of this method:

  • If you make some PHP syntax error in functions.php then you might see a white screen instead of your website, which means that the Dashboard and the website will become unavailable until you fix the error;
  • + All the disadvantages of editing header.php (1st method);

Method 3: Add Analytics Code by Using a Plugin

Using a plugin is the recommended way to add Google Analytics code to your website.
There are many plugins (link) that add the code, some of them can even integrate Analytics directly into your admin dashboard.

Advantages of this method:

  • The code will appear on your website at all times, no matter if you use one theme or another, if you update a theme, customize it, etc.

Disadvantages of this method:

  • You will have one extra plugin just for the Google Analytics code.

Method 4: If you are using a theme by HermesThemes

If you are using a WordPress theme by HermesThemes, then there is a 4th option available.
Simply go to the Dashboard > HermesThemes > Theme Options page, the Miscellaneous tab, and add your Google Analytics code there.

Add Google Analytics to WordPress from the Theme Options page
Add Google Analytics to WordPress from the Theme Options page

Advantages of this method:

  • You don’t have to install any new plugins or edit any files;
  • You can customize your HermesThemes theme without worrying about deleting this code.

Disadvantages of this method:

  • If you decide to switch to a non-HermesThemes theme, you will need to re-include the GA code;

In Conclusion

All of the above methods are fine, as long as you understand the pros and cons of each one. Whichever method you decide to use, just don’t forget to make sure that your stats are being gathered properly and that you are not missing out on precious data about your website.

Author: Dumitru Brinzan

Founder and WordPress developer at HermesThemes.
With over 7 years of experience in the hospitality industry, it is my strong belief that WordPress brings unprecedented opportunities for independent hotels, B&Bs and inns.
Over a thousand of amazing properties worldwide use our Hotel WordPress Themes on their websites.

 
  • ianeverdell

    Just a quick note for #2 – it should be ‘wp_head’ not ‘wp_header’.

    Thanks for the reminder on how to do this!

  • Ariana Nicole

    Thank you for this post, it helped me choose the best method to add Google Analytics to my website. Method 3 seems most ideal, but I am curious about the disadvantages? They contradict the advantages for theme changes and updates.

  • Jackson Pcy

    this is a very easy to understand post… short and direct. nice info there

  • indra utama

    Thank you for sharing, wery useful post!

  • Cypherinfo

    Hello, your tip about the method 3: first you say it is not “linked” to any theme then you say the exact opposite!!

    • That was an error in the article, I have corrected it, thank you for spotting it :)

  • Cypherinfo

    Hello, your is a very important article because of it invites us to reflect to the best option to select when it comes to add a new functionality so not just promote something. I wonder what is be the plugin you would select to install if you should need to track information for periods of six months and the OS language of any visitor among the other traditional pieces of information. Thank you.

  • Nice article! Quick question: Do you need to post the code in every new blog post? (assuming I don’t paste it into the header)

  • Awesome Guide once again. Google analytics is a very awesome web analytics tool.