Adding Google Analytics to WordPress (With & Without Plugins)

Find Google Analytics Tracking ID

Depending on how you want to add Google Analytics to your WordPress site, you'll need either the Tracking ID or tracking code from Google Analytics.

  1. Log into Google Analytics.
  2. In the top left, select the account and property you want to track.
  3. In the bottom left, click Admin.
  4. In the Property column, click Tracking Info, then Tracking Code.
  5. Page will show Tracking ID and Global Site Tag (gtag.js).
Analytics tracking ID

Note: This page contains affiliate links. Please see Affiliate Disclosure.

Add Google Analytics with a Plugin

The Google Analytics plugin we use is Google Analytics by MonsterInsights (formerly Google Analytics for WordPress, by Yoast). It has several benefits over other ways of adding Google Analytics to your site:

  • You won't lose your tracking code if you change your theme.
  • You can ignore (not track) WordPress user roles you specify, which makes your stats more accurate.
  • You can see your analytics right in your WordPress Dashboard.

Here's how we configure the plugin:

  1. Install and activate Google Analytics by MonsterInsights. WordPress admin menu > Insights > Settings:
    1. General tab
      1. Google Authentication: Click here to authenticate manually. Manually enter your Tracking ID (UA code).
      2. Check box for Disable Dashboard (we prefer to not show Google Analytics in the WordPress Dashboard).
      3. Set Automatic Updates Settings to Minor Only.
      4. Check box for Allow Usage Tracking.
    2. Tracking tab
      1. Engagement
        1. Ignore users: select/add all except Subscriber (and Customer, if present)
  2. If you're using WooCommerce, install WooCommerce Google Analytics Integration. Configure WooCommerce > Settings > Integration > Google Analytics. Set following:
    1. Google Analytics ID.
    2. Enable Universal Analytics.
    3. Enable Enhanced eCommerce.

If you'd like to use a plugin but the MonsterInsights one is too heavy/complicated for you, consider the Header and Footer plugin which allows you to add code to your WordPress header. It doesn't have the features of the MonsterInsights one, but at least it avoids the problems you'll face if you add code directly to your theme (as described in the rest of this post).

Add Google Analytics Without a Plugin

Some themes and theme frameworks have a place to add Google Analytics to the theme options/settings. Important: If you ever change themes, you must remember to add Google Analytics to your new theme. However, if you use a framework such as Genesis, you can change child themes without needing to do anything with Analytics, because the settings are in the parent theme rather than the child theme.

In Genesis, you can add the tracking code to WordPress admin menu > Genesis > Theme Settings > Header Scripts.

Genesis Header Scripts

Another option is to put the tracking code into the functions.php file in your theme. Important: Only do this if you use a child theme. If you don't use a child theme, your functions.php file will be replaced each time you update your theme, which will erase the Analytics code from your site. Also, if you ever change themes, you must remember to add the tracking code to your new theme. Because so many people forget, I recommend using a plugin instead (see the first half of this post).

Code to add to functions.php:

<?php
add_action('wp_head', 'ow_add_google_analytics');
function ow_add_google_analytics() { ?>
 
// Paste tracking code from Google Analytics
 
<?php } ?>

You Could Keep Googling for Answers, or …

"How do I add Google Analytics to my WordPress website?" is just one of the myriad questions you’ll face as you manage your website and online marketing. Do you want to google for answers every time you encounter a question? Save yourself the hassle and headache, and simply work with a web agency that can guide you through the entire process.

Filed Under: 
Tagged With: ,

Want tips to rocket-boost your website?

Simply sign up.

7 comments on “Adding Google Analytics to WordPress (With & Without Plugins)”

  1. Thanks for the info Chad. Is there a speed drawback when using a google analytics plugin vs adding code functions to php?Also, on this site I didn't see a cookie tracking popup, is this required when using Google Analytics? Would be nice if there was a section on GDPR compliance. Perhaps a separate a post about GDPR and what you need to implement to be complaint.

    1. Prez, I can't think of a reason that a Google Analytics plugin would be slower or faster than putting the code into functions.php. Either method should just load the Google Analytics script asynchronously, which shouldn't delay page load.

      I'm not a lawyer, but my understanding is that the EU has much stricter requirements about cookie notices, and that GDPR is an EU regulation. Our clients are US businesses operating within the US, so we haven't yet needed to familiarize ourselves with those requirements. If you operate within the EU, it would definitely be wise to seek legal counsel on the requirements. Even for businesses that operate only within the US, it doesn't hurt to have a privacy policy that mentions cookies, as we do in our Privacy Policy.

  2. Merci pour cet article si interressant; Celui-ci tombe vraiment à pic au moment où, je fais des recherches sur comment insérer le code; Toutefois, je voudrais savoir est-ce utilisé un plugin ou copier le code dans la balise head, comporte pas des risques (questions confindentialités)?

  3. Hi Chad, I was wondering: I use GA as well as the plugin. But I am considering deleting the plugin, since you cannot make filtered views? So on the dashboard I see only the view that also includes ghost visits. Is this correct or can I change views? By the way, I'm usin Google Analytics Dashboard for WP. Regards!

  4. I am glad to find it. Google analytics is very important tool for SEO process. You have explained everything in detail about google analytics WordPress plugins tips. Thanks for such post and please keep it up.

Ready to Blast Off?

Let's talk.

Contact OptimWise
crossmenuarrow-right