"This Post contains affiliate links; we earn commissions if you shop through the links on this page."
I switched from Authority Pro to the WP Astra theme in May 2022. During the website redesign, I had one hiccup, how to add Google Analytics to Astra theme header.
I was able to fix it using one of the methods explained below.
If you’re in the same situation, you will find three options on this page to add Google Analytics code to Astra. Two of the options are suitable to execute if you’re using the free Astra theme.
The other method is for Astra Pro users. If you’re in a hurry, you can use the Table of content by the sidebar to navigate through this content.
Before you continue, I will assume you already have your Google Analytics code ready, if not, let’s create one together…
Sign up for Google Analytics 4
Before you can get your Google Analytics code, you need an Analytics account.
If you don’t have one, it’s easy. The only requirement is a Gmail account and your website URL. If you’re already a Gmail user, you can create a Google Analytics account at https://google.com/analytics/
Note: Google will sunset the universal tracking code in July of 2023, so, you should create a GA4 analytics code instead.
After signing up, log in and create your web property, then follow these steps below to get your Google Analytics 4 code.
How to Get Your GA4 code in Google Analytics
Here are the steps to find your Google Analytics 4 tracking code;
- Log in to into Google Analytics account
- Click on Admin from the left bottom
- On the right side of your screen, click Data Stream
- Select your website property from the stream
- Scroll down the page and click on view tag instructions.
- Select install manually. Next, copy the code.
Now, let’s installed the code in your Astra theme header.
Option 1: Astra Hook
If you’re using the free Astra theme, you should follow this method.
First, install and activate the Astra Hook plugin. It’s free so you have nothing to fear. The Astra Hook plugin allows you to add custom or third-party scripts to the footer, body, content, sidebar, and header location without editing your theme files.
You can install it directly from your WordPress admin or via the WordPress plugins library. I prefer to install directly from the WordPress dashboard. It’s quicker and saves time.
Type Astra Hook in the plugin search box, install and activate. Next, we need to add the Google Analytics tag to the Astra theme customizer.
By now you should have your GA4 snippet with you.
So, follow the steps described here to add your Analytics code to Astra using Hooks.
From the WordPress admin dashboard, hover your mouse on appearance > Customizer > Hook> Header > wp_head.
Insert the Google Analytics code, and don’t forget to click the publish button. If you prefer a walkthrough visual tutorial, here is a simple step-by-step guide to follow.
That’s it…you can start tracking website users’ data such as traffic sources, time on site, bounce rate, landing pages, etc.
Option 2: Astra Pro Plugin
For Astra Pro users, the recommended method is through the custom layout module. But you need to have the Astra Pro add-on Plugin installed and activated to have this module available.
The Astra Custom layout model allows you to create custom 404 pages, custom content layouts, pages, Hooks, etc.
So first, install and activate the Astra Pro addon-on plugin. If you don’t have it yet, you can follow this link to purchase one from the official Astra website.
After your purchase, activate the add-on plugin to enable the custom layout modules and others. Next, from the WordPress admin, go to appearance > Astra Options – scroll down the page to activate Custom Layout Module.
After activating the custom layout, hover your mouse on appearance once again, and click on Astra Option.
Click Add New Custom layout and give it a name to remember its purpose. Alternatively, you can edit the default custom layout.
Next, paste the Google Analytics code into the code editor, scroll down the page, and select Hooks from the layout option. In the action field, select wp_head.
In the “display on” option field, select the entire website. And click publish.
So, that’s how to add Google Analytics to the Astra theme using custom layout modules.
Option 3: Head and Footer WordPress Plugin
Another easier option is to use the WordPress Insert headers and footers
+ Custom Code Snippet plugin.
This method can be the best option if you’re not planning on using the Astra theme for long. This is because the head and footer WordPress plugin is independent of any WordPress theme.
If you ever switch to another theme in the future, you won’t lose any tracking script you’ve added to the WordPress head through the plugin.
Plus, you can use the plugin to add script, HTML, or text snippets to any part of your website. You can define whether the code is sitewide, or in a specific location, page, post, or custom post type.
The plugin is available for download at wordpress.org or through your admin dashboard.
FAQ – How to Add Google Analytics Code to Astra
Can you use the Astra theme for free?
Yes, Astra is 100% free to use. I’m using the free version for now with little to no need to buy the Astra Pro or add-on.
Though your needs might be different from mine, it is worth comparing the Astra Pro features vs the free theme. If there are features you need not available in the free version, you may consider buying the Pro version instead.
What is the difference between Astra and Astra Pro?
WP Astra is the free theme available for download at the WordPress theme repository. It has all the tools and theme features you need to build a stunning website design. While the Astra Pro is the premium add-on that brings more functionalities, features, and integration to the WP Astra theme.
You can read the complete comparison between Astra vs Astra Pro on this page.
Why do I need Astra Pro?
You don't need Astra Pro if your website design needs are limited to basic functionalities.
But if you need more functions like e-commerce features, full and seamless integration with other marketing tools, custom design, and page layout, and more control over your site designed for things like color, background, width, custom header, footer, priority supports, White label, etc…you need Astra Pro.
Check out Astra Pro features here.
Why is Astra the best theme?
No doubt, Astra is one of the best WordPress themes in the market. With 1,653,898 websites using Astra, it’s certainly the most popular.