Partytown + Google Tag Manager in Hydrogen
This recipe integrates Partytown with your Hydrogen storefront to run Google Tag Manager and other third-party scripts in a web worker, keeping the main thread free for critical rendering tasks.
Key features:
- Moves GTM and analytics scripts off the main thread
- Improves Core Web Vitals scores
- Maintains full GTM functionality
- Includes CORS reverse proxy for third-party scripts
- CSP headers configured for GTM domains
TypeScript users need to manually add GTM types to env.d.ts.
TypeScript users need to manually add GTM types to env.d.ts.
Anchor to RequirementsRequirements
- Google Tag Manager container ID (remember to set your
GTM_CONTAINER_IDorGTM_IDenvironment variable) - Basic understanding of web workers and CSP
- Node.js 18.0.0 or higher
Anchor to IngredientsIngredients
New files added to the template by this recipe.
| File | Description |
|---|---|
| app/components/PartytownGoogleTagManager.tsx | Component that loads GTM scripts in a web worker via Partytown |
| app/routes/reverse-proxy.ts | Reverse proxy route for third-party scripts requiring CORS headers |
| app/utils/partytown/maybeProxyRequest.ts | URL resolver to control which scripts should be reverse-proxied |
| app/utils/partytown/partytownAtomicHeaders.ts | Helper utility to enable Partytown atomic mode for better performance |
Anchor to Step 1: Ignore Partytown library filesStep 1: Ignore Partytown library files
Add public/~partytown to ignore Partytown library files.
Anchor to Step 2: Create GTM web worker componentStep 2: Create GTM web worker component
Add a GTM component that loads scripts in a web worker.
File
Anchor to Step 3: Document Partytown setupStep 3: Document Partytown setup
Document Partytown setup and configuration instructions.
File
Anchor to Step 4: Add CORS reverse proxyStep 4: Add CORS reverse proxy
Reverse the proxy route for third-party scripts requiring CORS headers.
File
Anchor to Step 5: Configure CSP headersStep 5: Configure CSP headers
Configure the CSP headers for GTM and Google Analytics domains.
Anchor to Step 6: Add URL resolver for proxyingStep 6: Add URL resolver for proxying
Add a URL resolver to control which scripts should be reverse-proxied.
File
Anchor to Step 7: Initialize Partytown and GTMStep 7: Initialize Partytown and GTM
Initialize Partytown and GTM in the root layout.
File
Anchor to Step 8: Enable atomic modeStep 8: Enable atomic mode
Add a helper utility to enable Partytown atomic mode for better performance.
File
Anchor to Step 9: Install PartytownStep 9: Install Partytown
Add a Partytown dependency and npm script for copying library files.
Anchor to Step 10: Configure Vite for PartytownStep 10: Configure Vite for Partytown
Configure Vite to exclude Partytown library from build optimization.
Anchor to Next stepsNext steps
After applying this recipe:
- Install dependencies:
- Copy Partytown library files:
- Add your GTM container ID to
.env:
- For TypeScript projects, update
env.d.ts:
- Test your implementation:
Visit your site and check the Network tab to verify GTM is loading via the web worker.