How to Add Google Analytics Code to Magento 2?
Google Analytics offers metrics to track incoming traffic and supports offline and mobile app interactions.
You get enhanced commerce to track your website’s activities. It includes purchase activities, successful orders, and wishlist buttons.
Customer metrics let you optimize purchase funnels to increase conversion rates.
We cover the steps to add Google Analytics code to Magento 2.
- Steps to Add Google Analytics Code In Magento 2 Stores
- Google Analytics for Magento 2 Website
- Ecommerce Shopping Behavior Report
Steps to Add Google Analytics Code In Magento 2 Stores
1. Create a Google Analytics Account
- Go to Google Analytics and create an account.
- Click on Create Account and fill in your Gmail account address and password.
Read and agree to the Privacy and Terms by clicking on I Agree.
You will be redirected to Google Analytics after the account is set up.
- Click on Sign Up to start using the tool. Fill in the details about your website, such as:
- Account and website name
- Website URL
- Industry category
- Reporting timezone
- Agree to the Google Analytics Terms of Service Agreement.
2. Get the Tracking Code
You have to add a Google Analytics code to Magento 2 to collect traffic statistics.
Navigate to the left panel of the Analytics dashboard and click on the Admin Tab.
Click on Tracking Info and open the Tracking Code panel.
Copy the Tracking ID and the Tracking Code from the section.
3. Add Google Analytics Code to Magento 2
- Log in to the Magento 2 Admin Panel to add the Google Analytics code.
- Select Stores from the left-side menu and go to the store Configuration tab.
- In the Sales panel, click on Google API.
- Add your Google Analytics Account Number and Tracking ID.
- If you wish to render the anonymous part of your visitors’ IP addresses, set Anonymize IP to Yes.
- Enable Content Experiments to Yes. It is used for A/B testing and other performance tests.
Note: It might take up to 24 hours before Google Analytics can track your website data.
4. Enable Ecommerce Tracking for Google Analytics
From your Google Analytics Dashboard, go to the Admin panel.
Under All website data, click on eCommerce settings.
- Enable the eCommerce button and enhanced eCommerce reporting.
- Click on Save.
5. Check if the Code is Working Properly
- Navigate to the store’s frontend in one tab.
- Open the Google Analytics account in another tab.
- Choose a Real-Time report, and click on Overview.
There should be one active user on site right now.
Reasons why your tracking code won’t be working
1. Cross-check the analytics code
- Copying and pasting the code from a doc file may break the code.
- Ensure that you copy the code directly from the Google Analytics account.
- There should be no extra spaces between the letters.
2. Check for Google Analytics Filters
- From the Google Analytics account, go to Admin > Filters.
- Review all filters added to the account.
- You may have excluded the IP address tracking.
- Multiple Include filters may also affect your tracking data.
3. Google Penalties
- Google penalties can affect the website’s ranking.
- It may happen if you aren’t using an SSL certificate or have questionable content.
- This can cause a traffic drop, but it should not be mistaken for Google Analytics not tracking.
Google Analytics for Magento 2 Website
Google Analytics gives insights into how many people visit your store.
You can track specific website events, demographics, and session times.
You can also check whether the traffic came from social media, ad campaigns, or SERPs.
Other tools such as Google Tag Manager are used to update and manage website tags.
Some components for Magento 2 include:
-
Website traffic
- Get real-time statistics on visitors.
- Trackable events include Add to Cart, Place Order, and Checkout.
-
Visitor segmentation
- Segment visitors for a period.
- Check which pages, links, and sources they access.
-
Demographics
- Evaluate users’ demographics like age, gender, behavior, and location.
- See devices used: phones, PCs, tablets, etc.
- Use data to create better Google Ads campaigns.
-
Data visualizations
- Visualize tracking data in dashboards with charts and tables.
-
Goal-related tracking
- Goals represent key objectives (like a purchase).
- Configure goals to measure conversions.
Ecommerce Shopping Behavior Report
Add funnels to your Magento store to get specialized ecommerce data.
Turn on Enhanced Ecommerce:
Navigate to Conversions > Ecommerce > Shopping Behavior
This feature counts user sessions for each step in the funnel:
All sessions → Product Views → Add to cart → Check-Out → Transactions
You can analyze:
- Product views vs engagement.
- Drop-offs in the funnel.
- Checkout completion rates.
Other useful funnels include:
- Checkout Behavior – Track drop-offs in checkout.
- Custom Reports – Compare conversion paths with advanced segments.
- Goal Funnels – Add goals like product clicks or checkout completions.
Google Analytics helps Magento store owners optimize funnels, improve ads, and increase conversions.
👉 Check out more tutorials on Magento at MGT-Commerce Tutorials