Server-Side Implementation Guide
A step-by-step guide to deploying server-side tagging infrastructure on Google Cloud Platform.
Prerequisites
Before starting, ensure you have:
- [ ] Google Cloud Platform account with billing enabled
- [ ] Domain with DNS access (for first-party subdomain)
- [ ] Existing GTM web container
- [ ] GA4 property configured
- [ ] SSL certificate capability
Architecture Overview
┌─────────────────────────────────────────────────────────┐
│ Your Domain │
│ (example.com) │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────────────────┐ │
│ │ Website │ │ First-Party Domain │ │
│ │ │────────▶│ (gtm.example.com) │ │
│ │ GTM Web │ │ │ │
│ │ Container │ │ Cloud Run / App Engine│ │
│ └─────────────┘ │ GTM Server Container │ │
│ └───────────┬─────────────┘ │
│ │ │
└──────────────────────────────────────│──────────────────┘
│
▼
┌────────────────────────┐
│ Vendor Endpoints │
│ GA4, Meta CAPI, etc. │
└────────────────────────┘
Step 1: Create Server Container
In Google Tag Manager
- Navigate to Admin > Container > Create Container
- Select Server as the container type
- Name your container (e.g., "Server - Production")
- Click Create
Initial Configuration
After creation, you'll see provisioning options:
| Option | Best For | Cost | |--------|----------|------| | Automatically provision | Testing only | Free tier limited | | Manual provisioning | Production | Pay-as-you-go |
Always choose manual provisioning for production.
Step 2: Deploy on Google Cloud
Option A: Cloud Run (Recommended)
Cloud Run offers automatic scaling and pay-per-use pricing.
# Set your project
gcloud config set project YOUR_PROJECT_ID
# Enable required APIs
gcloud services enable run.googleapis.com
gcloud services enable secretmanager.googleapis.com
# Deploy the container
gcloud run deploy gtm-server \
--image gcr.io/cloud-tagging-10302018/gtm-cloud-image:stable \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--min-instances 1 \
--max-instances 10 \
--memory 512Mi \
--set-env-vars "CONTAINER_CONFIG=YOUR_CONTAINER_CONFIG"
Option B: App Engine
App Engine provides more predictable pricing for consistent traffic.
Create app.yaml:
runtime: custom
env: flex
automatic_scaling:
min_num_instances: 1
max_num_instances: 10
cool_down_period_sec: 60
cpu_utilization:
target_utilization: 0.6
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
env_variables:
CONTAINER_CONFIG: "YOUR_CONTAINER_CONFIG"
Deploy:
gcloud app deploy
Container Configuration
Get your container config from GTM:
- In GTM Server Container, go to Admin > Container Settings
- Copy the Container Config value
- Use this value in your deployment
Step 3: Configure First-Party Domain
DNS Setup
Create a subdomain pointing to your server container:
| Record Type | Name | Value | |-------------|------|-------| | CNAME | gtm | your-cloud-run-url.run.app | | A (alternative) | gtm | Your static IP |
SSL Certificate
For Cloud Run with custom domain:
# Map custom domain
gcloud run domain-mappings create \
--service gtm-server \
--domain gtm.example.com \
--region us-central1
SSL is automatically provisioned by Google.
Verify Setup
Test your endpoint:
curl -I https://gtm.example.com/healthy
# Should return 200 OK
Step 4: Configure Web Container
Update GA4 Configuration Tag
In your web GTM container:
- Open your Google Tag (GA4 Configuration)
- Go to Configuration Settings
- Set Server Container URL:
https://gtm.example.com
// The tag will now send data to your server instead of Google directly
// Before: www.google-analytics.com
// After: gtm.example.com → Google
First-Party Cookie Setup
Configure first-party cookies in your server container:
- In Server GTM, create a GA4 Client
- Enable Migrate to first-party cookies
- Set Cookie Domain:
.example.com
Step 5: Server Container Tags
GA4 Server Tag
- Create new tag: GA4
- Configure:
- Measurement ID:
G-XXXXXXXXXX - Enable Send to GA4
- Measurement ID:
Trigger Configuration
Create a trigger for GA4 events:
| Setting | Value | |---------|-------| | Trigger Type | Custom | | Fire on | Client Name equals GA4 |
Step 6: Verification
Testing Checklist
- [ ] Server container responds to health checks
- [ ] First-party domain resolves correctly
- [ ] SSL certificate is valid
- [ ] GA4 data appears in DebugView
- [ ] Cookies are set on first-party domain
- [ ] No CORS errors in browser console
Preview Mode
Use GTM Preview for both containers:
- Start Preview in web container
- Start Preview in server container
- Verify event flow from web → server → GA4
Debug Request
# Send test event to your server
curl -X POST https://gtm.example.com/g/collect \
-H "Content-Type: application/json" \
-d '{"client_id":"test","events":[{"name":"test_event"}]}'
Scaling Considerations
Cloud Run Scaling
# Recommended production settings
min-instances: 2
max-instances: 100
concurrency: 80
cpu: 1
memory: 1Gi
Monitoring
Set up alerts for:
| Metric | Warning | Critical | |--------|---------|----------| | Request latency | > 500ms | > 2s | | Error rate | > 1% | > 5% | | Instance count | > 50% max | > 80% max |
Cost Optimization
| Optimization | Impact | |--------------|--------| | Minimum instances: 1-2 | Reduce cold starts | | Request batching | Lower request count | | Regional deployment | Reduce latency | | Committed use discounts | 20-40% savings |
Common Issues
Container Not Starting
# Check logs
gcloud run logs read --service gtm-server --limit 50
CORS Errors
Ensure your server allows requests from your domain:
- The GTM server image handles this automatically
- Verify no proxy is stripping headers
Cookie Not Setting
- Verify SSL is working
- Check cookie domain configuration
- Ensure SameSite settings are correct
Previous: Server-Side Overview Next: Marketing Pixels