The following guide expands on displaying a test ad, and introduces more basic concepts to make the most of the Google Publisher Tag (GPT) library. These concepts include:
- Ad sizing
- Key-value targeting
- Single request architecture
Load the GPT library
Start by loading and initializing the GPT library. Add the
following to the of the HTML document:
This loads the GPT library and initializes both the googletag
and CommandArray
objects. Initializing these objects lets you start
using the GPT command array immediately. Add the
JavaScript code that follows to the body of the empty function defined in this
snippet.
Define ad slots
After loading GPT, you can define ad slots. The following example defines three ad slots with different ad formats, sizing, and targeting options.
Fixed-size ad slot
Here's a simple, fixed-size ad slot:
// Define a fixed size ad slot, customized with key-value targeting. googletag .defineSlot("/6355419/Travel/Asia", [728, 90], "banner-ad") .addService(googletag.pubads()) .setTargeting("color", "red") .setTargeting("position", "atf");
In addition to the ad unit path, size, and container Here's an anchor ad slot, attached to the bottom of the viewport: Anchor slots are a type of out-of-page format, which are defined
using the Out-of-page formats often have restrictions on the sorts of pages and
devices they're eligible to serve to. Due to these restrictions, you must
verify that the slot is successfully defined before interacting with it. See
the Display an anchor ad sample for details. Here's a fluid ad slot for a native ad: Fluid ad slots don't have a fixed size. Fluid ad slots adjust to fit the
creative content from the ad. You define fluid ad slots with the Certain GPT configuration options apply globally, rather than
to specific ad slots. These are referred to as page-level settings. Here's an example of how to configure page-level settings: This snippet does three things: Finally, add the following snippet to the This defines two ad slot containers: After all ad slots are defined, a call to display the The following is the full source code for the sample page this guide is based
on. You can also view an interactive demo of this page. Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Last updated 2025-02-03 UTC.Anchor ad slot
// Define an anchor ad slot that sticks to the bottom of the viewport.
const anchorSlot = googletag.defineOutOfPageSlot(
"/6355419/Travel",
googletag.enums.OutOfPageFormat.BOTTOM_ANCHOR,
);
// The slot will be null if the page or device does not support anchors.
if (anchorSlot) {
anchorSlot.setTargeting("test", "anchor").addService(googletag.pubads());
document.getElementById("status").textContent =
"Anchor ad is initialized. Scroll page to activate.";
}
defineOutOfPageSlot
method, rather than the usual
defineSlot
method. Learn more about out-of-page creatives.Fluid ad slot
// Define a fluid ad slot that fills the width of the enclosing column and
// adjusts the height as defined by the native creative delivered.
googletag
.defineSlot("/6355419/Travel", ["fluid"], "native-ad")
.addService(googletag.pubads());
fluid
size
option. Learn more about
ad sizing and the available sizing options.Configure page-level settings
// Configure page-level targeting.
googletag.pubads().setTargeting("interests", "basketball");
// Enable SRA and services.
googletag.pubads().enableSingleRequest();
googletag.enableServices();
Display ads
of the page:
Anchor ads are not supported on this page.
banner-ad
and native-ad
. These
container id
values correspond to the values you provided when you defined the
ad slots earlier in this example.banner-ad
is made. Since
SRA is enabled, this single display call requests and renders all ad slots
defined up to this point. If necessary, you can more precisely
control ad loading and refresh and
batching behavior with SRA enabled.Complete example
JavaScript
TypeScript