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 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 an GA4 Configuration tag, or edit an existing one.
  2. Click More Settings > Fields to Set.
  3. Add a field with these values:
  4. Field Name: transport_url
  5. Value: https://<DOMAIN_NAME> where DOMAIN_NAME is your tagging server domain name.
  6. Save the tag and configure the trigger to fire on all pages.
  7. 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 tagging server URL to the Transport URL field (e.g. https://<DOMAIN_NAME> where DOMAIN_NAME is your tagging server domain name).
  5. Save the tag and configure the trigger to fire on all pages.
  6. Publish the container.

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 tagging server.

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

gtag('config', '<MEASUREMENT_ID>', {
  transport_url: 'https://analytics.example.com'
});

Replace <MEASUREMENT_ID> with your Google Analytics 4 or Universal Analytics ID. Replace https://analytics.example.com with your tagging server 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-side tagging URL. When enabled, this client will activate in response to requests to the default gtag.js request paths, e.g. /gtag/js?id=<MEASUREMENT_ID>. Click Add Measurement ID to add one or more Google Analytics measurement IDs.

Load Google Analytics scripts

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

Update the gtag.js source domain

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

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

  gtag('config', 'GA_MEASUREMENT_ID');
</script>

Client configuration

To get your server-side tagging 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.

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-side tagging installation 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 API, you can specify additional parameters by including them as command parameters. See gtag.js API reference for more information.

To configure parameters you want sent on all events:

gtag('config', 'GA_MEASUREMENT_ID', {
  '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-123456789', {
  '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-side tagging installation, 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-side tagging installation 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=UA-XXXXX-Y&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 tagging server, replace the www.google-analytics.com hostname with the domain name of your tagging server. For example:

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

v=1&tid=UA-XXXXX-Y&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.