If you have a website and want to study your audience in details, Google provides an amazing (and free) platform called Google Analytics. It is a web based platform that you can use to understand your website visitors a bit more – their age, gender, locations, the devices they use to visit your website, the pages they visit, the average time spent engaging with your website, their user journey as they navigate from one page to another and a lot more.
If you haven’t yet integrated GA (as it is fondly called) in your website, this post will help you immensely. Although there are different ways to integrate Google analytics, we’ll be focusing on how to add Google analytics code in your website without using a plugin. Why add another plugin to clutter your WordPress website if you can do without one, right?
Before we implement the process, let’s see how your website will look without the tags. You can make use of a Chrome extension called Tag Assistant Legacy (by Google), and once added to your browser, it shows the number of tags fired on a particular page. Since we haven’t yet associated our Google Analytics account with our website, it’ll look blank as shown below:

We’ll come back to this extension a bit later when we set up Google analytics on your website. So let’s get to it.
How to add Google Analytics code in your website without a plugin
1) Login to your Google analytics account by visiting: https://analytics.google.com
2) Once you’ve logged in, you will require the Global Site Tag (gtag.js) to add it to the header of the website. If you’re using Google Analytics 4, you will find the global site tag on your Web Stream Details page as shown:

3) Copy the gtag.js code in your notepad locally.

4) If you’re using Google Universal Analytics, you will find the Global Site Tag on the tracking code page, as shown below. Copy the code as we’ll need it soon.

5) Next, login to your WordPress site. At WP Adventure, we almost exclusively use the Astra theme to build WordPress websites. Go to Appearance and click on Theme Editor.

6) Select the theme from the dropdown that you want to edit. Search for a file called header.php and click to open.

7) Paste your Global Site Tag code (gtag.js) just above the closing </head> tag as shown below. This is the same code that you copied from your Google analytics account in step 2 – 4 above.

8) Click Update file to save it.
9) Technically, we’re done at this point however it is always a good practice to verify if it has been implemented correctly. Let’s revisit the Chrome extension to check if the tag is firing correctly. And if you’ve followed the steps outlined above correctly, the tag does fire as shown below:

That’s it. Job done. You’ve added your global site tag in your website to connect your Google analytics account without using a plugin. It was very easy and straightforward, wasn’t it? You can now sit back and watch the data flow in your Google analytics account as your visitors navigate the various pages of your website.
If you just want to stick with the default features provided by Google Analytics, using the gtag.js will be sufficient. However, it has limitations such as its ability to work with only other Google tools or difficulty in implementing more advanced tracking such as link clicks or button clicks etc.
Our recommendation? Use Google tag manager instead. We’ve written a post that will help you add Google analytics code in your website using Google tag manager. It’s a much versatile solution and we always take that route over the one above. Go, give that a read and decide!