How to Add Google Analytics 4 (GA4) to Your Astra-Themed WordPress Site
Step 1: Create a GA4 Property
- Go to the Google Analytics website and sign in with your Google account (If you don’t have a Google account, you’ll need to create one).
- Click on Admin in the left sidebar.
- In the Property column, click Create Property.
- Enter your website details, such as the Property Name, Reporting Time Zone, and Currency.
Step 2: Get Your Tracking Code
- Set Up a Data Stream: After creating the property, select “Web” as the platform and enter your website URL.
- Copy the Tracking Code: Once the data stream is set up, you’ll receive a tracking code (gtag.js). Copy this code.
The below code is similar but you have to replace G-XXXXXXX with your Measurement ID.
!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXX');
</script>
Step 3: Add the Tracking Code to Your Astra Theme
You have a few options to add the tracking code to your Astra theme:
Option 1: Using Astra Hooks (Free Version)
- Install Astra Hooks Plugin: From your WordPress dashboard, go to Plugins > Add New, search for “Astra Hooks,” install, and activate it.
- Add the Tracking Code: Go to Appearance > Customizer > Hooks > Header > wp_head. Paste the GA4 tracking code here and publish the changes
Option 2: Using Astra Pro Custom Layouts
- Activate Custom Layouts: If you have Astra Pro, go to Appearance > Astra Options and activate the Custom Layouts module.
- Create a Custom Layout: Go to Appearance > Custom Layouts > Add New. Name your layout, paste the GA4 tracking code, and set the action to “wp_head.” Publish the layout
Option 3: Using a Plugin
- Install Insert Headers and Footers Plugin: Go to Plugins > Add New, search for “Insert Headers and Footers,” install, and activate it.
- Add the Tracking Code: Go to Settings > Insert Headers and Footers. Paste the GA4 tracking code into the “Scripts in Header” section and save
Step 4: Verify the Installation
- Check Real-Time Reports: Go back to your Google Analytics account and check the real-time reports to ensure data is being collected.
By following these steps, you should be able to successfully add GA4 to your Astra-themed WordPress site.