Note: This developer site is moving to developers.google.com/tag-platform and will be redirected by September 30, 2021

Send data to server-side Google Tag Manager

This article describes the different ways to send events to a server-side container. If you have not done so already, complete the initial server-side tagging set up before you begin the steps in this article.

This article will describe how to:

  • Send data from a website to your server-side container
  • Receive data in your server-side container
  • Load Google Analytics scripts from your first-party endpoint
  • Use a Google Analytics 4 tag to send additional data to a server container
  • Send data from sources other than websites

Website configuration

You can route data through a server-side implementation via Tag Manager or global site tags (gtag.js).

Tag Manager

You can configure supported tags to route data via your server-side configuration with the Set Transport URL setting. The transport URL specifies your first-party domain endpoint.

To set up a tag for server-side processing, add your transport URL:

Google Analytics 4

  1. In a web container, add a GA4 Configuration tag, or edit an existing one.
  2. Select Send to server container checkbox.
  3. Enter the Server Container URL into the corresponding text field.
  4. Save the tag and configure the trigger to fire on All pages trigger.
  5. Publish the container.

Universal Analytics

  1. In a web container, add a Universal Analytics tag with a Google Analytics Settings Variable, or use an existing Universal Analytics configuration.
  2. Navigate to More Settings > Advanced Configuration.
  3. Select the Set Transport URL checkbox.
  4. Add your server container URL to the Transport URL field (e.g. https://<DOMAIN_NAME> where DOMAIN_NAME is your server container domain name).
  5. Save the tag and configure the trigger to fire on all pages.
  6. Publish the container.

Global site tag (gtag.js)

If you use gtag.js on your website, update your Universal Analytics or Google Analytics 4 config command to send events to your server container.

For both GA4 and UA tags, add a transport_url and a first_party_collection parameters to your existing tag:

gtag('config', 'G-XXXXXX', {
  transport_url: 'https://analytics.example.com',
  first_party_collection: true
});

Replace G-XXXXXX with your Google Analytics 4 or Universal Analytics ID. Replace https://analytics.example.com with your server container URL.

Server-side client configuration

Once the transport URL is set, data is sent to a client in your Tag Manager server-side container. By default, GA4 and a UA clients are pre-installed on your server-side container.

In your Tag Manager server container, click Clients in the left navigation to view the list of clients. Click the name of the client to view or edit details. In most cases, the client will require no modifications. However, there are settings that may require edits for certain cases:

  • Priority: Determines the order in which clients will run. Higher numbers run first, and the first client that matches the incoming request will become the active client for that request.

  • Activation Criteria: Activation criteria define when the client will respond to requests:

    • Default Universal Analytics paths: The client will activate when the request paths include /collect, /r/collect, and other similar paths used by JavaScript files that send data to Google Analytics. This setting is active by default.

    • Default gtag.js paths for specific IDs: Use this setting to enable gtag.js JavaScript serving through your server container URL. When enabled, this client will activate in response to requests to the default gtag.js request paths, e.g. /gtag/js?id=G-XXXXXX. Click Add Measurement ID to add one or more Google Analytics measurement IDs.

Load Google scripts

To unlock the full benefits for server-side tagging, you can optionally load supported scripts via your server container. This reduces your reliance on a third-party party domain.

Update the gtag.js source domain

When you install gtag.js, the default configuration loads the Google Analytics libraries from www.googletagmanager.com. To load the Analytics code via your server container, change this domain name to the domain of your server container:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://<DOMAIN NAME>/gtag/js?id=<G-XXXXXX>"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '<G-XXXXXX>');
</script>

Client configuration

To get your server container to respond to requests to load Google Analytics scripts, you need a few minor modifications to your clients:

  1. In your server-side container, click Clients to view the list of clients.
  2. Click the name of the client you would like to edit.
  3. Select Default gtag.js paths for specific IDs.
  4. Click Add Measurement ID and enter your ID.
  5. Save the client configuration and publish the container.

Update the gtm.js source domain

When you install gtm.js on your website, the default configuration loads the Google Tag Manager libraries from www.googletagmanager.com. To load the GTM code via your server container, change this domain name to the domain of your server container for both the gtm.js script and ns.html file:

Screenshot of the Google Tag Manager snippet for gtm.js and ns.html insertion

Client configuration

To get your server container to respond to requests to load Google Tag Manager scripts, you need to add the Google Tag Manager: Web Container client and make changes to the configuration:

  1. In your server-side container, click Clients to view the list of clients.
  2. Click New in the Clients section.
  3. Click on the Client Configuration section to choose a client type.
  4. Select Google Tag Manager: Web Container from the list of available clients.
  5. Click Add Container ID and enter your GTM web container ID. This ID must match the ID that you used in the HTML snippet shown above.
  6. Save the client configuration and publish the container.

Include additional data for other tags

You might have multiple tags installed on your page that send similar data to the server. Rather than using separate tags, we recommend that you route that data to your server container using the Google Analytics 4 tags via Tag Manager or gtag.js. You can specify additional event parameters in the tag, then use variables in your server-side container to extract the parameters. Doing so allows you to reduce the amount of JavaScript on your page and minimize the number of HTTP requests, both of which will increase your website's performance.

Tag Manager

If there are parameters that you want to send across all events, such as a unique identifier, add it to the Fields to Set in the Google Analytics 4 Configuration tag:

  1. In a web container, add a Google Analytics 4 Configuration tag or edit an existing one.
  2. Click Fields to Set > Add Row.
  3. Add the parameter you want present in every event to the parameter name/value table.
  4. Configure a trigger to fire on All Pages and then save the tag.

If there are parameters that you want to send on some events, add the parameters in the Event Parameters section of the Google Analytics 4 Event tag.

  1. In a web container, add a Google Analytics 4 Event tag or edit an existing one.
  2. Click Event Parameters > Add Row.
  3. Add the parameter to the parameter name/value table.
  4. Configure a trigger and save the tag.

gtag.js

Using the gtag.js API, you can specify additional parameters by including them as command parameters. Read the gtag.js API reference for more information.

To configure parameters you want sent on all events:

gtag('config', '<G-XXXXXX>', {
  'transport_url': 'https://analytics.example.com'
  'custom_parameter': 'value',
});

To configure parameters you want sent on certain events:

gtag('event', 'event_name', {
  'custom_parameter': 'value',
});

To configure parameters to be sent on only page_view events, add the send_page_view parameter with a value of false in the config command and add a separate page_view event.

gtag('config', 'G-XXXXXX', {
  'transport_url': 'https://analytics.example.com'
  'send_page_view': false,
});
gtag('event', 'page_view', {
  'custom_parameter': 'value',
});

Receiving additional data in server-side Google Tag Manager

In your server container, the Google Analytics 4 client will parse the additional parameters and set them in the event data. To use this data in other tags, we recommend creating an Event Data variable and specifying the parameter name in the Key Path. You can now use this variable in any tag that needs the parameter.

Send data from other sources

The Google Analytics Measurement Protocol can be used to enable server-side tagging support from sources such as mobile apps and server-to-server applications.

Mobile apps

To get your Android or iOS app to send data to your server container, you will create a custom image tag and configure it to use the Measurement Protocol:

  1. In your mobile container (Android or iOS), click Tags > New.
  2. In Tag Configuration select the Custom Image tag type.
  3. Set the value of Image URL to a pixel image on the server container at the path in the Measurement Protocol client that you specified earlier. Append any desired Measurement Protocol parameters to the end of your pixel URL:
    https://custom.example.com/app?v=1&tid=G-XXXXXX&cid=555&t=screenview&an=myApp&version=1.6.2&aid=com.foo.myapp&cd=home
  4. Deselect Enable Cache Busting.
  5. Save the tag and publish the container.
  6. In your server-side container, click Clients > New.
  7. In Client Configuration, select the Measurement Protocol client type.
  8. Set Activation Path to the path of your app's collection endpoint.
  9. Save the client configuration and publish the server-side container.

Server-to-server apps

To send measurement protocol data to your server container, replace the www.google-analytics.com hostname with the domain name of your server container. For example:

POST /batch HTTP/1.1
Host: collection.example.com

v=1&tid=G-XXXXXX&cid=555&t=screenview&an=myApp&version=1.6.2&aid=com.foo.myapp&cd=home

To receive Measurement Protocol hits, configure your server-side Google Tag Manager installation with a Measurement Protocol client:

  1. In your server-side container, click Clients > New.
  2. In Client Configuration, select the Measurement Protocol client type.
  3. Set Activation Path to the path of your server's endpoint.
  4. Save the client configuration and publish the container.