This project is a Cloudflare Worker script designed to act as a proxy for Google Analytics 4 (GA4) data collection and provide additional features like modifying analytics script behavior. The script integrates and modifies the lightweight analytics script from the Minimal Analytics project to suit custom requirements.
-
GA4 Proxy:
- Proxies data to the GA4 collection endpoint, ensuring the
MEASUREMENT_ID
matches the one configured in the environment.
- Proxies data to the GA4 collection endpoint, ensuring the
-
Custom Analytics Script:
- Fetches the analytics script from the Minimal Analytics project.
- Modifies the script to replace the default GA4 endpoint with the worker’s proxy endpoint.
- Adds a fallback mechanism for analytics data collection.
-
CORS Support:
- Handles cross-origin requests, including preflight OPTIONS requests.
-
Flexible Data Handling:
- Supports GET and POST requests for data collection.
- Provides fallback methods for sending analytics data.
-
Image Pixel Response:
- Returns a transparent 1x1 image for successful GET requests, emulating traditional pixel tracking.
This project integrates the analytics script from the Minimal Analytics project by Jack Hill, which is licensed under the MIT License. Modifications include:
- Replacing the default Google Analytics endpoint with a custom proxy endpoint.
- Adding fallback logic for alternative methods of sending analytics data.
-
Setup Cloudflare Workers:
- Deploy the provided script using Cloudflare‘s dashboard or the Wrangler CLI.
-
Environment Variables:
- Configure the following variable in your worker environment:
MEASUREMENT_ID
: Your Google Analytics 4 measurement ID.
- Configure the following variable in your worker environment:
- Send GET or POST requests to the worker’s URL with query parameters required by GA4. The worker will forward the data to the GA4 endpoint.
- Append the
?fallback=true
query parameter to the worker‘s URL to fetch the modified analytics script.
curl -X POST https://<worker-url> \
-H "Content-Type: application/json" \
-d '{"tid":"G-XXXXXXX", "cid":"123456", "t":"event", "en":"test_event"}'
curl https://<worker-url>?fallback=true
This project is licensed under the MIT License.
It integrates and modifies the analytics script from the Minimal Analytics project by Jack Hill, which is also licensed under the MIT License.