How to Add Google Analytics Code in Magento 2
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.
Free PDF · the crash course
AI Agents for Marketing Crash Course
Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.
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:
- Log in to your Google Analytics account.
- Click on the Admin gear icon in the bottom-left corner.
- In the Property column, make sure your desired GA4 property is selected.
- Click on Data Streams.
- You will see a list of data streams. Click on the one associated with your website.
- 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).
- 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:
- Log into your Magento 2 Admin Panel.
- From the left-hand sidebar, navigate to Stores > Settings > Configuration.
- In the menu on the left, expand the Sales section and click on Google API.
- You’ll see a section called Google Analytics. Expand it.
- Set the Enable dropdown to "Yes."
- 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.
- 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.
- 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
- 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>. - In your Magento 2 Admin Panel, go to Content > Design > Configuration.
- Find the row for your active theme/store view and click the Edit link in the "Action" column.
- Expand the HTML Head section.
- In the Scripts and Style Sheets textbox, paste the first GTM snippet (the one for the
<head>). - Next, expand the Footer section.
- In the Miscellaneous HTML textbox, paste the second GTM snippet (the one for the
<body>). - Click Save Configuration and clear your Magento cache as described in the previous section.
Part B: Setting Up Your GA4 Tag in GTM
- Now that GTM is on your site, you need to tell it to load Google Analytics.
- Go back to your Google Tag Manager workspace.
- Click on Tags > New.
- Give your tag a descriptive name, like "GA4 - Pageview Configuration."
- Click inside the Tag Configuration box and choose Google Analytics: GA4 Configuration as the tag type.
- In the Measurement ID field, paste your "G-" ID that you copied earlier.
- Leave the "Send a page view event when this configuration loads" box checked.
- 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.
- 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.
Free PDF · the crash course
AI Agents for Marketing Crash Course
Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.
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
Facebook Ads for Pressure Washing: The Complete 2026 Strategy Guide
Learn the proven Facebook advertising strategies for pressure washing businesses in 2026. Generate more leads with targeted campaigns, compelling creatives, and proper follow-up systems.
Facebook Ads for Caterers: The Complete 2026 Strategy Guide
Learn how to run effective Facebook ads for caterers in 2026. This complete guide covers campaign structure, creative requirements, budget allocation, and timeline for results.
Facebook Ads for Mechanics: The Complete 2026 Strategy Guide
Learn how to use Facebook ads for mechanics to fill your service bays with high-value customers. Complete targeting, offers, and creative strategy for 2026.