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.
Note: This page contains affiliate links. Please see Affiliate Disclosure.
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:
Here's how we configure the plugin:
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).
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.
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 } ?>
"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.
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.
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.
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)?
Ruffin, I don't know French, so sorry if I misunderstand your question. I'm relying on Google Translate. Whether you use a plugin or add the code directly to your site, Google Analytics works the same way. The same data is collected and sent to Google. Here's an article that describes how Google Analytics deals with data and privacy.
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!
Tessa, we view Google Analytics in the Google Analytics website rather than within the WordPress admin area. I'm not familiar with which views or filters can be displayed within the WordPress admin area. I suggest asking in the support forum for the plugin you're using (here's the support forum for Google Analytics Dashboard for WP).
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.