How to Build Your Own Server-Side GTM Server on a VPS (Step by Step)

Meta, Google & TikTok Ads Specialist · Upwork Verified Expert

Build your own server-side GTM server on a VPS step by step guide

Paying a monthly fee for every server-side GTM server adds up fast when you track several websites. The good news: you can build your own. With one VPS, Docker and the free Google tagging server image, you get the same setup that hosted services sell, running on your own subdomain, under your full control.

This guide walks you through the whole process A to Z: which hosting to buy, what to install, which DNS records to add, the exact prompts to paste into Claude Code so it builds the server for you, how to add a control panel, and finally how to connect Google Tag Manager. If you can copy and paste, you can do this.

Build your own server-side GTM server on a VPS step by step guide

Quick Answer: How to Build a Server-Side GTM Server

  • Buy a VPS (for example Hostinger KVM 1 or KVM 2) with Ubuntu 24.04. Shared hosting cannot run it.
  • Create a Server container in GTM and copy the Container Config string.
  • Add two DNS A records (sgtm and sgtm-preview) that point to the VPS IP.
  • Open the VPS terminal, install Claude Code, and paste the build prompt from this guide.
  • Claude installs Docker, the GTM tagging server, the preview server and free SSL.
  • Add https://sgtm.yourdomain.com to GTM and send your Google tag to it. Test, then publish.

How a Self-Hosted Server-Side GTM Server Works

In normal tracking, the browser sends data straight to Meta, Google and TikTok. Ad blockers and browser privacy rules stop a lot of it. With server-side tagging, the browser sends events to your own subdomain, such as sgtm.yourdomain.com. Your server then forwards clean, enriched events to each platform. If you are new to the idea, read server-side tracking vs client-side tracking first.

How a self-hosted server-side GTM server on a VPS works: website, subdomain, Docker sGTM, ad platforms

Google publishes the tagging server as a free Docker image. On a VPS you run two copies of it:

  • Tagging server: receives live traffic from your website and runs your server tags (Meta Conversion API, GA4, Google Ads and others).
  • Preview server: only used when you click Preview in GTM, so you can debug safely.
  • Caddy: a small web server in front of both that gets free SSL certificates automatically and passes traffic to the right container.

Because the subdomain belongs to you, cookies are set as first-party cookies and last longer. I explain this in detail in first-party cookies and custom subdomains for server-side GTM.

Hosting vs VPS: What You Need to Buy

This is the most common confusion. Normal shared or WordPress hosting runs your website, but it does not give you root access or Docker, so it cannot run a server-side GTM server. A VPS (virtual private server) gives you a full Linux machine where you can install anything.

Shared hosting vs VPS hosting for a server-side GTM server

So in practice you keep your website on its current hosting, and you buy one VPS only for tracking. One VPS can serve many websites, each with its own subdomain and its own GTM container. If you are still comparing options, my guide on server-side GTM hosting options compares Google Cloud, managed hosting and self-hosting.

What You Need Before You Start

  • A domain you control, plus access to its DNS (Hostinger hPanel, Cloudflare, Namecheap or any other provider).
  • A VPS with Ubuntu 24.04 LTS and root access.
  • A Google Tag Manager account where you can create a new container.
  • A Claude Pro or Max plan for Claude Code. The free Claude plan does not include Claude Code. If you prefer, you can skip Claude and use the manual commands later in this guide.
  • About 30 to 60 minutes.

Step 1: Buy and Set Up the VPS

I use Hostinger VPS hosting because it is cheap, fast to set up and has a browser terminal, but any KVM VPS with Ubuntu works the same way (DigitalOcean, Hetzner, Vultr, Contabo and others).

PlanvCPURAMNVMe diskBest for
KVM 114 GB50 GBOne store or a few small websites
KVM 228 GB100 GBAgencies, many client containers or a panel
KVM 4416 GB200 GBHigh traffic or many busy stores

At the time of writing, Hostinger lists KVM 1 from $6.49 per month and KVM 2 from $8.99 per month on a 24-month term, with higher renewal prices. Prices change by region and promotion, so check the live page before you buy.

VPS order settings for a server-side GTM server: plan, location, Ubuntu 24.04, root password

During the order and setup screen, choose:

  1. Location: the data center closest to most of your visitors.
  2. Operating system: Ubuntu 24.04 LTS as a plain OS, with no control panel such as cPanel or CyberPanel. We do not need one.
  3. Root password: long and unique. Save it in a password manager.
  4. SSH key: optional but recommended.

When the VPS is ready, open its overview page and copy the IPv4 address. You need it in Step 3.

Step 2: Create the GTM Server Container

Where to find the Container Config string in a Google Tag Manager server container
  1. Go to tagmanager.google.com and open your account.
  2. Click Create Container, give it a name such as yourdomain.com Server, and choose Server as the target platform.
  3. On the install screen choose Manually provision tagging server.
  4. Copy the Container Config string and keep it in a notepad. It is a long text that starts with letters like aWQ9R1RN.

If you close that window, you can find the string again under Admin, Container Settings in the server container.

Step 3: Add the DNS Records

Now point two subdomains to the VPS. Use any names you like. In this guide I use sgtm for the tagging server and sgtm-preview for the preview server.

DNS A records for server-side GTM subdomains pointing to the VPS IP

On Hostinger hPanel

  1. Go to Domains, select your domain, then open DNS / Nameservers.
  2. Add a record: Type A, Name sgtm, Points to your VPS IP, TTL 300.
  3. Add a second record: Type A, Name sgtm-preview, same IP.

On Cloudflare

  1. Open your domain, then DNS, Records, Add record.
  2. Add the same two A records.
  3. Set the proxy status to DNS only (grey cloud). This lets Caddy get the SSL certificate directly.

Wait a few minutes, then check both names on dnschecker.org. They must show your VPS IP before you continue, otherwise SSL will fail.

Step 4: Open the Terminal and Update the Server

You can use the Browser terminal button in the Hostinger VPS panel, or connect from your own computer with SSH (Terminal on Mac and Linux, PowerShell on Windows):

ssh root@YOUR_VPS_IP

Type yes the first time, then enter your root password. Now update the server and install two small tools:

apt update && apt upgrade -y
apt install -y curl tmux

tmux keeps your session running even if your internet drops. Start one now:

tmux new -s sgtm

If you get disconnected later, log in again and run tmux attach -t sgtm to continue exactly where you left off.

Step 5: Install Claude Code on the VPS

Terminal commands to update an Ubuntu VPS and install Claude Code for a server-side GTM build

Claude Code is an AI assistant that works inside the terminal. It can read your server, run commands, write config files and test the result, while asking your permission before each action. Install it with the official command from the Claude Code setup docs:

curl -fsSL https://claude.ai/install.sh | bash

Close and reopen the terminal (or run source ~/.bashrc), then create a working folder and start Claude:

mkdir -p /opt/sgtm && cd /opt/sgtm
claude

The first time, Claude shows a login link. Open it in your browser, sign in with your Claude Pro or Max account, paste the code back into the terminal, and you are ready.

Step 6: Paste the Build Prompt

Copy the prompt below, replace the four values in the My details section with your own, and paste it into Claude Code. This is the same prompt structure I use for my own servers.

You are setting up a production server-side Google Tag Manager (sGTM) server on this Ubuntu 24.04 VPS. I am logged in as root.

My details:
- Tagging server domain: sgtm.yourdomain.com
- Preview server domain: sgtm-preview.yourdomain.com
- GTM Container Config string: PASTE_YOUR_CONTAINER_CONFIG_HERE
- Email for SSL certificates: you@yourdomain.com

Do the following step by step. Before each step, tell me in one line what you are going to do:
1. Check the OS version, RAM and free disk space.
2. Install Docker Engine and the Docker Compose plugin from Docker's official repository.
3. Confirm that both domains resolve to this server's public IP. If not, stop and tell me exactly which DNS record to fix.
4. In /opt/sgtm create a docker-compose.yml with three services:
   - sgtm-preview: image gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable with CONTAINER_CONFIG and RUN_AS_PREVIEW_SERVER=true
   - sgtm: the same image with CONTAINER_CONFIG and PREVIEW_SERVER_URL=https://sgtm-preview.yourdomain.com
   - caddy: caddy:2 as a reverse proxy on ports 80 and 443 with automatic HTTPS for both domains
   Keep the Container Config in a .env file with chmod 600. Never publish port 8080 to the internet. Use restart: unless-stopped and Docker log rotation (max 10 MB, 3 files).
5. Enable the UFW firewall and allow only SSH (22), HTTP (80) and HTTPS (443).
6. Start the stack, then test https://sgtm.yourdomain.com/healthz and https://sgtm-preview.yourdomain.com/healthz and show me the result.
7. Add a weekly cron job that pulls the latest gtm-cloud-image and recreates the containers.
8. Finish with a short summary: running containers, URLs, file locations, and the commands to restart, view logs and update.
Ask me before you delete or overwrite anything.

What you will see after you press Enter

  • Claude explains its plan, then asks Do you want to proceed? before running commands. Choose Yes to approve one command, or Yes, and don’t ask again for that type of command (for example apt or docker) to move faster.
  • It installs Docker. This takes 1 to 3 minutes.
  • It checks your DNS. If a record is wrong, it stops and tells you what to change. Fix it in your DNS panel, wait a few minutes and type check again.
  • It writes docker-compose.yml, .env and Caddyfile, starts the containers and requests the SSL certificates.
  • It opens both /healthz URLs. When you see ok, your server-side GTM server is live.
  • At the end you get a summary with every command you need later. Save it.

If anything fails, just tell Claude what you see, for example the preview URL shows 502. It reads the logs and fixes the cause.

Manual Setup Without Claude Code

Prefer to do it yourself? These are the same files, based on Google’s manual setup guide for server-side tagging. First install Docker with the official Docker guide for Ubuntu or the convenience script:

curl -fsSL https://get.docker.com | sh
mkdir -p /opt/sgtm && cd /opt/sgtm

Create /opt/sgtm/.env (run nano .env, paste, save with Ctrl+O and exit with Ctrl+X):

CONTAINER_CONFIG=PASTE_YOUR_CONTAINER_CONFIG_HERE
SGTM_DOMAIN=sgtm.yourdomain.com
PREVIEW_DOMAIN=sgtm-preview.yourdomain.com

Create /opt/sgtm/docker-compose.yml:

services:
  sgtm-preview:
    image: gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable
    restart: unless-stopped
    environment:
      CONTAINER_CONFIG: "${CONTAINER_CONFIG}"
      RUN_AS_PREVIEW_SERVER: "true"

  sgtm:
    image: gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable
    restart: unless-stopped
    environment:
      CONTAINER_CONFIG: "${CONTAINER_CONFIG}"
      PREVIEW_SERVER_URL: "https://${PREVIEW_DOMAIN}"
    depends_on:
      - sgtm-preview

  caddy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    environment:
      SGTM_DOMAIN: "${SGTM_DOMAIN}"
      PREVIEW_DOMAIN: "${PREVIEW_DOMAIN}"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config

volumes:
  caddy_data:
  caddy_config:

Create /opt/sgtm/Caddyfile:

{$SGTM_DOMAIN} {
    reverse_proxy sgtm:8080
}

{$PREVIEW_DOMAIN} {
    reverse_proxy sgtm-preview:8080
}

Start everything and open the firewall:

chmod 600 .env
docker compose up -d
ufw allow 22 && ufw allow 80 && ufw allow 443 && ufw --force enable
curl https://sgtm.yourdomain.com/healthz

When the last command prints ok, the server is working. Caddy renews the SSL certificates automatically, as described in the Caddy automatic HTTPS docs.

Step 7: Add More Websites and a Control Panel

One VPS can run a separate sGTM container for every website you manage. Each website needs its own GTM server container, its own Container Config and its own two DNS records. Then paste this prompt into Claude Code:

Add a new server-side GTM container for another website on this server.
- Tagging domain: sgtm.secondsite.com
- Preview domain: sgtm-preview.secondsite.com
- Container Config: PASTE_THE_NEW_CONTAINER_CONFIG
Create a separate sgtm and preview service pair with unique names in the same compose project, add both domains to the Caddyfile, reload Caddy without downtime, and test /healthz on both new domains. Do not change or restart the existing containers.

Optional: build your own sGTM control panel

If you manage many websites, or you want to offer server-side tracking to clients, a panel saves a lot of time. This prompt asks Claude to build a simple one:

Build a simple web control panel for this sGTM server at panel.yourdomain.com (I have already pointed its A record to this server).
Requirements:
- Admin login with a strong password stored as a hash, served over HTTPS through Caddy.
- Dashboard listing every sGTM container: website, tagging domain, preview domain, status, uptime, and requests in the last 24 hours and 30 days (read from the Caddy access logs).
- Add website form: tagging domain, preview domain and Container Config. The panel checks that the DNS A records point to this server, then creates the containers, updates Caddy and shows the /healthz result.
- Buttons to start, stop, restart, view the last 100 log lines, and delete a container (delete must ask for confirmation).
- Brand colors: primary #2563EB, dark #0F172A, accent #06B6D4.
- Run the panel itself in Docker, keep its data in a volume, protect the Docker socket, and write a README that explains backup and update.
Show me the plan first, wait for my OK, then build it and test every feature before you finish.

After it finishes, open https://panel.yourdomain.com, log in with the admin details Claude gives you, and you will see your containers. From now on, adding a website is: create the GTM server container, add two DNS records, then fill in the Add website form.

Step 8: Connect Google Tag Manager to Your Server

Connect GTM to your own server: server container URL and server_container_url in the Google tag

In the server container

  1. Open your server container and go to Admin, Container Settings.
  2. Under Server container URLs, add https://sgtm.yourdomain.com and save.
  3. The GA4 client is already in a new server container. Add your server tags, for example a Meta Conversion API tag, a GA4 tag and a Google Ads conversion tag.
  4. Click Submit, then Publish.

In the web container

  1. Open your Google Tag (the tag with your GA4 measurement ID).
  2. Under Configuration settings, add the parameter server_container_url with the value https://sgtm.yourdomain.com.
  3. Save, test in Preview and publish.

Your events now travel through your own server. For platform tags, follow my step-by-step guides for Meta Conversion API with GTM, TikTok Events API and Google Ads Enhanced Conversions. If you run the Meta Pixel and CAPI together, set up event_id deduplication so purchases are not counted twice.

Step 9: Test and Go Live

Checklist to test a self-hosted server-side GTM server: healthz, SSL, preview, GA4, Meta server events
  1. Open https://sgtm.yourdomain.com/healthz. It must show ok.
  2. Check the padlock in the browser address bar. The SSL certificate must be valid.
  3. In the server container click Preview. A new tab opens without errors.
  4. Visit your website in another tab. A page_view request from the GA4 client appears in the server Preview.
  5. In Meta Events Manager open Test Events and confirm that events arrive with the Server connection method.

When all five pass, publish both containers. Keep an eye on event counts in Meta, GA4 and Google Ads for the next 2 to 3 days to compare with your old numbers.

Security and Maintenance

  • Update the tagging image regularly. Google ships security fixes and new features. In /opt/sgtm run docker compose pull && docker compose up -d, or let the weekly cron job from the prompt do it.
  • Keep Ubuntu patched with apt update && apt upgrade -y once a month.
  • Take a snapshot in your VPS panel before big changes. Hostinger includes weekly backups and a manual snapshot.
  • Never open port 8080 to the internet. Only Caddy on 80 and 443 should be public.
  • Use SSH keys and a strong root password, and never share the Container Config string publicly.
  • Watch the resources with docker stats. If memory stays near the limit, upgrade the plan from KVM 1 to KVM 2.

Many tracking problems are not server problems at all. Read 7 server-side tracking mistakes that quietly break your data before you go live, and use these Event Match Quality tips to send better customer data.

Troubleshooting Common Errors

ProblemLikely causeFix
SSL certificate failsDNS not pointing to the VPS yet, or Cloudflare proxy is orangeWait for DNS, set the record to DNS only, then run docker compose restart caddy
502 Bad GatewayA tagging container is stopped or still startingRun docker compose ps and docker compose logs sgtm
GTM Preview does not loadWrong PREVIEW_SERVER_URL or preview record missingCheck the preview domain in .env and DNS, then docker compose up -d
Site cannot reach the serverFirewall blocks 80 or 443Run ufw status and allow both ports
No events in server Previewserver_container_url missing in the Google tagAdd the parameter, then publish the web container
Claude Code says command not foundShell not reloaded after installRun source ~/.bashrc or reopen the terminal

Frequently Asked Questions

Can I run a server-side GTM server on shared hosting?

No. Shared and WordPress hosting do not give root access or Docker, which the Google tagging server needs. Keep your website on your hosting and use a separate VPS for server-side GTM.

Which VPS plan do I need for server-side GTM?

For one store or a few small websites, a 1 vCPU and 4 GB RAM plan such as Hostinger KVM 1 is a good start. For an agency with many client containers or a control panel, choose 2 vCPU and 8 GB RAM such as KVM 2.

Do I need Claude Code to build the server?

No. Claude Code makes it faster because it installs and tests everything for you, but you can follow the manual Docker Compose and Caddy setup in this guide instead.

Is a self-hosted sGTM server cheaper than Stape or Google Cloud?

Usually yes when you track several websites, because one VPS can run many containers for a fixed monthly price. You do take on the updates and monitoring yourself.

Can one VPS run sGTM for many websites?

Yes. Each website gets its own GTM server container, its own Container Config and its own subdomain, and all of them can run on the same VPS as long as it has enough memory.

Do I need Cloudflare for server-side GTM?

No. Any DNS provider works, including Hostinger hPanel. If you use Cloudflare, set the sGTM records to DNS only so Caddy can issue the SSL certificate.

How do I update the Google tagging server image?

In the project folder run docker compose pull and then docker compose up -d. The build prompt in this guide also adds a weekly cron job that does it automatically.

Final Thoughts

Building your own self-hosted server-side GTM server is easier than it looks: one VPS, two DNS records, one prompt, and about an hour of your time. You get first-party tracking on your own domain, full control of your data and a fixed monthly cost, whether you track one store or twenty client websites. If you would rather have it done for you, see my server-side tracking service, or the server-side tracking service in Bangladesh if your business is in Bangladesh.

Useful Resources

Want me to build your server-side tracking for you?

I set up server-side GTM, Meta Conversion API, GA4 and Google Ads tracking on my own servers for stores and service businesses. You get the server, the subdomain and the tags, tested and ready.

Get a Free Ads & Tracking Audit

Send me your website and tell me what you need. I will check your ads setup, Pixel, Conversion API and GA4 tracking, and reply with clear next steps. No obligation.

Picture of Rubel Hasan Rokib

Rubel Hasan Rokib

Data-driven digital marketer and tracking expert from Bangladesh with 5+ years of agency experience. I help brands grow with Meta Ads, Google Ads, TikTok Ads, GA4, Conversion API and server-side tracking. Upwork Verified Expert with 100% Job Success.

Join the Discussion

Have a question about this guide or your own setup? Leave a comment below. I read and reply to every comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Reading

More Articles You Might Like

ChatGPT কি ইসলামবিদ্বেষী, নুরুদ্দিন জেঙ্গিকে জঙ্গি লেখা নিয়ে আমার অভিজ্ঞতা
AI Tools

ChatGPT কি ইসলামবিদ্বেষী? সুলতান নুরুদ্দিন মাহমুদ জেঙ্গিকে “জঙ্গি” লেখা ও দাড়ি-টুপি মানেই অপরাধী: আমার অভিজ্ঞতা ও প্রমাণ

ChatGPT সুলতান নুরুদ্দিন মাহমুদ জেঙ্গির নাম “জঙ্গি” লিখেছে, আর এআই ছবিতে দাড়ি-টুপি পরা মানুষকে অপরাধী দেখানো হচ্ছে। আমার অভিজ্ঞতা, স্ক্রিনশট ও গবেষণার প্রমাণ।

Read Article →