Disclosure: CyberNaira content is reader-supported. We may receive financial compensation if you purchase products or services on the merchant website but at no additional cost.

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 to add Google Analytics code to Astra on this page.

Two options are suitable to execute if you use 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

You need an Analytics account before you can get your Google Analytics code.

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 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 bottom left.
  • 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 install the code in your Astra theme header.

Option 1: Astra Hook

You should follow this method if you’re using the free Astra theme.

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 the plugin 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 must add the Google Analytics tag to the Astra theme customizer.

By now, you should have your GA4 snippet with you.

So, follow the steps 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.

GIF Tutorial on Astra Hooks

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, 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.

Astra custom layout activation module

After activating the custom layout, hover your mouse on appearance 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. 

Astra theme code editor

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 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

Where do I put the Google Analytics code?

The Google Analytics code should be on after the opening HTML tag and before the closing HTML tag in the wp_head section of your website. And it should fire on all the website pages you want to track.

Does Google Analytics have a WordPress Plugin?

Google Site Kit is a WordPress plugin that lets you add Google Analytics code to your WordPress blog effortlessly. 

The plugin also connects your Google search console account to your WordPress blog so that you can analyze search console data directly in WordPress. 

How do I add Google Analytics to Astra Theme?

Follow the step guide in this tutorial to add Google Analytics code to your Astra theme, whether the free or Astra Pro version; the methods are described in this post.

Similar Posts

2 Comments

  1. This is the very useful post to analyse your website traffic. An Astra theme is a well-optimized theme and takes less time to load website.

    1. Hi Anaya,

      Thanks for your time here, glad to read you like the Astra theme.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.