Improve Core Web Vitals with Third-Party Scripts
In this article
Third-party scripts and ads are among the most common causes of poor Core Web Vitals (CWV) scores. They block rendering, consume CPU, and shift layout. This guide shows you how to reduce their impact while keeping the functionality you need. You’ll learn practical techniques like deferring, lazy-loading, and using placeholders, plus how to measure the results.
What You'll Need
- Access to your website’s HTML, CSS, and JavaScript files (via a CMS, code editor, or hosting file manager).
- A Google account for PageSpeed Insights (free) or access to your browser’s DevTools (Chrome, Edge, Firefox).
- Basic familiarity with HTML tags and JavaScript. No deep coding required.
- If you use a tag manager like Google Tag Manager, you’ll need edit access to it.
Step-by-Step
- Measure your current Core Web Vitals. Go to PageSpeed Insights, enter your URL, and run a test. Note your LCP, INP (formerly FID), and CLS scores for both mobile and desktop. Also run a lab test in Chrome DevTools (Lighthouse) to see which third-party scripts are flagged.
- Identify the third-party scripts on your page. Open Chrome DevTools, go to the Network tab, and reload the page. Look for requests to domains like googletagmanager.com, doubleclick.net, cdn.jsdelivr.net, or any vendor you’ve added (chat widgets, analytics, pixels). Sort by “Time” to see which ones block rendering or take longest.
- Add
asyncordeferto non-critical scripts. If a script doesn’t need to run before the page is interactive, adddeferto its<script>tag. If it doesn’t need to run in order, useasync. This prevents the browser from waiting for these scripts before rendering the page. For example:<script src="analytics.js" defer></script>. - Move scripts to the footer. For scripts that must run but aren’t needed for above-the-fold content, move the
<script>tag from the<head>to just before the closing</body>tag. This ensures the HTML is parsed and rendered first. - Lazy-load third-party embeds. For iframes (YouTube, maps, social posts) use the
loading="lazy"attribute. For JavaScript-heavy widgets, implement a “click-to-load” pattern: show a static placeholder (like a thumbnail) and load the script only when the user clicks it. This can dramatically improve LCP and INP. - Use a tag manager with consent-based loading. If you use Google Tag Manager, load tags only after user consent (if required) and use built-in triggers like “DOM Ready” or “Page View” rather than “All Pages” for non-essential tags. This reduces the number of scripts that run on initial load.
- Set explicit dimensions for ad slots and embeds. To prevent CLS, always reserve space for ads and iframes. Use CSS to set width and height (or aspect-ratio) on the container. For responsive ads, use a fixed height that matches the ad size (e.g., 300x250) or use the
min-heightproperty. - Use a Content Delivery Network (CDN) with HTTP/2 or HTTP/3. A CDN can serve third-party scripts from servers closer to the user, reducing latency. Many ad networks and analytics providers already use CDNs, but ensure your own scripts are also served via a CDN.
- Combine and minify scripts where possible. If you have multiple small third-party scripts, consider combining them into one file (if they are from the same provider) or minifying them. This reduces the number of requests and bytes.
- Implement a Content Security Policy (CSP) to block unwanted scripts. A strict CSP can block malicious or unnecessary third-party scripts, but be careful: it can break legitimate ones. Use it to allow only specific domains.
- Re-test after each change. After implementing a change, run PageSpeed Insights again. Compare the metrics. If a change doesn’t help, revert it. Keep a log of what you changed and the impact.
Common Mistakes
- Using
deferon scripts that must run before the DOM is parsed. Some scripts (like polyfills) need to execute early. Deferring them can break functionality. - Lazy-loading above-the-fold content. If the main hero image or a critical ad is lazy-loaded, it can hurt LCP. Only lazy-load below-the-fold elements.
- Not reserving space for ads. Even with lazy-loading, if the ad container has no height, the page will shift when the ad loads. Always set a min-height.
- Adding too many scripts at once. Each third-party script adds overhead. Audit and remove any that are not essential.
- Ignoring mobile performance. Mobile networks are slower, and CWV is mobile-first. Test on a real device or use throttling in DevTools.
Tips
- Prioritize LCP. Make sure your largest content element (often an image or text) loads fast. Preload critical images with
<link rel="preload">. - Use the Performance panel in DevTools. Record a page load and look for long tasks (over 50ms) caused by third-party scripts. This helps you identify which script to optimize first.
- Consider using a lightweight analytics alternative. If GA4 is heavy, try a lighter solution or load it only on certain pages.
- Use the “Save Data” and “Lite” modes. Some browsers have data-saver modes that can block heavy scripts. Test with these enabled.
- Work with your ad provider. If ads are the main issue, ask your ad network about “lazy-load” or “delay” options they offer.
Troubleshooting
If your LCP doesn’t improve: The LCP element might be a hero image that’s still being delayed by a script. Check if the image is preloaded. Also, ensure that the server response time is under 200ms (use a CDN or caching).
If your INP is still high: Some third-party scripts run long tasks on the main thread. Use the Performance panel to see which script is causing it. If it’s an ad script, try to delay it until after the page is interactive (e.g., using requestIdleCallback).
If CLS persists: Even with dimensions set, some ads resize. Use the min-height property to reserve space, and consider using a CSS aspect-ratio. Also, ensure that fonts are loaded with font-display: swap to avoid layout shift.
If a script breaks after deferring: Revert the change and instead use a “DOMContentLoaded” event listener inside the script to delay execution. Alternatively, load the script asynchronously but with a fallback.
If PageSpeed Insights scores don’t match field data: Lab tests are not always accurate. Use the CrUX report in PageSpeed Insights to see real user data. Focus on field data for final assessment.
Frequently Asked Questions
Will deferring all third-party scripts break my site?
Not necessarily. Only defer scripts that don’t need to run before the DOM is parsed. Test each script after deferring to ensure functionality remains.
What is the best way to lazy-load ads?
Use a placeholder with a fixed height (e.g., 300x250) and load the ad script only when the placeholder is scrolled into view. Many ad networks offer this as a built-in option.
How do I know which third-party scripts are critical?
Ask: does this script affect the user’s ability to read or interact with the page? If not, it’s non-critical. Examples of non-critical: analytics, chat widgets, social share buttons.
Can I use a Content Security Policy to block ads?
Yes, but it’s risky. Ads often load from multiple domains, and blocking them can break ad revenue. Use CSP to block only known malicious domains, not all ads.
How long does it take to see improvements in CWV?
Lab tests (PageSpeed Insights) update immediately. Field data (CrUX) takes 28 days to reflect changes. So you may see lab improvements right away, but field data will lag.
Do I need to remove all third-party scripts to get a perfect score?
No. It’s about reducing their impact. Even with ads, you can achieve good CWV by deferring, lazy-loading, and reserving space. A perfect score is not necessary; aim for “good” thresholds.