Server-side Google Analytics 4 for Magento 2

Ivanna Holubovska

Ivanna Holubovska

Author
Updated
May 11, 2026
Also available in

Google Analytics has long been a go-to tool for tracking and analyzing website data, providing valuable insights into user behavior and performance metrics. For Magento 2 users, integrating GA4 into their server-side implementation can unlock a wealth of data and enable more precise tracking and reporting.

In this article, we will explore the benefits of server-side Google Analytics 4 for Magento 2 and provide a step-by-step guide on setting up the Magento 2 extension, empowering you to harness the full potential of the e-commerce data.

Advantages of using server-side Google Analytics 4

Here we would like to outline the benefits of implementing server-side GA4 for your Magento 2 Store:

  • Enhanced Data Accuracy and Consistency. By capturing data directly from the server, it eliminates the reliance on client-side scripts, reducing the chances of data loss or discrepancies caused by ad blockers, browser settings, or user behavior. This ensures that your analytics reports provide a more reliable and comprehensive view of user interactions, allowing for more informed decision-making. 
  • Cross-Device and Cross-Platform Tracking. In today's multi-device and multi-channel world, customers interact with businesses through various touchpoints. Server-side tracking captures these interactions, allowing you to understand the entire customer journey, regardless of the device or platform used.
  • Increased Data Privacy and Compliance. Server-side GA4 implementation reduces the amount of personally identifiable information (PII) collected and transmitted to third-party platforms. This helps businesses adhere to data privacy regulations such as the General Data Protection Regulation (GDPR) and eases concerns about data security and user privacy. 
  • Flexibility and Customization. Now you have more control over the data you send to Google Analytics and can tailor the tracking to match your specific business requirements. This enables you to track custom events, attributes, and user interactions that are unique to your Magento 2 store, allowing for deeper analysis and insights that align with your business objectives. 
  • Scalability and Performance. Server-side tracking offers scalability and improved performance, especially for high-traffic websites or complex e-commerce platforms like Magento 2. By offloading the tracking to the server, you reduce the burden on the client's browser, resulting in faster page load times and a smoother user experience. This is particularly beneficial for e-commerce sites where page speed directly impacts user engagement and conversion rates.

How to set up server-side Google Analytics 4 for Magento 2

1.1 Select your GTM account → Click Admin → Click + next to the Container name.

Set up server GTM

1.2 Add Container Name → Under Target platform, choose Server. Click Create.

Set up server GTM

1.3 Choose Manually provision tagging server. Copy your container config. We will need it in the following steps.

Set up server GTM

2.1 Go to Stape and create an account or log in. You can try our service for free and explore the benefits of Stape hosting for the GTM server.

Log in to Stape

2.2 Press the button Create container in the top right corner.

2.3 Enter your container details:

  • Container name. The name does not necessarily have to be the same as the container name in your server GTM.
  • Container configuration: paste the Container Config you copied from your Google Tag Manager Server Container.
  • Server location: for best performance, choose the server location that’s closest to where most of your clients are. See the list of available server locations here. If you have traffic from different regions, you can select the Global multi-zone server location option. It will automatically route incoming requests to the nearest available zone based on the user’s IP address.

Then click Create Container.

New container

2.4 Choose a plan for your container. You can start with a Free plan. Click Continue with a Free plan.

Stape pricing

You will see the status of your container, container config, and plan name. It takes several minutes to deploy a server container. Please reload the page to update the status. If the setup was done correctly, you should see the status Running.

Running container on Stape

The critical feature of server-side tagging is setting first-party cookies, which requires a server-side setup. You need to use a custom tagging server URL or domain to enable this feature. The custom tagging server URL or custom domain should share the same domain as your website.

Due to cookie restrictions, configuring custom domains for sGTM has become more complex. ITP affects user tracking in Safari and Firefox.

To improve your tracking, you need to set up a custom domain. It will help you prolong your cookie lifetime in all browsers.

Let’s discuss ways to configure a custom domain for your sGTM tagging server URL.

There are three ways to set up a custom domain:

  • Same origin
  • Subdomain
  • Default domain

However, the “Same origin” method is technically advanced. You'll require a CDN or a load balancer to configure a same origin domain.

Types of custom domain configuration

If you can't configure the same origin domain, we have solutions for prolonging cookie lifetime in Safari: Own CDN and Cookie Keeper.

By default, when you add your custom subdomain to your Stape container, it will be verified by the CNAME record.

Below is an example of a custom subdomain setup:

Step 1. Add your custom domain

Сhoose your container on Stape, scroll down, and click Add custom domain.

How to add a custom domain in the Stape admin

Add Subdomain name - it can be any name that works for you, just be sure to avoid using phrases specific to advertising or tracking: ad, gtm, stgm, tracking, analytics, metrics, stape, gtag, etc.

Step 2. Add CDN

A Content Delivery Network (CDN) distributes your website's assets across a global network of servers to reduce latency and improve load times for users in different locations. It is useful if your audience is geographically distributed or if you want to improve cookie tracking and data accuracy.

By default, the CDN is disabled, which means JavaScript files (like gtm.js, gtag.js, and analytics.js) are served directly from the server location where your sGTM container is hosted. Effectively, it bypasses both Stape Global CDN and Own CDN options and gives you a direct, unproxied connection between your site and the sGTM container.

Important: please consider your country's privacy regulations before setting up a CDN.

To enable the CDN, click the Add CDN toggle and select one of the following options:

CDN toggle
  • Stape Global CDN - loads scripts from a server closest to your website visitor's location, which reduces latency and improves page load times for geographically distributed audiences. We will use the Stape Global CDN in our setup. This feature is free and available for all Stape sGTM containers.
  • Own CDN (incompatible with the Automatically (via Entri) connection option) - helps route sGTM custom domain and proxy sGTM traffic through the DNS provider of your website. In this case, the IP addresses of your website and the custom domain of the sGTM will match, and server-side cookies will be considered as first-party. As a result, it gives the ability to increase cookie duration.
Own CDN selected

Step 3. Select the "How to connect" method

 Next, in the How to connect section, there are two ways of connecting a custom domain to your container:

"How to connect" options
  • Automatically (via Entri)

The easiest way to connect domains. Log in with your DNS provider details via Entri, and give us one-time permission to connect your domain. That's it, you can proceed directly to Step 5 to verify your domain; no manual configuration is required.

Automatic connection via Entri
  • Manually

With this option, you have to add DNS records manually to your hosting provider to connect your subdomain to Stape.

Manual connection selected

After you add a custom subdomain to your container, you will see DNS records that should be created to verify the custom domain. The DNS record depends on the server location and the CDN option you choose. In the next step, we show how to add DNS records.

Step 4. Add DNS records (for the manual connection method)

By default, subdomain verification is performed with CNAME records, but you can verify your subdomain with A and AAAA records if you need to (usually in cases when you need to avoid adding duplicated records).

We will cover both options below with a step-by-step guide on how to create records using Cloudflare. But you can use any other provider.

Adding a custom domain with a CNAME record

You must create one CNAME record if you do not use Stape Global CDN and two records if you use Stape Global CDN.

1. Log in to your domain name service and create a new DNS record for the subdomain you want to use. To do it, click three dots and choose Configure DNS next to your domain name on the account home page.

Configure DNS

2. Click Add record.

"Add record" button

3. Go back to your Stape account and input the record you see in the account. Make sure that the Proxy status is turned off.

CNAME records in the Stape admin

We use Stape Global CDN, so we create two CNAME records:

CNAME record
Additional CNAME record (for the Stape Global CDN option)
Adding a custom subdomain with A and AAAA records
Please note that A/AAAA records are incompatible with enabled CDN.

1. If you need to verify your custom subdomain using A and AAAA records, add the custom subdomain to the container and make sure that the CDN toggle is switched off.

Click Advanced settings and tick the Use A records instead of CNAME records box. You will see DNS records that you should add inside your Stape container. The DNS records you should configure vary depending on the server location.

"Use A records instead of CNAME records" checkbox

2. Log in to your DNS account and add the records you see in your Stape account. On the account home page, next to your domain name, click three dots and choose Configure DNS.

Configure DNS

3. Click Add record.

"Add record" button

 4. Then, create the records you see in your Stape account. Make sure that the Proxy status is turned off.

Take the values for your DNS records from the Stape account:

A/AAAA records in the Stape admin

As a result, you will get two DNS records:

A record
AAAA record

Step 5. Verify your domain

Once you've entered the DNS records to your DNS provider, return to the Stape container page where you've been configuring the custom domain and click Verify.

"Verify" button

Wait for Stape to finish verifying your custom domain (when the container status bar changes from Verifying to Ready). Usually, it takes 2-3 hours to verify the custom domain. For some DNS providers, verifying records might take up to 72 hours. We will email you once the custom domain is verified or any error is detected.

If you face any problems with domain verification, please follow our guide on troubleshooting a custom domain.

Status "Ready" in the Stape admin

Step 6. Add tagging server URL and update your website script

Go to your Google Tag Manager server containerAdminContainer Settings → change Tagging server URL to your subdomain.

Server container URL in GTM

Update the script

If you use a custom domain, updating the Web GTM script on your website is highly recommended. This tweak will load gtm.js from your domain. To do so, replace the default domain googletagmanager.com with the custom domain you set up in the previous step.

Alternatively, you can automate this process with the Custom Loader power-up. Instead of manually editing the GTM script, Custom Loader generates a ready-to-use code snippet that loads GTM and GA4 scripts directly from your custom domain. This saves your time, makes your tracking scripts more resilient to ad blockers, and assures all cookies are treated as first-party.

Custom Loader power-up interface

Custom Loader modifies gtag.js and gtm.js loading paths to resist ad blockers and ITPs, potentially increasing data volume by up to 40%.

5.1 Find and copy your web GTM ID in GTM. To find GTM ID log in to your Google Tag Manager account and open a Web GTM container. In the top right corner (next to the Submit and Preview buttons) you’ll see some short text that starts with "GTM-" and then contains some letters/numbers. 

Find and copy your web GTM ID in GTM

 5.2 Open your sGTM container on Stape →  Click Power-Ups.

Find Custom Loader and click Use.

Use Custom Loader

5.3 Add the following settings:

Domain - from the list of domains connected to your container, select the necessary ones. 

Web GTM ID - add web Google Tag Manager ID. Check out how to find web GTM ID in our guide.

GA4 ad block bypass - enables bypassing adblockers' impact on the GA4 tracking code. Note that this setting will affect all container domains. Be sure to debug both the web and server GTM containers after enabling this toggle.

Same Origin Path - if you are using same origin approach for configuring custom domain, please specify the proxy path for requests.

Platform - the Custom Loader code differs for each platform. Select "Magento 2".

5.4 Once you’ve entered all the Code & Setup information, click the Generate button.

Click the Generate button

5.5 Follow the instructions on the Configuration panel to set up Custom Loader in your app.

1. Log in to your Magento Admin.

2. Go to Magento Marketplace, then choose Stape Conversion Tracking extension, click Add to cart and proceed to Checkout.

Stape Conversion Tracking extension

Please note that the extension is entirely free, and all plugin functions - except the Cookie Keeper - can be used regardless of whether you use the Stape service or not. The Cookie Keeper function will only work if you use the Stape service on any tariff and have activated it in your container settings.

3. Follow the instructions provided to install the extension on your Magento 2 site.

4. In your Magento account go to Stores and choose Configuration

Magento account configuration

6. Choose Stape and click on Stape Conversion Tracking.

Magento account configuration

7. Paste your WEB container ID, Custom domain and Custom Loader from your container. Don’t forget to click the Save config button.

Container configuration in Magento account

8. You also have an option to send data layer with the help of Magento 2 extension. Simply enable two boxes to send e-commerce and user data. 

  • View item
  • View collection
  • Add to cart
  • View cart
  • Begin checkout
  • Payment info
  • Purchase

User data:

  • Email
  • First name
  • Last name
  • Customer ID

The Stape Suffix Enabled option determines whether the _stape suffix is added to event names to prevent conflicts with your existing GTM events; when this option is turned off, events keep their original names.

Data Layer for Magento

There can be two scenarios:

6.1 If you already configured Google Analytics 4 inside your web GTM container, all you need to do is:

6.1.1 Add your server container URL to Google Tag settings. In the configuration settings, add the configuration parameter server_container_url and add your tagging server URL as a value. 

Add your server container URL to Google Tag settings

6.1.2 Create Google Analytics 4 client in server GTM container. To do so, open the clients’ section → Create New client → Select client type Google Analytics: GA4 (Web) → Add client name and click Save

Set up GA4

You can also read more on region-specific parameters in our blog.

6.1.3 In the server GTM container create a new tag with the tag type Google Analytics: GA4

Set up GA4

6.1.4 Add Measurement ID and Event Name.

Measurement ID -  follow this guide to find GA4 ID. You can add it as variable or if the event came from a GA4 web tag, you can leave this field blank to inherit the measurement ID of the event.

Event Name - the event name to send to Google. See the recommended events for more information. If this field is blank, the value of the event_name parameter will be sent.

Set up GA4

6.1.5 Click Triggering and configure a trigger with the trigger type Custom that will fire every time when client name equals GA4 (or the name of the GA4 client that you’ve specified on the step 2.b) → Click Save

Set up GA4
6.2 If you do not have GA4 configured, follow the below steps:

6.2.1  Inside the Web container, create a new tag of the tag Type Google Tag. Add your Google Tag ID.

Add trigger to GA4 tag. It usually should trigger on all page views. 

Set up GA4

You can also create a Google Tag: Configuration Settings variable that will predefine Google Tag settings if you need to use multiple Google Tags on your website and do not want to add settings for each tag manually.

These parameters can, for example, define whether you want to send a page view event every time a Google Tag triggers, set UTM parameters, set client ID, etc. There is a list of standard Google Tags configuration parameters.    

6.2.2 To set up the GA4 event tracking, go to the tags section and create a new tag with the tag type Google Analytics: GA4 Event. Add your GA4 ID and the event name; there is a list of standard event names

Set up GA4
Set up GA4

6.2.3 Go to your Google Tag Manager Server container. Click Clients and New.

Set up GA4

6.2.4 Choose Google Analytics: GA4 (web) and click Save.

Set up GA4

6.2.5 Go to Tags and click New.

Set up GA4

6.2.6 Choose Google Analytics: GA4.

Set up GA4

6.2.7 Add Measurement ID and Event Name.

Measurement ID -  follow this guide to find GA4 ID. You can add it as a variable or if the event came from a GA4 web tag, you can leave this field blank to inherit the measurement ID of the event.

Event Name - the event name to send to Google. See the recommended events for more information. If this field is blank, the value of the event_name parameter will be sent.

Set up GA4

6.2.8 Click Triggering

Set up GA4

6.2.9 Create triggers for the tag. Client name should equal GA4. Example:

Click +

Set up GA4

Choose Custom type → click Some events → choose Client Name in built-in variables →  set Equals GA4 (the name of your GA4 client) → click Save

Set up GA4

6.2.10 Open web and server GTM debuggers and test the setup. 

Open the Server container preview mode and check that you see GA4 requests. Publish updated inside server and web Google Tag Manager containers. 

Set up GA4

Add the following record inside your Google Tag in the web Google Tag Manager container. 

Configuration parameter: server_container_url

Value is your domain name in Stape admin

Tagging server URL
Tagging server URL
8.1 Use Google Tag Manager Server-Side Container Preview and GA4 Debug Mode

We suggest starting with the Tag Manager preview and debug mode to ensure that your tags are triggering when they should. GTM debug tool will show you which tags and events were added to the site and whether they fired or not on particular pages/triggers.

Tag manager server container debug mode works similarly to web debugger. Just click the preview button on the top right corner. Then, navigate to the different pages and click on the button or perform events set up in the server container.

Return to the Tag Manager debugger and check which tags and events were triggered and if all required parameters were sent to the dataLayer. If everything works correctly, we can move to the next step.

Test GA4 in Google Tag Manager

GA4 has it's debug view that will show you all events, event parameters and user data that we processed by GA4. To find GA4 debugger click ConfigureDebug view

Use Google Analytics 4 debug view
8.2 Check if GA4 sends requests from the correct tagging URL

To ensure that requests are sent from a custom tagging URL, you’ll need to dive deeper into the developer’s zone. We’ll need Chrome or Safari developer tools here (you can use other browsers as well).

On Mac, you can access the developer tool by clicking command+option+I or click the right mouse button, then Inspect.

Test Google Analytics in console

Once you’ve opened the developer tools, go to the Network tab and refresh the page. Use the filter and type collect to find GA4 requests. Click on a GA4 request and check the Request URL and parameters on the right to confirm that hits are being sent to your server-side tagging domain (the same URL configured in the server container and GA4 tag/variable).

Keep in mind that you may not see collect requests if GA4 ad block bypass is enabled. In that case, it’s recommended to look for requests directly via your server-side subdomain (for example, sst.testdns.io) instead of relying on the default collect filter.

GA4 ad block bypass
8.3 Check server-side cookies

Note: cookies will be extended only if you are using a custom subdomain inside the tagging URL. For instance, your website URL is example.com. Then, the custom domain for tagging URL should look like gtm.example.com.

Go to the application tab inside the developer tool that you’ve used in step two. Click Storage → Cookies. On the right side, you will find cookies named FPID; check the date in the column “expires”. This way, cookies should be extended up to 2 years. I am writing this post in February 2021, and my cookie will expire in February 2023. If you are not using server-side tracking and a custom tagging URL located under your main domain, Safari will decrease your cookie lifetime to 1 or 7 days. If you see that cookies were not extended, go to the GA4 client inside the server container, click more settings and check what Server Cookie Settings look like on the screenshot below. Or make sure that you are using a custom tagging URL that looks like gtm.yourdomain.com.

Test Google Analytics server-side cookies

Conclusion

Implementing server-side Google Analytics 4 for Magento 2 offers significant advantages and unlocks a new level of data-driven insights for e-commerce businesses. By shifting the tracking process to the server, businesses can achieve enhanced data accuracy, consistent tracking across devices and platforms, improved data privacy and compliance, customization options, and better scalability and performance. 

In addition to GA4, many merchants also explore the Magento Facebook Conversion API to improve attribution and advertising results, making server-side tracking even more powerful when combined with other platforms.

By following the step-by-step guide provided in this article, using Magento 2 you can seamlessly set up server-side GA4 and harness the full potential of the e-commerce data.

Need help setting up server-side tracking?

We've got you covered! Click Get help, fill-up the form, and we will send you a quote.

Get help
author

Ivanna Holubovska

Author

Ivanna is a Lead Content Manager at Stape and a certified author on Clutch and KyivPost. She writes high-quality content on server-side tracking to help businesses optimize strategies and analytics.

Comments

Try Stape for all things server-side