General Best Practices

Integrating the Google Publisher Tag (GPT) library into your website is in many ways the same as integrating any other third-party script. However, there are some unique aspects of working with GPT that must be considered to ensure you make the most of your ad space with minimal impact to site performance. After all, fast ads matter.

The following sections cover the best practices to use when working on your integration.

Load GPT from an official source

Always request the GPT library from an official source:

Integration type Script tag
Standard

Including publishers integrated with the IAB Europe TCF.
<script src="https://securepubads.g.doubleclick.net/tag/js/gpt.js" crossorigin="anonymous" async></script>
Limited ads

For publishers who want to manually enable limited ads.
<script src="https://pagead2.googlesyndication.com/tag/js/gpt.js" async></script>

Only use the limited ads URL on sites where you intend to manually enable limited ads. Requesting GPT from the limited ads URL when this feature is not in use causes the browser to open an additional, unnecessary network connection, which may slow down ad loading.

To aid in publisher regulatory compliance needs, GPT may be loaded from the standard URL with the crossorigin="anonymous" attribute to prevent cookies from being sent with the script request.

Don't serve cached versions of GPT

Never serve versions of gpt.js, pubads_impl.js, or any libraries they load from your own server or load them from an unofficial source.

Using a locally cached version of the GPT library may not always work in newer versions of browsers or operating systems and may not be updated with new features and improvements. Earlier versions of the GPT library may stop working at any time and may lead to loss of ad revenue if ads stop serving to your tags.

This also applies to service workers. If you use service workers to cache or modify responses to HTTP requests coming from your site, use a network-only strategy when handling requests for gpt.js in order to fetch an up-to-date version.

Load GPT early

The GPT library (gpt.js) contains only a small fraction of the code necessary to load ads. The majority of that code is contained in separate files (such as pubads_impl_XX.js), which are loaded by gpt.js as needed.

By loading the core GPT library early, these dependent scripts are also able to be loaded earlier. This avoids any extra latency related to fetching these scripts (if uncached) and allows for ads to be loaded more quickly. All of this contributes to lowering the time to render first ad metric, which in turn increases the viewability of your ads.

Use preload when appropriate

Sometimes you may not be in direct control of when or how GPT is loaded. For example, when using a third-party ads script which loads GPT on your behalf. In these cases, it may be appropriate to preload the request for gpt.js. Preloading a request instructs the browser to immediately download a specified asset that is critical to the loading of your page. When the specified asset is a JavaScript file, the script contents are fetched immediately but execution is deferred until the script is needed.

Example preload request for standard integrations

<link rel="preload" href="https://securepubads.g.doubleclick.net/tag/js/gpt.js" as="script">

Example preload request for limited ads integrations

<link rel="preload" href="https://pagead2.googlesyndication.com/tag/js/gpt.js" as="script">

Use GPT on prerendered pages

Prerendering instructs the browser to preemptively download and render pages that users have yet to request, but are likely to visit. When loaded on a prerendered page, the GPT library only requests ads if and when the page is made visible.

Load GPT statically

Avoid injecting the GPT library into your page dynamically or loading it from an external script. Instead, load the library statically in the <head> of your page as illustrated in Get Started with Google Publisher Tags. This prevents other resources from delaying the fetching and loading of the GPT library, which in turn would delay the loading of ads.

For more details, see the Publisher Ads Audits for Lighthouse load ad scripts statically audit documentation.

Load GPT asynchronously

Include the async keyword in your script tag definition, as illustrated in Get Started with Google Publisher Tags. This instructs the browser to load the GPT library in parallel with other resources and page content, rather than blocking execution until the script is done loading.

For more details, see the Publisher Ads Audits for Lighthouse load ad tag asynchronously audit documentation.

Load GPT securely

Always load the GPT library over HTTPS, as illustrated in Get Started with Google Publisher Tags. This not only provides better security for your users, it also improves performance. Since ad requests issued by GPT always use HTTPS, loading the library itself using HTTPS ensures that the browser only needs to open 1 connection for all requests related to ad serving.

For more details, see the Publisher Ads Audits for Lighthouse load ad tag over HTTPS and load GPT from recommended host audit documentation.

Practice good page performance

While the best practices in this guide focus specifically on optimizing your GPT integration, many other factors contribute to the overall performance of your page. When making changes to your site (especially those based on broad recommendations, such as the ones in this guide), it's important to evaluate the impact of those changes on all aspects of your page's performance. It's recommended that you regularly run tools like Lighthouse and Publisher Ads Audits for Lighthouse to identify and address performance issues, and find the right balance of optimizations for your site.