Google tag gateway for advertisers: Load Google scripts first-party

This document is for developers who use server-side tagging and want to serve Google scripts in a first-party context.

Google tag gateway for advertisers lets you load Google scripts, such as gtm.js, directly from your first-party infrastructure instead of from Google's servers. This lets you serve data within a first-party context either through your tagging server or through a CDN.

Before you begin

This guide assumes that you have done the following:

To get started, pick how you want to serve your Google scripts.

Serve scripts with a Tagging server

You can set up your Tagging server to serve Google scripts either from a path on the same domain as your website (depicted below) or as a subdomain on your site. This setup adds more load on your Tagging server and may incur a higher cost due to additional egress script requests from your server-side instance.

Same-origin overview with server-side tagging.

Step 1: Configure the client

To establish a first-party context between your web container and your tagging server, Google scripts need to be loaded through your server.

To load Google scripts through your server container:

  1. Open Google Tag Manager
  2. In your server container, click Clients.
  3. Click New
  4. Select the Google Tag Manager: Web Container client type. Choose client type dialog with Tag Manager: Web Container client highlighted

  5. In Client Configuration:

    • Add Container ID: Enter the container ID of the Tag Manager web container that you will use on your website.
    • Automatically serve all dependent Google scripts: When enabled (default setting), the tagging server will automatically serve scripts that are needed by the root Google script. This removes the need to allowlist each container that the Google script requires. If you only want to serve the initial container from the tagging server, deselect this option.
    • Enable region-specific settings: Use this option to trigger certain tags based on the user's location. Learn more.

    View of the Tag Manager web container client

  6. Name the client and Save.

  7. Publish the workspace.

Step 2: Update the script source domain

By default, Tag Manager loads their dependencies from Google-owned servers, such as https://googletagmanager.com. You need to update the script URL on your website to load dependencies through your own server.

Tag Manager

To load the Tag Manager code using your server container:

  1. Locate the existing Tag Manager code on the page.
  2. Replace the string https://example.com/metrics with the domain name of your tagging server in the <head> and <body> of the Google Tag Manager installation code.
  • Head:
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://example.com/metrics/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','TAG_ID');</script>
<!-- End Google Tag Manager -->
  • Body:
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://example.com/metrics/ns.html?id=TAG_ID"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

gtag.js

To load the Google tag using your server container:

  1. Locate the existing Google tag in your code or copy the following snippet.
  2. Edit the snippet to use your server infrastructure:

    • Replace the string https://www.googletagmanager.com with your tagging server URL, for example https://example.com/metrics.
    • Replace TAG_ID with your measurement ID (G-ID).
<!-- Google tag (gtag.js) -->
<script async src="https://example.com/metrics/gtag/js?id=TAG_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'TAG_ID', {
    server_container_url: 'https://example.com/metrics',
  });
</script>

Step 3: Verify the source of your scripts

The following instructions show how to test using Google Chrome. You can use any browser of your choice, but the steps might differ.

To test if your server container is loading the dependencies:

  1. Open your website in a new browser tab.
  2. Open the browser's developer tools. In Google Chrome, you can right-click anywhere on the page and select Inspect.
  3. In the developer tools, open the Sources tab. This window displays all dependencies that were loaded when you opened the website.

    • ✅ Your implementation is correct if gtm.js or gtag is loaded from the source you specified.

    Screenshot of the developer tools with your own server as the source for Google scripts

    • ❌ If the Sources tab displays www.googletagmanager.com as the source for gtm.js or gtag, the dependencies are still loaded from Google servers.

    Screenshot of the developer tools with www.googletagmanager.com as the source for Google scripts

    • Check if you've modified the source URL in your code, see step 2.
    • Check if the code is live.

Next steps

If you are using consent mode, you can set up region-specific tag behavior for even more control.