Google Analytics code not showing in Magento 2 page code

Google Analytics code not showing in Magento 2 page code

October 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Google Analytics code not showing in Magento 2 page code

Solving Google Analytics Tracking Code Missing Issue in Magento 2

Tracking your website traffic is crucial for understanding your audience and optimizing your e-commerce strategy. In this guide, we’ll troubleshoot the common issue of missing Google Analytics tracking code in Magento 2 stores.

Checking Your Setup: Basic Configuration

First, let’s ensure you’ve properly configured Google Analytics in the Magento admin panel:

  1. Navigate to Stores > Configuration.
  2. Click on Sales and then Google API.
  3. Enter your Google Analytics Account Number (GA Tracking ID) in the field provided.
  4. Save your changes.

Understanding the Magento 2 GA Integration

Magento 2 integrates Google Analytics using a unique approach. The tracking code is not hardcoded into every page but dynamically added based on user acceptance of cookies, as per EU guidelines.

By default, Restrict cookie for compliance reasons is enabled in Magento 2’s configuration. This mode disables scripts like GA until the user accepts the cookie warning message.

Step-by-Step: Troubleshooting Missing GA Code

Step 1: Verify Configuration

Ensure your GA tracking ID is entered correctly and saved without errors.

Navigate to Stores > Configuration > General > Web. Under Cookie Restriction, verify the mode is set to Allow cookies from third parties or that GA is specifically allowed in the Allowed Cookies field.

Step 3: Test Page Loading Sequence

Load your website and observe the following:

  1. You should see a cookie consent pop-up at the bottom of your page.
  2. By default, Magento disables all non-essential cookies (including GA) until user acceptance.
  3. After accepting the cookies, the page reloads.
  4. On reload, you should see the Google Analytics tracking code inserted after the opening <body> tag.

Debugging Missing GA Code

If the GA code is still missing after reloading:

1. Enable Magento Developer Mode

Switch to developer mode by adding app/design/frontend/Magento/luma/en_US/css/source/_theme.less file and adding @media screen { body { background: #000; } }. Reload the page.

2. Check Layout XML Files

Verify your active theme’s layout files, especially Magento_Theme/page/html/head.phtml and Magento_Theme/layout/default.xml.

Ensure the following code is not commented out or missing:

<referenceContainer name="before.body.end">
   <block class="Magento\GoogleAnalytics\Block\Html" name="google_analytics" template="GoogleAnalytics::js.phtml"/>
</referenceContainer>

3. Check GA Module Status

Verify that the Magento_GoogleAnalytics module is enabled in your setup.

Conclusion: Resolving Persistent Issues

If you’ve checked all these steps and the Google Analytics tracking code is still missing, consider the following:

  • Theme Customization: Your custom theme might have overwritten or removed essential layout XML elements.
  • Extension Conflicts: Check if any third-party extensions are conflicting with the GA integration.
  • Server Errors: Check server logs for any PHP errors that may prevent the GA code from rendering.

Remember, Magento 2’s built-in GA integration is designed to comply with cookie restrictions while still providing essential tracking data. If you need custom GA configurations or advanced analytics features, consider using a dedicated extension like Amasty Google Analytics Pro.

With these steps and careful troubleshooting, you should now have your Google Analytics tracking code properly integrated into your Magento 2 store.

Magento 2 Performance API Admin

Our network