How to implement server-side Google Tag Manager

Author:

Server-Side Google Tag Manager (SSGTM) is an advanced method of using Google Tag Manager (GTM) where the tag firing process is handled on a server rather than the client (browser) side. This implementation enhances privacy, performance, and control over how data is sent to third-party platforms. Server-side GTM provides several advantages, such as increased data security, reduced page load times, and better management of user consent for tracking.

This guide will walk you through the process of implementing Server-Side Google Tag Manager and its key benefits.

What is Server-Side Google Tag Manager?

Server-Side Google Tag Manager is a feature that allows the management of tags on a server rather than in the browser. Instead of firing tags directly from the user’s browser (client-side), the data is first sent to a server container, which processes the information and then forwards it to various third-party platforms (such as Google Analytics, Facebook, etc.).

Benefits of Server-Side GTM

  1. Improved Performance: Reduces client-side load because the browser does not need to load multiple JavaScript libraries, which can enhance page speed.
  2. Enhanced Data Privacy: Server-side tags can collect, process, and forward data without exposing sensitive user data directly to third-party services, helping ensure better privacy compliance.
  3. Reduced Ad Blockers Impact: Since requests are routed through a server rather than the client, server-side tagging is less likely to be blocked by ad blockers.
  4. Greater Control and Security: Server-side tagging gives you more control over the data that is sent to third-party services, and you can manage user consent and privacy preferences at the server level.

Prerequisites for Server-Side Google Tag Manager

Before you begin implementing server-side Google Tag Manager, you need the following:

  • A Google Cloud Platform (GCP) account to host the server container.
  • A Google Tag Manager account with admin access.
  • A basic understanding of cloud computing and server management.

Steps to Implement Server-Side Google Tag Manager

Step 1: Create a Server-Side Container in Google Tag Manager

  1. Login to Google Tag Manager:
  2. Create a New Container:
    • In the GTM dashboard, click on the Admin section.
    • Under the Container column, select Create Container.
    • Choose Server as the container type. This will configure your container to work in server-side mode.
  3. Configure the Container:
    • Name your container and select the Server option.
    • After creating the container, Google Tag Manager will provide you with a unique container ID.

Step 2: Set Up a Server to Host the Container

To run a server-side container, you need a server to host the container. Google provides an option to use Google Cloud Platform (GCP) to host your container.

  1. Sign In to Google Cloud:
  2. Create a New Project:
    • From the Google Cloud Console, click on Select a Project and create a new project for your GTM server-side container.
  3. Enable the GTM Server-Side API:
    • In your Google Cloud project, navigate to APIs & Services and enable the Tag Manager Server-Side API.
  4. Set Up App Engine:
    • GTM server-side containers are hosted on Google Cloud’s App Engine, so you need to set up App Engine.
    • In the Google Cloud Console, go to the App Engine section, click Create Application, and select your region.
  5. Deploy the Container:
    • Follow the prompts in the Google Cloud Console to deploy the GTM container. You will be asked to deploy the container image to your App Engine instance.

    This deployment will automatically create a secure endpoint (with a URL) to which you can send data from your website.

Step 3: Set Up the Server-Side Tagging Environment

Once your container is hosted on a server, you can configure it similarly to the client-side Google Tag Manager interface, but with additional server-side options.

  1. Configure Tags:
    • Go back to your GTM dashboard, and in the server-side container, you can configure tags just like in the client-side container. For example, you can configure tags for Google Analytics, Google Ads, Facebook Pixel, or custom server-side tags.
  2. Set Up Data Streams:
    • You will need to set up data streams that define how data is passed between your website (client-side) and the server container.
    • For example, use HTTP Requests to send data from the client (browser) to the server container.
    • You can do this by configuring a Measurement Protocol to send events from your website to the server container via HTTP requests.

Step 4: Set Up Client-Side to Server-Side Communication

To send data from your website to the server container, you need to implement a client-side container (your regular GTM container) that will forward data to the server container.

  1. Create a New Client-Side Container:
    • In Google Tag Manager, create a new web container for your website.
    • Install the GTM snippet on your website as you would for a typical GTM implementation.
  2. Configure HTTP Request to Server Container:
    • Inside the client-side GTM container, create tags that send data to the server-side container via HTTP requests. You can use Custom HTML tags or JavaScript to send data to the server endpoint.
    • The request typically involves sending the data as JSON or URL parameters to the server container’s endpoint URL.

    Example of sending data from the client to the server container:

    javascript
    fetch('https://<server-container-url>/collect', {
    method: 'POST',
    headers: {
    'Content-Type': 'application/json'
    },
    body: JSON.stringify({ event: 'page_view', page_url: window.location.href })
    });

Step 5: Set Up Tags and Triggers in the Server-Side Container

  1. Add Tags:
    • In your server-side GTM container, configure tags that correspond to the platforms where you want to send data (e.g., Google Analytics, Facebook, etc.).
    • You can set up standard tags like Google Analytics (GA4) or custom tags using JavaScript or third-party APIs to forward data.
  2. Set Up Triggers:
    • Create triggers in the server container to fire the tags based on the data received from the client-side container.

Step 6: Test and Debug

  1. Preview Mode:
    • Use the Preview mode in both client-side and server-side GTM containers to test the implementation. This allows you to ensure that data is properly sent from the website to the server and processed accordingly.
  2. Use Server Logs:
    • Check server logs in Google Cloud to verify that data is being received and processed correctly by the server-side container.
  3. Monitor Tag Firing:
    • In Google Tag Manager’s server container interface, use the Tag Assistant to monitor the firing of tags and troubleshoot any issues.

Step 7: Publish the Server-Side Container

Once you’ve confirmed that everything is working correctly in preview mode and testing, you can publish the server-side container to make it live. This will ensure that all data processing and forwarding to third-party platforms is done through the server-side GTM setup.

Conclusion

Server-Side Google Tag Manager provides an enhanced and more secure way to manage tags for your website, offering better control over data privacy, performance, and security. Implementing server-side GTM involves setting up a server container, deploying it on Google Cloud, configuring tags and triggers, and setting up communication between client-side and server-side containers. By following these steps, you can leverage the power of server-side tagging to optimize your tracking and analytics setup while ensuring data privacy and security.