Skip to content

Server-side tracking with Google Tag Manager and Cloud run

Section titled “You won’t need the user’s consent anymore”

server_container_gtm

  • Familiar sections like tags, triggers, and variables.
  • Server-side specific sections like clients and transformations.

server side gtm dashboard

Once you hit save, a pop-up will appear in your new container. Click on “automatically provision tag server”.

Automatically provision server side

GTM will create a new server in the US-Central 1 region. Don’t worry, if your customers are in the EU, you can change the region later on.

add bliing account

Follow the steps and once you are done, you can select the billing account you added from the dropdown and click to provision the server.

server side billing account

In a few moments, you will see a new screen with your server’s details.

Server side intaance information

When you browse to Cloud Run in your GCP account, you will find two types of servers:

  • Server-side-tagging: The main server that receives data.
  • Server-side-tagging-preview: As the name indicates, this server will preview requests by the server.

Services in cloud run

Click on “EDIT & DEPLOY NEW REVISION” then scroll to the bottom.

Edit and deploy new revision

You will notice that our deployment is running on 0 to 1 server. This is fine for testing purposes, but the number of servers will likely not be enough for a production environment. Having only one server can cause abrupt pauses when there are traffic spikes.

Autoscaling instances

We need some margin of safety by upgrading to at least 2 and up to 10 instances. This will guarantee smooth functioning of your deployment. Even if traffic spikes, the new setup can handle it.

Changing your server’s location is a good idea, especially for businesses based in the EU. It brings your server closer to your customers and helps to stay in line with GDPR, for example.

Copy preview service

The simplest way to do this is by selecting and copying the server-side-tagging-preview.
Once copied, you can change the region to europe-west2 (England), for example.

The only things you need to change are:

  • The name of the new server:server-side-tagging-eu, for example.

change location

  • Check the option “Allow unauthenticated invocations”.

Allow unauthenticated invocations

Copy tagging server

We will do the same thing for the server-side-tagging. Ensure that you change the “PREVIEW_SERVER_URL” to point to the new preview server.

Add server tagging URL

Make sure to change the name and check “Allow unauthenticated invocations.”  You can delete the old setup and keep only EU services.

One of the main benefits of server-side GTM is to load cookies from a first-party context. The default domain is still considered as third party. We need to fix this by creating your own subdomain.

Go to your tagging server and browse to the integration
Section titled “Go to your tagging server and browse to the integration”

all custom domain 1

Select “Custom domains, Google Cloud Load Balancing”. Here, you need to add your custom domain, for example: ssgtm.hamzaelkharraz.com.

add custom domain 2

Next, enable all APIs and click on submit.

This process may take up to 24 hours or more.

Add DNS records to your hosting provider.

Once the DNS records are available, browse to your hosting provider and add the A record. If you don’t have access, you can ask your developer to do this for you.

Configure CDN record

By this time, we are ready to link our web container with the new server-side domain. We do this by adding the server URL to the configuration tag.

Google Tag Manager URL server

Server-side tracking will play a major role in a world that doesn’t rely on third-party cookies. It is a bit weird at first, but one could argue that learning is a must. More and more vendors like Facebook are pushing towards this approach.

Of course, there are many ways to set up server-side tagging. This approach is quite complicated for those new to GCP. Feel free to reach out if you are having issues with setting up your SSGTM.