Implementing Google Tag Manager Server-Side Using Cloud Run

Tim Hutton
6 min readJan 25, 2023

Server-side tagging has been a hot topic for a while now in the analytics implementation space for several reasons. With browser privacy restrictions, more platforms pushing their APIs and the sheer number of marketing tags on a website growing all the time, the need to improve tracking has pushed many brands to consider this alternative.

There are a number of very good server-side products including Google Tag Manager (GTM) Server-Side, Tealium EventStream and the recently updated Commanders Act. The upside of the latter two in comparison to Google Tag Manager is that they handle the server setup on your behalf, unlike GTM. There is an automatic provisioning option available in GTM, but it’s not recommended for large traffic websites. In the below article, I will take you through setting up Google Tag Manager server-side using their Cloud Run product.

The first step, similar to when setting up GTM client-side, is to create a tagging container. From the GTM start page or in Admin, select Create Container. Give the container a name and then select Server from the Target Platform, followed by Create.

Select Manually provision tagging server from the next step, and make a note of the Container Config as this will be needed in the server setup.

The following article from Google is a useful resource for setting up Cloud Run, along with containing the prerequisites such as needing a Google Cloud Platform Billing Account as well as Project Creator (if you are not using an existing project) and Billing Account User Roles.

If you need to create a new project, head to Cloud Platform Home and click New Project. Give the project a suitable name such as GTM Server Side or similar and click Create.

Now, whether you have created a new project or are utilising an existing one, ensure that you are inside the relevant project — you may need to click the Projects drop-down at the top of the screen — and head to Cloud Run, either from this link or the left-hand navigation menu.

Note: If you are working with multiple websites or across mutliple regions, you will likely need to create multiple servers. See the following article for more information on how to set up Cloud Run multi-region. For the below example, we will simply use one region.

Click Create Service and we will start by creating the Preview Server first. Select Deploy one revision from an existing container image, and paste the following URL in: gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable

You will then provide the server with a Service name, typically something descriptive. Google’s documentation suggests naming it server-side-tagging-preview, but if you are working across multiple brands, perhaps include a brand abbreviation. Also select a region here. For the purposes of testing, I have picked London. You can then follow the steps under Provision a new preview and tagging server in the following article for the most part, but I will also cover them below.

Under CPU allocation and pricing, select CPU is always allocated and for Autoscaling set the minmum number of instances to 0, and maximum to 1. For a preview server, setting this to more than 1 will break the preview feature.

For Ingress select Allow all traffic and then for Authentication select Allow unauthenticated invocations. Expand the Container, Networking, Security header and under Capacity set Memory to 512 MiB, CPU to 1 and Request timeout to 60. These are the advised values by Google, but you are welcome to change these if you have particular knowledge/confidence of doing so.

Under Environment variables, add RUN_AS_PREVIEW_SERVER with the value of true and CONTAINER_CONFIG as the config value we saw earlier when choosing manual setup on the container creation (see image 2 of this article). Click Create — and you now have a preview server!

Wait for this to be created and then make a note of the URL that is shown, as this will be required when you create the tagging server, which we will do next. Next to Service Details, click the back arrow to be taken back to the Cloud Run console and again click Create Service. The steps are very similar to the above.

Again, select Deploy one revision from an existing container image and then gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable. Set the service name to something memorable and select the relevant region.

One thing to note when creating the name is that if you do not map your own custom domains, the Service name will be included in the domain that the server runs from on your website. If one of your aims is to use server-side to attempt to bypass ad blockers, you are probably better off avoiding using words like “google”, “ga” or “gtm” in this name.

In this example I have called it timhuttonco-shop. Select CPU is always allocated as before. For autoscaling, Google advises a minimum of 2 instances and maximum of 10. Again, this depends on the amount of traffic you receive — you may need even more than 10 for high traffic sites. For the sake of testing and this article, I have a maximum of 2.

As before, for Ingress select Allow all traffic, and for Authentication select Allow unauthenticated invocations. Again, expand Container, Connections, Security. Under Capacity, set Memory to 512 MiB, CPU to 1 and Request timeout to 60. Now in Environment variables, add PREVIEW_SERVER_URL and provide this with the URL of the preview server that you created earlier, and add CONTAINER_CONFIG as the config value we saw earlier when choosing manual setup on the container creation (see image 2 of this article). Once done, click Create.

Your tagging server is ready. As before, make a note of the URL of this server as it will be needed in the GTM configuration. Also — if you ever want to make changes to a server, for example to amend the number of instances, click on Edit & Deploy New Revision.

Now return to GTM and go to Admin > Container Settings. Under Server Container URLs, click Add URL and add the URL of your Tagging Server (the second server that we set up, not your Preview Server) and click Save.

The final step, and to make sure everything has been setup as expected, click Preview from inside your GTM Server-Side container to open Preview Mode. If Preview Mode opens as expected, such as in the image below, you have successfully set everything up correctly and are ready to go!

In future articles I will dive deeper into sending data to and from your server-side container.

--

--

Tim Hutton

All things Analytics and Implementation. Currently at YOOX Net-A-Porter and previously at Decibel and Tealium. Also a lover of books. Here's to something good.