GA4 network request duplicator (browser inline script) - TypeScript source + minified dist build.
Intercepts Google Analytics 4 (GA4) collect requests and duplicates them to a D8A server endpoint.
This library provides browser-based network interception to duplicate GA4 analytics calls. It intercepts requests made to Google Analytics and forwards them to a configured D8A server endpoint, enabling parallel data collection.
Supported interception methods:
- Fetch API requests
- XMLHttpRequest calls
- navigator.sendBeacon calls
- Script tag loads
Include the built script in your HTML and initialize the duplicator:
``html`
- server_container_url: Default D8A server endpoint URL.Can be overridden for each destination.destinations
- : Array of destination objects with measurement_id and server_container_url (default: []).debug
- : Enable debug logging (default: false)
`javascript`
window.createGA4Duplicator({
server_container_url: "https://default-endpoint.com",
destinations: [
{
measurement_id: "G-ABC123",
server_container_url: "https://endpoint1.com",
},
{
measurement_id: "G-XYZ789",
server_container_url: "https://endpoint2.com"
}
]
});
bash
npm run build:prod
`
Output: dist/gd.min.js$3
`bash
npm run build:dev
`
Output: dist/gd.jsSource Code Hashing
The build process generates a SHA256 hash of all TypeScript source files in the
src/ directory and saves it to src.hash. This ensures build integrity and enables CI verification.$3
`bash
npm run hash
`This command compares the stored hash with the current source code hash:
- Exit code 0: Source code matches the hash (build is up-to-date)
- Exit code 1: Source code has changed since last build
Use this in CI pipelines to ensure
dist/ files are regenerated when source code changes.Test
$3
`bash
npm test
`$3
1. Install Playwright browsers (first time only):
`bash
npx playwright install chromium
`2. Run e2e tests:
`bash
npm run test:e2e
`The e2e tests start a local HTTP server and use Playwright to verify that GA4 requests are properly duplicated to the D8A endpoint.
$3
Open
test.html` in a browser to manually test different network interception methods. The page includes buttons to trigger various types of GA4 requests and displays logs of duplication activity.