When you use Google Analytics on the web, you are most likely implementing one of analytics.js, the global site tag (gtag.js), or Universal Analytics tags via Google Tag Manager.

These libraries all end up doing the same thing: compiling a payload-rich HTTP request to an endpoint at https://www.google-analytics.com.

What if you want to have the JavaScript libraries do their job, but instead of sending the data to Google’s servers, you send them to a new, custom endpoint?

Perhaps you’re already using Google Tag Manager’s new Server-side tagging setup, and you want to redirect your site’s Universal Analytics data collection through your new server-side proxy?

Or maybe you’re using Snowplow Analytics, and you want to leverage its capacity to digest Google Analytics payloads as well.

A recent update to gtag.js and Google Tag Manager has made it much easier to redirect the payload. In this #GTMTips article, we’ll take a look at this new field and how it works.

Tip 116: Redirect the Google Analytics payload to a custom endpoint

Within Google Tag Manager, the Universal Analytics template as well as the Google Analytics Settings variable have a new setting under Advanced Configuration.

The Set Transport URL setting expands a text field to which you can now type a base URL string.

A valid base URL is a URL string that begins with http:// or https:// and does not end with /.

Typically, you’d just have a base hostname here, assuming your collector domain is housed on a subdomain mapped specifically for collecting the data. An example would be this:

https://collector.simoahava.com

It’s possible to have the tracker embedded in a path as well, so this would be just as fine as a base URL:

https://www.simoahava.com/collector

However you build it, the payload will be sent to the base URL + /collect. A request might thus look like this:

https://collector.simoahava.com/collect?v=1&t=pageview&tid=UA-12345-1...

Occasionally, if you have advertising features enabled, the endpoint can be base URL + /r/collect. In some cases the endpoint can also be base URL + /j/collect, so you need to configure your server or service to account for these when configuring the collector APIs.

NOTE! In a Server container, the extractEventsFromMpv1 API automatically intercepts all possible path variations of /collect, so you don’t have to manually configure anything if building your own custom server-side tagging Client.

gtag.js

The gtag.js library also supports this feature.

In gtag.js, the field is named transport_url, and it’s set in the tracker configuration:

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

All hits that utilize this tracker will now send their payloads to https://collector.simoahava.com instead of the endpoint represented by the MEASUREMENT_ID.

analytics.js

If you have an inline analytics.js implementation, there is a field named transportUrl that you could use for this purpose:

ga('create', 'UA-12345-1', {name: 'ss', transportUrl: 'https://collector.simoahava.com'});

However, this is a bit of a relic and doesn’t have official support going forward. The transportUrl field doesn’t, for example, process the custom paths (/r/, /j/) correctly, which might lead to issues when working with ad integrations in your server-side endpoint.

There’s no official word if analytics.js will ever be patched to have feature parity with transportUrl, so your best option right now is to use either gtag.js or Google Tag Manager.

Summary

With the introduction of Server-side tagging in Google Tag Manager, having a way to redirect the Google Analytics call away from the GA servers to your own custom endpoint is a necessity. That’s why the Transport URL feature was released, and it should help a great deal in building your own event stream to your server-side container.

However, Universal Analytics has been around for so long that the payload format (Measurement Protocol) it uses has been baked into countless analytics pipelines. It’s such a familiar schema that if I were to create a custom pipeline for analytics data collection, I would definitely include a processor for the Measurement Protocol format.

This setting should prove valuable to anyone working with a server-side tagging setup, where hits are proxied through a Google Tag Manager Server container rather than sent directly to Google’s servers. Or, perhaps you’ve built something that integrates the GA payload directly into BigQuery, or maybe you’re using Snowplow Analytics; regardless, this feature will make life a bit easier for you, in case you want to delegate the JavaScript tracker functionality to Google’s own libraries.