In this article I describe how to install Google Analytics on your WordPress website using the Header and Footer plugin.
There are many ways to install GA: you can use specific plugins and many of the SEO tools (like RankMath) do this for you too. While those approaches do make it easy for you the advantage of using this method is that it makes it easier to apply addition tools that require you to add tags to the header, such as the Facebook Pixel. In my view, if you do it just this one way it will be easier to manage.
Currently there are two ways of doing this – using the old Universal Analytics tag or the newer GA4 tag. This article assume that you have already created a tag and provides instructions on adding it to your website.
The process is the same whether it’s a GA4 or a UA tag, however there is one extra step required for UA tags in order to ensure the data you collect protects private information of visitors to your site, which is important for compliance with GDPR.
How to Set up Google Analytics Property on your WordPress Website using the Header and Footer Plugin
Time 10 minutes
Create your Analytics Property
The steps for creating an Analytics property are not included here and it is assumed that you have already completed this step.
Open Google Analytics.
If you have more than one account, select the Account you want to use and and the appropriate property.
Click the Admin wheel (at the bottom of the left menu sidebar) and the select Setup Assistant.
Click on Tag installation and select Web from the Data Streams list.
Now you’ll see information about the tag to use on your website, including the Stream URL (your website address), the Stream Name (this will match the Property settings) and the Measurement ID.
Scroll down to the Tagging Instructions section and select Global site tag (gtag.js) and copy the code.
Copy the Tag Code into your Header and Footer Plugin
Copy the gtag.js code to your clipboard.
Go back to your WordPress website and select Tools and then Insert Headers and Footers.
Paste the gtag.js code into the Scripts in Header section.
The final step is to go over to Google Analytics at the same time as visiting your site in another open tab or browser window.
If you don’t see anything and you’re running a cache tool (such as LiteSpeed) on your website, go back into the WP backend and clear it. Then go back to GA and check.
Go to the Real Time view and you should see a dot on or around your location . This means that it’s working.
Just type (or paste) your URL into the field and click the blue arrow button. The result will be shown below.
Choosing a Method for adding your Google Analytics Code
Although I wrote about the Code Snippets approach I think this is by far the easiest method. It’s also much simpler (and cleaner) than using a dedicated plugin because it’s so flexible: you could use this for adding Hotjar trackers, Facebook Pixel, Tag Manager – you name it. It’s fast and pretty simple and while it might seem daunting because you’re handling “code” you’re only copying and pasting so there’s really not much that can go wrong.