A CloudFront Function to route /metrics/* requests to Google Tag Gateway for Advertisers while preserving visitor geolocation information.
- Routes
/metrics/*requests to Google Tag Gateway - Automatically forwards visitor country and region information
- Uses CloudFront Functions 2.0 runtime for optimal performance
- Configurable Google Tag Gateway domain
-
Configure the domain
Edit
function.jsand replace the domain with your Google Tag Gateway endpoint:const GOOGLE_TAG_GATEWAY_DOMAIN = 'YOUR-TAG-ID.fps.goog';
-
Deploy to CloudFront
Create a CloudFront Function using the AWS Console, CLI, or Infrastructure as Code:
aws cloudfront create-function \ --name google-tag-gateway \ --function-config Runtime=cloudfront-js-2.0 \ --function-code fileb://function.js
-
Associate with Distribution
Add the function to your CloudFront distribution's viewer-request event.
The function intercepts requests to /metrics/* paths and:
- Extracts visitor country/region from CloudFront headers
- Routes the request to your Google Tag Gateway domain
- Adds
x-forwarded-countryandx-forwarded-regionheaders - Preserves all original query parameters and cookies
Verify setup by navigating to: https://yourdomain.com/metrics/healthy
The page should display "ok" if configured correctly.
MIT License - see LICENSE file for details.