How to Add Google Analytics Code in Magento 2

Cody Schneider9 min read

Connecting your Magento 2 store to Google Analytics is one of the most important first steps for understanding how your customers find and interact with your site. This article walks you through exactly how to add the Google Analytics tracking code to Magento 2, covering a few different methods so you can choose the one that works best for you.

Why Connect Google Analytics to Your Magento 2 Store?

Before you get started, it’s helpful to understand what you'll gain by linking these two platforms. Simply put, Google Analytics is the key to unlocking critical data about your store's performance. It’s not just about counting visitors, it's about understanding them.

When you have Google Analytics set up correctly, you can answer essential business questions like:

  • Where do my customers come from? Discover which marketing channels (like Google Ads, Facebook, email newsletters, or organic search) are actually driving traffic and, more importantly, sales.
  • What do visitors do on my site? See which product pages are most popular, what paths customers take before purchasing, and where they might be getting stuck or leaving your site.
  • How is my store performing? Track key e-commerce metrics like conversion rate, average order value, total revenue, and product performance. You can see which banners drive clicks and which promotions actually convert.
  • Who are my customers? Get valuable demographic data about your audience, such as their age, location, and interests, helping you better tailor your marketing efforts.

Without this data, you're essentially marketing in the dark. Connecting Google Analytics provides the visibility you need to make informed decisions that grow your revenue.

Finding Your Google Analytics 4 Measurement ID

No matter which method you choose for installation, you’ll first need to find your unique GA4 Measurement ID. This ID tells Magento which Google Analytics property to send the data to. Universal Analytics has been phased out, so we'll be focusing exclusively on GA4.

If you don't have a GA4 property yet, head over to the Google Analytics website and create one for your store. If you already have one, here’s how to find your Measurement ID:

  1. Log in to your Google Analytics account.
  2. Click on the Admin gear icon in the bottom-left corner.
  3. In the Property column, make sure your desired GA4 property is selected.
  4. Click on Data Streams.
  5. You will see a list of data streams. Click on the one associated with your website.
  6. On the right side of the screen, you’ll see your Measurement ID. It starts with a "G-" followed by a string of characters (e.g., G-XXXXXXXXXX).
  7. Copy this ID. You'll need it in a moment.

Now that you have your Measurement ID, you're ready to add it to your Magento 2 store.

Method 1: Using Magento 2's Built-in Functionality

The simplest way to connect Google Analytics is by using Magento's native integration. This method is quick, easy, and perfect for store owners who just need basic tracking without a lot of complexity.

Follow these steps to set it up:

  1. Log into your Magento 2 Admin Panel.
  2. From the left-hand sidebar, navigate to Stores > Settings > Configuration.
  3. In the menu on the left, expand the Sales section and click on Google API.
  4. You’ll see a section called Google Analytics. Expand it.
  5. Set the Enable dropdown to "Yes."
  6. A field for Account Number will appear. Paste your "G-" Measurement ID here. While the field says "Account Number," this is the correct place for your GA4 Measurement ID.
  7. Note: If you're on an older Magento 2 version, this built-in functionality might not fully support all GA4 features like enhanced e-commerce events. For richer data, Method 2 is recommended.
  8. Once you've added your ID, click the Save Config button in the top-right corner.

Clearing the Magento Cache

After saving your configuration, a system message will appear reminding you to refresh your cache. This is a critical step to ensure your changes go live on your storefront.

  • Go to System > Tools > Cache Management.
  • Check the box next to Configuration and any other cache types that are marked as "Invalidated."
  • From the Actions dropdown at the top-left, select "Refresh" and click Submit.

How to Verify the Installation

Just because you saved the configuration doesn't mean it's working. It's always best to verify that your analytics code is firing correctly.

  • Using GA4 Realtime Reports: Open your website in a new browser tab or on your phone. Then, in your Google Analytics account, go to Reports > Realtime. Within a minute or two, you should see at least one user (that's you!) appear in the report. If you do, congratulations, it's working!
  • Using Browser Tools: You can also check your site's source code. Right-click on any page of your store and select "View Page Source." Use the search function (Ctrl + F or Cmd + F) to look for your Measurement ID (e.g., G-XXXXXXXXXX). If you find it within a script tag, you're all set.

Method 2: Using Google Tag Manager (GTM)

While the built-in method is simple, using Google Tag Manager is the industry-standard and most-recommended approach. GTM acts as a middleman for all your tracking scripts (not just Google Analytics). Instead of adding code snippets directly to Magento for every marketing tool, you just add GTM once. Then, you can manage everything else from the user-friendly GTM interface.

This method has two main parts: installing GTM on your Magento 2 site and then setting up the GA4 tag inside GTM.

Part A: Installing the GTM Container in Magento 2

  1. First, go to your Google Tag Manager account and find your container code. It's often found under the Admin tab or by clicking your GTM ID near the top of the interface. You’ll be given two snippets of code: one for the <head>, and one for the <body>.
  2. In your Magento 2 Admin Panel, go to Content > Design > Configuration.
  3. Find the row for your active theme/store view and click the Edit link in the "Action" column.
  4. Expand the HTML Head section.
  5. In the Scripts and Style Sheets textbox, paste the first GTM snippet (the one for the <head>).
  6. Next, expand the Footer section.
  7. In the Miscellaneous HTML textbox, paste the second GTM snippet (the one for the <body>).
  8. Click Save Configuration and clear your Magento cache as described in the previous section.

Part B: Setting Up Your GA4 Tag in GTM

  1. Now that GTM is on your site, you need to tell it to load Google Analytics.
  2. Go back to your Google Tag Manager workspace.
  3. Click on Tags > New.
  4. Give your tag a descriptive name, like "GA4 - Pageview Configuration."
  5. Click inside the Tag Configuration box and choose Google Analytics: GA4 Configuration as the tag type.
  6. In the Measurement ID field, paste your "G-" ID that you copied earlier.
  7. Leave the "Send a page view event when this configuration loads" box checked.
  8. Next, click the Triggering box below and select the All Pages trigger. This tells GTM to fire your GA4 tag on every single page of your website.
  9. Click Save. Finally, click the blue Submit button in the top-right of your GTM dashboard. Give your version a name (e.g., "Added GA4 Config Tag") and click Publish.

You can verify this installation using the same GA4 Realtime method. The big advantage now is that whenever you need to add another marketing tag, like a Facebook Pixel or a HubSpot tracking code, you can do it directly within GTM without ever touching your Magento settings again.

Method 3: Using a Third-Party Extension

If you find the above methods too technical or you want more advanced features out-of-the-box, a third-party extension can be a great solution. The Adobe Commerce Marketplace is filled with powerful extensions designed specifically to bridge the gap between Magento and Google Analytics.

Why consider an extension?

  • Enhanced E-commerce: Many extensions automatically configure Enhanced E-commerce tracking, which sends rich data to GA like product impressions, add-to-cart actions, and checkout step funnels. Setting this up manually via GTM can be complex.
  • Ease of Use: Most extensions provide a simple setup wizard. You just install it, input your Measurement ID, and the extension handles the rest of the code placement.
  • Extra Features: Some extensions offer features like cookieless tracking, consent management for GDPR/CCPA compliance, and integration with other Google services.

The main downsides are cost (many of the best extensions are paid) and the risk of conflicts with other extensions you have installed. Be sure to choose a well-reviewed extension from a reputable developer.

What to Do After You're Connected

Your job isn't done once the tracking code is live. Getting the data flowing is just step one. Now, you need to make sure you're tracking what matters.

  • Set Up E-commerce Parameters: To get the most out of GA4 for your store, you absolutely need to configure e-commerce events. This lets you see revenue, what products are selling, and funnel data. If you used Method 2 (GTM), you can find many guides online for setting up Magento 2 data layers and GA4 e-commerce tags. If you used an extension, this may already be done for you.
  • Define Your Conversions: Beyond a sale, what other actions are important? This could be a newsletter signup, creating a customer account, or submitting a contact form. In GA4, you can mark specific events as "Conversions" to easily track them in your reports.
  • Let Data Accumulate: Don't expect meaningful insights on day one. Give Google Analytics at least a week to collect data before you start trying to identify trends. The longer you let it run, the more powerful your data will become.

Final Thoughts

Adding the Google Analytics tracking code to your Magento 2 store is a fundamental step for any serious e-commerce business. Whether you use the simple built-in method, the powerful Google Tag Manager, or a feature-rich extension, getting this data pipeline set up is your starting point for making smarter, data-driven decisions.

Once your analytics data is flowing in, making sense of it becomes the next challenge. At Graphed, we make this part simple. Simply connect your Google Analytics account, and you can instantly build reports and dashboards with natural language - just ask a question like "show me my revenue by traffic source for the last 30 days" and get your answer. We help you move from complex data to clear insights without the steep learning curve of traditional BI tools.

Related Articles

How to Connect Facebook to Google Data Studio: The Complete Guide for 2026

Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.

Appsflyer vs Mixpanel​: Complete 2026 Comparison Guide

The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.