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.

WordPress Themes & Website Support for Independent Hotels & Inns

HermesThemes is a family-run business that helps independent hoteliers and innkeepers create, maintain and improve their websites. Since launching in 2013 we have helped hundreds of independent property owners to take control and make sense of it all.

Join to Download Our ThemesAsk a Question