lightbulb_outline Blog Post: Fast ads matter (web.dev)

Load ad scripts statically

Overview

This audits checks whether or not ad scripts are being injected into the page. Other resources on the page may delay the fetching and loading of injected scripts, which will in turn delay the loading of ads. In some situations these scripts may not be fetched at all, preventing ads from being loaded all together.

Recommendations

Load scripts via async script tags to improve speed. The browser preload scanner can fetch script tags earlier, even if render blocking resources are blocking the script execution.

Incorrect
<script>
  var el = document.createElement('script');
  el.src = 'https://securepubads.g.doubleclick.net/tag/js/gpt.js';
  var node = document.getElementsByTagName('script')[0];
  node.parentNode.insertBefore(el, node);
</script>
Correct
<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>

More information

This audit operates against a whitelist of ad scripts which are known to be safe to load statically. The current list is:

Library Script(s)
AdSense pagead2.googlesyndication.com/pagead/js/adsbygoogle.js
Amazon Publisher Services amazon-adsystem.com/aax2/apstag.js
Criteo Direct Bidder static.criteo.net/js/*/publishertag.js
Google Publisher Tag

googletagservices.com/tag/js/gpt.js

securepubads.g.doubleclick.net/tag/js/gpt.js

Index Exchange js-sec.indexww.com/ht/p/*.js

Intervening against document.write()
Script-injected "async scripts" considered harmful