This guide explains how to:

- Provision a preview server to enable the preview feature for the container.
- Provision a tagging server to handle live traffic.
- Increase or decrease the number of servers that are running your Google Tag Manager container.
- Keep your tagging server version updated after provisioning the server.

## Prerequisites

1. You need a GCP account. If you don't have one, [create a new GCP account](https://console.cloud.google.com).
2. You need a GCP billing account. If you don't have one, [create a GCP billing account](https://console.cloud.google.com/billing/create) (requires the Billing Account Creator role).
3. You need the Project Creator and the Billing Account User role. Learn more about [adding roles](https://cloud.google.com/resource-manager/docs/default-access-control#adding_a_billing_account_creator_and_project_creator).

## Provision a preview and tagging server

You can provision a Cloud Run service either automatically in Google Tag
Manager or manually in Google Cloud.
<button value="automatic" default="">Automatic Provisioning</button> <button value="manual">Manual Provisioning</button>

### Edit the service configuration

To change your service configuration:

1. [Open Cloud Run](https://console.cloud.google.com/run).
2. Select the service you need to adjust.
3. Click **Edit \& Deploy New Revision**.
4. Make changes and click **Deploy**.

### Cloud Run cost

In this Cloud Run configuration, each server costs approximately $45 /month
(USD). Each server is a Cloud Run instance with
[1 vCPU and 0.5GB memory](https://cloud.google.com/run/pricing) using the CPU
always allocated pricing model.

We recommend running a minimum of 2 instances to reduce the risk of data loss in
case of a server outage. However, you may choose to run fewer (or more) servers.
We expect that autoscaling 2-10 servers will handle 35-350 requests per second,
though the performance will vary with the number of tags, and what those tags
do.

Cloud Run will dynamically scale with load. The `max-instances` setting is the
worst case scenario for how much you will need to pay for resources. Cloud Run
won't provision that many instances unless necessary.

> [!TIP]
> **Tip:** To avoid unexpected costs, set up a [billing alert](https://cloud.google.com/billing/docs/how-to/budgets).

### Cloud Run cost estimation

To estimate the monthly cost of running your tagging servers, use the
[Google Cloud Pricing Calculator](https://cloud.google.com/products/calculator?dl=CjhDaVJoTTJObVlUTTNOaTB3TURJd0xUUmxaamd0WVRBeFpDMDRNVGhoTlRVMFl6WTFPRFFRQVE9PRAcGiRGRDJEQjgzNS00QjU3LTQwMUEtOTUzOC0xOEIxRjJGOENCN0Y). The calculator opens
with a pre-configured estimate for a default server-side tagging deployment. You
can adjust the settings to get a more accurate estimate based on your expected
traffic.

### Optional: Migrating from App Engine

If you previously created an App Engine deployment and have verified that it is
no longer receiving any traffic,
[disable the App Engine application](https://cloud.google.com/appengine/docs/standard/managing-projects-apps-billing#disabling_an_application)
to prevent unexpected billing charges.

### Optional: Multi-region deployment

If your website has a global presence or you would like to build redundancy into
the service, deploy the tagging servers to
[multiple regions](https://cloud.google.com/run/docs/multiple-regions).

Before you start:

1. [Create a load balancer](https://cloud.google.com/run/docs/multiple-regions#create-lb)
2. Note your chosen <var label="backend_name" translate="no">BACKEND_NAME</var>.

To add more regions to your deployment:

1. Replace <var label="backend_name" translate="no">REGION</var> with the region where the preview server is deployed. This may already be filled out if you followed the command line options to provision the preview and tagging server.
2. Replace <var label="backend_name" translate="no">CONTAINER_CONFIG</var> with container config string from Tag Manager. This may already be filled out if you followed the command line options to provision the preview and tagging server.
3. Replace <var label="backend_name" translate="no">NEW_REGION</var> with the new region where you would like the tagging server to be deployed.
4. Replace <var label="backend_name" translate="no">BACKEND_NAME</var> with the name you chose while provisioning the load balancer.
5. Optional: To add another region, substitute <var label="backend_name" translate="no">NEW_REGION</var> variable and the re-run the code snippet.

        gcloud run deploy "server-side-tagging" \
        --region NEW_REGION \
        --image gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable \
        --platform managed \
        --ingress all \
        --min-instances 2 \
        --max-instances 10 \
        --timeout 60 \
        --allow-unauthenticated \
        --no-cpu-throttling \
        --update-env-vars PREVIEW_SERVER_URL="$(
          gcloud run services describe server-side-tagging-preview \--region "REGION" \
          --format="value(status.url)")",CONTAINER_CONFIG="CONTAINER_CONFIG" && \

        gcloud compute network-endpoint-groups create server-side-tagging-neg \
        --region=NEW_REGION \
        --network-endpoint-type=SERVERLESS \
        --cloud-run-service="server-side-tagging" && \

        gcloud compute backend-services add-backend --global "BACKEND_NAME" \
        --network-endpoint-group-region=NEW_REGION \
        --network-endpoint-group=server-side-tagging-neg

### Optional: Disable logging

### Request logging

By default, information about every single request (e.g. request path, query
parameters, etc) is logged. If your tagging server handles a lot of requests
per month (e.g. greater than 1 million), those log messages may incur
significant [logging charges](https://cloud.google.com/logging). To reduce
or eliminate the logging charges, we recommend disabling the request
logging.

To disable request logging:

1. In Google Cloud platform, open the **[Logs Router](https://console.cloud.google.com/logs/router)** . Make sure you're in the project that matches your container ID:   
   ![screenshot of GCP project selector, showing a sample Tag Manager container
   ID.](https://developers.google.com/static/tag-platform/tag-manager/server-side/img/gtmaaa1010.png)
2. For the **Type** : *Cloud Logging bucket* , **Name** : *_Default* line, select the overflow menu, then click **Edit Sink**.
3. Under **Sink destination** , select logs bucket **_Default**.
4. Under **Choose logs to include in sink**, add a new line. Enter the
   following rule to the existing inclusion filter:

       NOT LOG_ID("run.googleapis.com/requests")

5. To also disable logging from the load balancer, add a new line and enter
   the following rule to the existing inclusion filter:

       NOT LOG_ID("requests")

   > [!NOTE]
   > **Note:** This will disable all logging from the load balancer, including requests that are not sent to the server container.

6. **Update Sink** to apply the changes. Now the requests will be excluded
   from logging.

7. Verify that no new requests are appearing in the
   **[Logs Explorer](https://console.cloud.google.com/logs/query)** logs.

### Console logging

The tagging server, clients, or tags in a container can log messages to
console that may incur [logging charges](https://cloud.google.com/logging).
To reduce or eliminate the logging charges, you can disable unwanted console
log messages.

Identify unwanted console logs:

1. In GCP, open the [Logs Explorer](https://console.cloud.google.com/logs/query).
2. Look for any unwanted log messages that originate from your tags. For
   example:

   A tag might send the following logs:

       const logToConsole = require('logToConsole');

       logToConsole('Custom message: ' + data.param1);
       logToConsole('An important message to keep around!');
       data.gtmOnSuccess()

   Look for the corresponding log messages in the
   `textPayload` field:  

   ![screenshot of GCP Logs Explorer, showing sample
   logs.](https://developers.google.com/static/tag-platform/tag-manager/server-side/img/gcp-custom-console-example.png)

To disable console log message:

1. In Google Cloud platform, open the **[Logs Router](https://console.cloud.google.com/logs/router)** . Make sure you're in the project that matches your container ID:   
   ![screenshot of GCP project selector, showing a sample Tag Manager container
   ID.](https://developers.google.com/static/tag-platform/tag-manager/server-side/img/gtmaaa1010.png)
2. For the **Type** : *Cloud Logging bucket* , **Name** : *_Default* line, select the overflow menu, then click **Edit Sink**.
3. Under **Sink destination** , select logs bucket **_Default**.
4. Under **Choose logs to include in sink**, add a new line. Enter the
   following rule to the existing inclusion filter:

       NOT textPayload:"Custom message:"

   For your console logs, replace the
   <var label="Custom message" translate="no">Custom message:</var> text
   with a substring from console log you wish to disable. For more
   elaborate filters, utilize the
   [logging query language](https://cloud.google.com/logging/docs/view/logging-query-language).
5. **Update Sink** to apply the changes. The matching `logToConsole`
   message should be excluded from logging.

6. Verify that no new console log messages are appearing in the
   **[Logs Explorer](https://console.cloud.google.com/logs/query)**.

## 2. Map the deployment to your custom domain

[Set up a custom domain](https://cloud.google.com/run/docs/mapping-custom-domains) to use a domain other than the default address that Cloud Run provides.

## 3. Add the server URL to Google Tag Manager

Now that you have a server, you need to make sure that Google Tag Manager knows
it should use your server.

1. Open [Google Tag
   Manager.](https://tagmanager.google.com/)

2. Click on the server container you want to point to your tagging server.

3. Open your server container settings in the **Admin** tab \> **Container
   Settings**.

4. Click **Add URL** and paste your server URL.

5. **Save** and go back to your workspace.

## 4. Validation

Now that you've set up your tagging server, make sure that it works as intended.

### UI verification

In your Tag Manager workspace, click the **Preview** button. If the preview page
loads, then everything is set up correctly.

### API verification

You can also verify that the server is responding to health checks using its
API:

1. Copy your **Server container URL** from **Admin \> Container Settings**.
2. Open a new browser tab.
3. Paste the URL and append `/healthy` to the path. For example: `https://www.example.com/metrics/healthy`
4. If your service works, you should see the text `ok` on the page.

If requests for a specific product are missing, verify that an event is being
triggered. The `config` command initializes the product, but data is usually
only transmitted when an `event` is called.

> [!NOTE]
> **Note:** Google Analytics is an exception; it automatically sends a `page_view` event when initialized using `config`, unless `send_page_view` is set to `false`.

For more information on best practices for server side tagging verification,
see [Custom domain configuration](https://developers.google.com/tag-platform/tag-manager/server-side/custom-domain?option=same-origin.).

### Preview multiple URLs

If you have mapped multiple domains to a single tagging server, make sure each
URL is added to the container settings.

If you provided multiple URLs, all paths (the string after the domain name) must
match.

| Works | Does not work |
|---|---|
| URL 1: `example.com/abc` URL 2: `example2.com/abc` | URL 1: `example.com/abc` URL 2: `example2.com/def` |

If multiple URLs are added, you will see an icon next to the **Preview** button
that lets you select the URL to preview.

## Update the tagging server version

> [!NOTE]
> **Note:** If you provisioned your tagging servers using the [App Engine setup guide](https://developers.google.com/tag-platform/tag-manager/server-side/app-engine-setup#update_the_tagging_server_version) or [Manual setup guide](https://developers.google.com/tag-platform/tag-manager/server-side/manual-setup-guide#update_the_tagging_server_version), please follow the instructions listed there.

New tagging server updates contain security vulnerability fixes and new
features. We recommend at least updating your tagging server for each major
version release (e.g. upgrading from version 1.x.x to 2.x.x) when Tag Manager
notifies you to update.

To update your tagging server, deploy a new revision using the same settings
you used previously.

1. Open [Cloud Run](https://console.cloud.google.com/run).
2. Select the service you want to update.
3. Click **Edit \& Deploy New Revision**.
4. Make sure the **Container image URL** is set to `gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable` and click **Deploy**.

To verify that the update was successful:

1. In your server container, click the **Preview** button to start a new [debug](https://developers.google.com/tag-platform/tag-manager/server-side/debug) session and send a request on a separate tab.
2. In the Summary, select the **Console** tab and make sure there are no messages asking you to update the tagging server.

> Tag Manager may show messages asking you to update your tagging server for
> up to a day after the server has been updated successfully. However, the preview
> page will show an up to date message about the tagging server version.