Buhlian

Set up server-side GTM with Cloud Run

May 5, 2023 7 min read Web Cloud

With server-side GTM you can take your web analytics and data activations to new levels

image

Why server-side GTM?

Google Tag Manager (GTM) is a popular tool that enables marketers to track user behavior on their websites. By using GTM, they can easily add tracking codes and manage their tags, which saves a lot of time and makes their work much more efficient. However, traditional GTM is client-side, meaning that all the tracking scripts are executed on the user's browser. This can lead to performance issues and data discrepancies, as different users might have different ad-blockers or privacy settings that affect the way the tags are executed.

Server-side GTM offers a solution to these problems. By deploying GTM on the server-side, all the tracking scripts are executed on the server before the page is sent to the user's browser. This not only improves performance, but it also ensures that the tags are executed consistently across all users. In addition, server-side GTM offers other benefits, such as increased data privacy, better data accuracy, and improved ad targeting. In addition, the server-side GTM endpoint allows for more advanced data enrichment and activation as it integrates well with the Google Cloud infrastructure. The use cases are endless - I will explore and write on these in upcoming blog posts.

In this blog post, we will discuss how to deploy server-side GTM on a website using Google Cloud Run, a serverless platform that enables developers to deploy and run their applications in a containerized environment. The Google Cloud Run service is chosen here to minimize costs for smaller websites (or for experimentation purposes). 

 

How to set up and deploy server-side GTM

Before we begin, there are a few prerequisites that need to be fulfilled:

  1. A Google Cloud Platform (GCP) account with billing enabled.
  2. A domain name that you own and can modify its DNS settings.

With these prerequisites fulfilled, let's move on to the steps required to deploy server-side GTM on a website using Google Cloud Run.

 

Step 1: Create a new GTM Container

The first step is to create a new GTM container. To do this, go to the Google Tag Manager website and sign in with your Google account. Then, click on the "New Account" button and follow the prompts to create a new container. Make sure to select the "Server" option when asked to choose the type of container.

 

Step 2: Set up the Google Cloud Infrastructure

Next, we need to set up the necessary infrastructure on Google Cloud. This involves creating a new project, enabling the Cloud Run API, and setting up a new Cloud Run service.

  1. Create a new project (if you do not already have an existing project, you want to use for this deployment): Go to the Google Cloud Console and create a new project. Give it a meaningful name and select a billing account to associate with it.

  2. Enable the Cloud Run API: In the Google Cloud Console, navigate to APIs & Services > Library and search for "Cloud Run". Click on the Cloud Run API and enable it for your project.

  3. Set up a new Cloud Run service: In the Google Cloud Console, navigate to Cloud Run > Create Service.

    1. In the Google Cloud Console, navigate to Cloud Run > Create Service.

    2. Choose "Deploy one revision from an existing container image" and enter the container image URL "gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable" in the "Container image URL" field.

    3. In the "Service name" field, enter a name for your new Cloud Run service - this will be for the production server so you can fx name it "gtm-ss".

    4. In the "Region" field, select the region that is closest to your website's users. Keep in mind that this will affect the latency of the requests and the cost of running the service.

    5. In the "Variables" setting add a variable with the name "CONTAINER_CONFIG". The value of this variable must be the container config key string from the server-side GTM container you created in step 1 (you find this under the container settings by choosing the "Manually provision tagging service" option). 

    6. Set the "Maximum instances" to a low number (fx 3) to keep costs down. (if your website has a lot of traffic, you should set this accordingly)

    7. Under "Advanced settings" in the "Container" section, set the "Memory allocated" to 256MB and the "CPU allocated" to 1 to keep costs down. (if your website has a lot of traffic, you should change these settings accordingly)

    8. In the authentication section, choose the option "Allow unauthenticated invocations" to enable the service for website usage (the service needs to be triggered on usage of your website and thus must be public available).
    9. Leave all other settings at their default values and click "Create".

    10. Now, do steps 1-9 again; this time it will be for the preview server (give this service a relevant service name like "gtm-ss-preview"). NB! the max instances here must be set to 1. In addition, you will also need to create an extra variable for the preview server called "RUN_AS_PREVIEW_SERVER" and set the value of this to "true". 

Once deployments of the two services are complete, you should see your two services in the Cloud Run interface accompanied with green checkmarks. 

 

Step 3: DNS Domain Mapping

To allow your website to send requests to the Cloud Run service, you need to map your website's domain to the Cloud Run service's URL. This can be done via the "MANAGE CUSTOM DOMAINS" option in the Cloud Run interface following these steps:

  1. Once you click the "MANAGE CUSTOM DOMAINS" option, you get the opportunity to choose "ADD MAPPING" which then will prompt a popup. 

  2. In the popup, you first select the service you want to map. Then you will need to verify the subdomain that the Cloud Run service will be running on. I will use the gtm.buhlian.dk subdomain for the production server and the debug.gtm.buhlian.dk for the preview server. If you have not verified your domain you can select the "Verify a new domain.." option in the dropdown and click "Continue". This should provide you with a link to Google's Webmaster Central, which provides you with a TXT key to include in your DNS setup for your domain (find this in your under settings in your web hosting account). Once the TXT key is added you should be able to see and select your domain in the dropdown. Once this is configured, you click "Continue" and is then prompted with 4 A and 4 AAAA keys you will need to add to your DNS setup. 
  3. Repeat step 1-2 for the preview service as well. Once the A and AAAA keys have been added, you will have to wait untill has mapped the domains - this will be done, when you see green checkmarks for each domain (gtm.buhlian.dk, debug.gtm.buhlian.dk in my case) in the Domain Mapping interface.
  4. Once the domain mapping has been finalized, we need to link the preview service to the production service. We do this by returning to the Cloud Run interface, where you should see the two services you created in step 2 (gtm-ss and gtm-ss-preview in my case). Click on the production service (gtm-ss) and then click "EDIT AND DEPLOY NEW REVISION". This will prompt the setup interface for your production service. In here, we now need to add an additional variable (besides the CONTAINER_CONFIG variable). Call the new variable "PREVIEW_SERVER_URL" and then add the url of the subdomain you mapped to your preview service - in my case this would be "https://debug.gtm.buhlian.dk"
  5. Click Create to deploy the production service with the new variable. Now, the production and preview Cloud Run services are linked.

 

Step 4: Validate the server-side GTM setup

Once you have green checkmarks for both the production and preview Cloud Run services and they have succesfully mapped to subdomains of your website, we are now ready to test the setup. Go to your new server-side GTM account. In the container settings add the production service subdomain to the Server container URL field (in my case, this would be "https://gtm.buhlian.dk"). Then return to the workspace section and click the "PREVIEW" button on the upper right to activate the preview of your server-side GTM endpoint - this will prompt a new browser tab. Now open yet a new browser tab and load your Server container url here (fx "https://gtm.buhlian.dk"). Then return to the preview tab - if the setup has been succesfully configured you should now see an incoming request with "/" as the request URL. Your server-side GTM has now been deployed and you can start utilize its many opportunities to optimize your web tracking, data analytics and data activations.

 

Want more?

Subscribe to get my latest posts in your inbox.