Skip to content

Send GA4 from web GTM through your server container

Your server container is running, but nothing sends data to it yet. In this lesson you create a GA4 property, add a Google tag and GA4 Event tags to your web container, and tell them to send every hit to your own subdomain instead of Google. The browser then talks only to sgtm.example.com, in a first-party context, and the server container decides where the data goes next.

Intermediate 12 min read

What you will learn

  • Create a GA4 property and web data stream and find the Measurement ID.
  • Add a Google tag with server_container_url in the web container.
  • Send custom and ecommerce events with GA4 Event tags.
  • Confirm in DevTools that hits go to your subdomain.

Before you start

  • A published web container on your site (lesson 1).
  • A server container that answers on your subdomain, for example sgtm.example.com (lesson 2).
  • Editor access to a Google Analytics account.

01 Create the GA4 property and web stream

Skip this section if you already have a GA4 property. You only need its Measurement ID.

  1. 1 Open analytics.google.com, go to Admin and click Create, then Property.
  2. 2 Enter a property name, your reporting time zone and currency, then finish the business details.
  3. 3 When asked to start collecting data, choose Web, enter your website URL and a stream name, and click Create stream.
  4. 4 Copy the Measurement ID from the stream details. It looks like G-XXXXXXXXXX.

02 Add the Google tag with your server URL

The Google tag loads GA4 on the page and sends the page_view. One setting, server_container_url, changes where its hits go.

  1. 1 In your web container go to Tags, click New and choose Google tag as the tag type.
  2. 2 Paste your Measurement ID into Tag ID.
  3. 3 Under Configuration settings, add a row with the parameter and value below.
  4. 4 Set the trigger to Initialization - All Pages (or All Pages), name the tag and save.
text
Configuration parameter:  server_container_url
Value:                    https://sgtm.example.com

Use exactly the URL of your server container: https://, no path and no trailing slash. Initialization - All Pages fires before other tags, so events that fire early already know where to send their data.

03 Send events with GA4 Event tags

Page views come from the Google tag. Everything else, such as sign-ups and purchases, needs a Google Analytics: GA4 Event tag. Reuse the sign_up trigger from lesson 1:

  1. 1 Create a tag of type Google Analytics: GA4 Event.
  2. 2 Set Measurement ID to G-XXXXXXXXXX (your ID). The event goes through the Google tag with the same ID, including its server URL.
  3. 3 Set Event Name to sign_up. Under Event Parameters add method with the Data Layer Variable you created earlier.
  4. 4 Select the Custom Event trigger sign_up and save.

For ecommerce, your site pushes the purchase details in the ecommerce object that GA4 expects. Clear the previous ecommerce object first, as in this example.

js
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({
  event: 'purchase',
  ecommerce: {
    transaction_id: 'T-10293',
    value: 129.90,
    currency: 'EUR',
    items: [{ item_id: 'SKU-1', item_name: 'Shoes', price: 129.90, quantity: 1 }]
  }
});
  1. 1 Create a Custom Event trigger for purchase.
  2. 2 Create a Google Analytics: GA4 Event tag with Event Name purchase.
  3. 3 Open More Settings, then Ecommerce, tick Send Ecommerce data and choose Data Layer as the Data source.
  4. 4 Attach the purchase trigger and save.

04 What changes in the browser

Without a server URL, GA4 sends hits to google-analytics.com. With it, the same hits go to your subdomain on the /g/collect path:

text
https://sgtm.example.com/g/collect?v=2&tid=G-XXXXXXXXXX&en=page_view&...
  • The request stays on your own domain, so it is first-party for the browser.
  • Fewer blockers match it, because it no longer goes to a known analytics domain.
  • The server container sees every hit before any vendor does, which is where you can filter or enrich data later.

05 Verify and publish

  1. 1 Click Preview in the web container and connect to your site. The Google tag should fire on Initialization and the GA4 Event tags on their events.
  2. 2 In the site window open DevTools, go to the Network tab and filter by collect.
  3. 3 Reload the page. You should see requests to https://sgtm.example.com/g/collect with status 200 or 204, and none to google-analytics.com/g/collect.
  4. 4 When everything looks right, click Submit and publish the web container.
  • No hard-coded gtag.js snippet for the same ID is left on the site.
  • The Google tag has server_container_url set to your subdomain.
  • /g/collect requests go to sgtm.example.com.
  • The server container Preview shows incoming requests (the debugging lesson explains how to read it).

Frequently asked questions

Can I set this without GTM, directly in gtag.js?

Yes. Add server_container_url to the config command, for example gtag('config', 'G-XXXXXXXXXX', { server_container_url: 'https://sgtm.example.com' });. The rest of the course assumes GTM.

Do I also need a Google tag for Google Ads?

Not for this lesson. GA4 hits are enough to feed the server container, and server-side tags for other vendors can reuse the same incoming data.

The requests still go to google-analytics.com. What is wrong?

Usually the change is not published yet, another Google tag or a hard-coded gtag.js snippet for the same ID loads without the setting, or the parameter name has a typo. It must be exactly server_container_url.

Ready to put it into practice?

Deploy a server GTM container on your own domain in a few minutes. The Free plan needs no card.

Create a free account