Lesson 12 of 14 · 20m
Third-Party Scripts: Other People's Main Thread
Every tag is a deployment pipeline you don't control — the three bills they charge, facades and worker offload, and the governance that contains them.
Every third-party script on your page is a deployment pipeline somebody else controls. Marketing drops a tag into the manager, a vendor pushes an update to cdn.vendor.com/v2.js, and your carefully budgeted bundle numbers go out the window — none of it passes through your bundler, your code review, or your CI. The Web Almanac has measured for years that third parties account for roughly a third of requests on a typical page and a disproportionate share of main-thread blocking time. Your size-limit gate cannot see any of it.
The wrong mental model is "it's just one little snippet." Each snippet is a supply of JavaScript executing on your main thread, holding your event loop, and competing with your own code for the network.
What a tag actually costs
Three separate bills:
- Network — the container itself (GTM's
gtm.jsis ~100 KB compressed before a single tag fires), then every vendor script the container injects, each on its own origin with its own DNS/TCP/TLS. - Main thread — vendor code runs unthrottled. Long Animation Frames attribution (the INP lesson) shows
sourceURLper blocking script; the first time you check, expect the chat widget and the A/B tester to be near the top. - Ordering — a synchronous third-party script in the
<head>makes your entire first paint hostage to the vendor's SLA. Ifab-testing-cdn.comhas a bad day, your users get a white page on your behalf. This is the actual mechanism behind the classic "site was down but our servers were fine" incident.
The worst offender is the synchronous A/B test snippet with anti-flicker: it deliberately blocks rendering until it decides which variant to show. Vendors sell it as necessary. What it does is move your LCP to whenever their CDN feels like answering.
Reduce cost without declaring war on marketing
<!-- never this -->
<script src="https://cdn.vendor.com/widget.js"></script>
<!-- async: arrival order doesn't matter -->
<script async src="https://cdn.vendor.com/widget.js"></script>
<!-- better: delay until the page is usable -->
<script>
addEventListener("load", () => setTimeout(() => {
const s = document.createElement("script");
s.src = "https://cdn.vendor.com/widget.js";
document.head.appendChild(s);
}, 2500));
</script>- Facade pattern. The chat widget nobody opens doesn't deserve 400 KB at boot. Render a real-looking button; load the actual widget on first interaction (click, or
pointeroverintent). Cal/Intercom-style "import on click" wrappers cut the boot cost to near zero. - Move it off the main thread. Partytown (Builder.io) runs third-party scripts inside a Web Worker with a synchronous-proxy trick so DOM-touching vendor code still works. Analytics and tag-manager style scripts are the designed-for case; a script that reads
offsetHeightconstantly is not. - Server-side tagging. GTM's server container and vendor server-side APIs move the network cost off the client entirely — the browser makes one first-party call and your server fans out. It also shrinks the JS payload and improves data control.
- Self-host what you can. A first-party
fonts.gstaticcopy or a vendored small library removes an entire origin's connection cost and a third party's availability from your critical path.
Governance is the real fix
Keep an inventory: every third-party origin on the page, its owner, its business justification, and its measured cost. LoAF attribution gives you the blocking-time share per sourceURL; put that number next to the script's name in the doc. Review quarterly — vendor scripts have a half-life, and the one nobody remembers adding is still running.
Then give the tag manager a budget like any other dependency: a cap on third-party script count and blocking time, enforced by the Lighthouse CI resource-summary:third-party:count assertion from the guardrails lesson plus a RUM alert on LoAF-attributed third-party time. "Remove one to add one" is a conversation marketing can have; "the page got slow" is not actionable.
Field notes
- Consent mode still runs JavaScript —
gtagconsent v2 loads and batches without firing tags. The code cost exists even when the tracking doesn't. - Audit embedded content too: a YouTube
<iframe>is third-party cost wearing a different tag.facadeit with the poster thumbnail; load the real iframe on click. - If a vendor demands a synchronous head script, that demand is itself data about the vendor's engineering. Push back once; escalate twice.
- Your CSP is also a performance tool: a strict script-src list doubles as the inventory of what's actually allowed to execute.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.
Inside TypeScript 7: What a Year-Long Rewrite in Go Actually Bought Developers
A deep look at how Microsoft's Go-based TypeScript 7 compiler achieves its speed gains, and what the Visual Studio Code team's year-long migration reveals about adopting it safely.
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.