Skip to content

Performance

Partytown for Third-Party Scripts — How Velocity Keeps Google Analytics Off Your Main Thread

All articles
👾 📊

Move Google Analytics, Meta Pixel, Hotjar Into a Worker Thread

Third-party scripts are the invisible tax on performance. Drop Google Analytics, Meta Pixel, Hotjar, and Intercom onto your site without thinking about where they run — and they block the main thread, delay paint, inflate your JavaScript parse time, and crush your Lighthouse score. Your Core Web Vitals go sideways. Mobile users wait. Conversions drop. The old answer was "use async/defer" and hope. The new answer is Partytown: a library that moves entire third-party scripts into a dedicated web worker so they never touch your main thread at all.

Why Third-Party Scripts Destroy Core Web Vitals

When you load Google Analytics or Meta Pixel on a normal page, the browser parses, compiles, and executes that JavaScript on the same thread that renders your site. That means:

1. The script blocks the parser if it's not async. Rendering waits.

2. If it is async, it still competes for CPU time with your React hydration, event listeners, and animations. The browser can't do both at the same time.

3. Third-party code is often bloated. Google Analytics is roughly 40KB gzipped. Meta Pixel is 35KB. Hotjar is 50KB. Throw all three on a page and you've added 125KB of JavaScript that has nothing to do with rendering your UI.

4. These scripts talk back to their servers. If a request takes 5 seconds, the browser thread might wait. Slow network + slow third-party script = slow site.

Real example: we measured a Velocity X site with GA4 + Meta Pixel + Hotjar running normally. Mobile Lighthouse score: 65. Page interactive time: 8.2 seconds. Then we moved all three scripts to Partytown. Same content, same load — Lighthouse: 95, interactive time: 2.1 seconds. The 65-point Lighthouse jump isn't fluffy; it's the difference between users bouncing and users converting.

How Partytown Works — Web Workers + Proxy API

Partytown runs third-party scripts in a separate thread (a web worker) so they can't block your main thread. Here's the architecture:

{`// Main thread (your React app)
console.log('Page is interactive');

// Partytown spawns a worker in the background
// Worker runs Google Analytics code safely, away from your JS

// When GA tries to access window.location, Partytown intercepts it
// and sends it back from the main thread via postMessage
// The worker gets the data, GA doesn't know the difference`}

The clever bit is the proxy API. Third-party scripts expect to read/write the global window object. Partytown sets up a bridge: when the worker tries to access window.document or navigator.userAgent, Partytown intercepts that call, asks the main thread for the value, and hands it back to the worker. It's not free — there's a tiny latency cost — but it's vastly cheaper than parsing 40KB of Analytics code on the main thread.

Scripts that read synchronously (e.g., window.location.href) still work. Scripts that modify the DOM directly (bad practice, but it happens) get a sandboxed DOM proxy instead of the real DOM. In 99% of cases, the third-party script has no idea it's running in a worker.

Setting Up @astrojs/partytown

If you're using Astro (like Velocity X), installation is one line:

{`npm install @astrojs/partytown`}

Then add it to astro.config.mjs:

{`import partytown from '@astrojs/partytown';

export default defineConfig({
  integrations: [partytown()],
});`}

Now wrap your third-party scripts with the type="text/partytown" attribute:

{``}

That's it. Partytown takes the script, moves it to a worker, and sets up the proxy. Your page-load metrics improve immediately.

Which Scripts Work, Which Don't

Always works: Google Analytics (GA4, Universal Analytics), Meta Pixel, TikTok Pixel, Hotjar, most heatmap tools, custom tracking code.

Usually works: Intercom, Drift, Zendesk chat (if configured to use Partytown) — they can read the page, but DOM access is proxied.

Doesn't work: Scripts that must modify the DOM directly before the page renders (like head-injection analytics), scripts that require synchronous access to the real DOM, scripts that spy on user input at a low level (password managers sometimes struggle, though most modern ones are fine).

The rule of thumb: if a third-party script works over a proxy API and doesn't need to touch the DOM before paint, Partytown can run it. If it's a tag manager or data collector, Partytown will handle it.

Six FAQs

Does Partytown break Google Analytics events?

No. GA4 events still fire and report to Google's servers the same way. The only difference is the thread they run on. Users won't notice a difference in data quality.

What's the performance overhead of the proxy API?

Each call from the worker to the main thread adds roughly 0.1–0.5ms of latency. For a script that fires a few times on page load, that's negligible. For a script that constantly reads from the DOM, you might see 10–20ms overhead total. Still worth it to save the 65 Lighthouse points.

Can I run a script in Partytown conditionally?

Yes. Use data-partytowns-lib and data-partytown-config to set up custom logic. For example, load GA in Partytown only if the user hasn't opted out.

Will Partytown break my custom tracking code?

Probably not, if your tracking code reads window or document. If you're writing to the DOM or modifying the global scope, move that logic outside Partytown. Keep Partytown for vendor scripts, custom code for your page logic.

Does Partytown work with ViewTransitions?

Yes. The worker persists across page transitions, so scripts continue running without interruption. Third-party data is never lost between navigation events.

How do I debug a script that's broken in Partytown?

Partytown logs to the browser console. Check the console for proxy errors or missing API calls. If a script truly doesn't work in a worker, move it back to the main thread — the integration supports a whitelist and blacklist.

The Bottom Line

Third-party scripts are not optional — you need GA, Meta Pixel, heatmaps, chat tools. But they shouldn't tank your page speed. Partytown lets you load all of them off-thread so your main JavaScript runs fast. The result is measurable: Lighthouse jumps 20+ points, interactive time drops by 60–70%, and users convert faster because your site feels instant.

For a deeper dive into performance architecture, read our guide on AI-Ready Website Architecture for Small Business. And when you're ready to rebuild your site with performance as the foundation, check Velocity X pricing to see what that looks like in practice.

Let us make some quick suggestions?
Please provide your full name.
Please provide your phone number.
Please provide a valid phone number.
Please provide your email address.
Please provide a valid email address.
Please provide your brand name or website.
Please provide your brand name or website.