Skip to content

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:

  1. Network — the container itself (GTM's gtm.js is ~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.
  2. Main thread — vendor code runs unthrottled. Long Animation Frames attribution (the INP lesson) shows sourceURL per blocking script; the first time you check, expect the chat widget and the A/B tester to be near the top.
  3. Ordering — a synchronous third-party script in the <head> makes your entire first paint hostage to the vendor's SLA. If ab-testing-cdn.com has 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 pointerover intent). 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 offsetHeight constantly 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.gstatic copy 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 — gtag consent 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. facade it 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?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.