Google Publisher Tag samples

This section contains a collection of examples and related resources for the Google Publisher Tag (GPT) library.

Much of the content in this section can also be found in our GitHub repository. Sample code and associated documentation is released as open source under the Apache License 2.0.

Basic samples

Ad sizes Specify which ad sizes are eligible to serve to an ad slot.
Configure personalized ads Customize ad personalization settings for GPT ad requests.
Configure privacy settings Customize privacy settings for GPT ad requests.
Display a limited ad Display an ad in the absence of consent for the use of cookies or other local identifiers.
Display a test ad Display a fixed-sized test ad.
Display a web interstitial ad Display a GPT-managed web interstitial ad.
Display an anchor ad Display a GPT-managed anchor ad.
Display an out-of-page ad Display a pop-up, pop-under, or floating ad.
Key-value targeting Use key-value targeting to control the ads eligible to serve to specific ad slots.
Reserve space for ads Minimize layout shift by reserving space for ads.

Advanced samples

Ad event listeners Monitor and report on ad events fired by GPT.
Collapse empty ad slots Automatically show/hide ad slots when certain criteria are met.
Control SRA batching Precisely control which ad slots are requested when in Single Request Architecture (SRA) mode.
Display a rewarded ad Display a GPT-managed rewarded ad.
Event-based ad requests Request GPT ads based on events.
Lazy loading Defer the requesting and rendering of ads.
Refresh ad slots Use GPT to dynamically reload ads.
Tag pages with infinite content Use GPT to dynamically generate new ad content.