Google Publisher Tag code sample

The following is a sample of the full code for creating a Google Publisher Tag (GPT) for desktop and mobile implementations. Use the Google Tag Generator to generate  tag automatically.

Developers and those in need of advanced implementations of GPT, refer to the API reference and sample implementations (such as lazy loading).

View advanced GPT samples

Google Publisher Tag example code

There are two sections of code implemented for GPT:

  • Configuration of GPT goes in the webpage's  .
  • Calls for each specific ad slot goes in the relevant section of the .

This sample is for informational purposes, and we recommend having a developer implement the code on your website.

GPT configuration

The following code example, includes calling the GPT JavaScript library, defining the ad slots, key-value targeting, and more.

1
2
3
4

Lines 3-6: asynchronously loads the GPT library used by Ad Manager, using SSL/HTTPS. This is where the command queue is built, which handles the list of functions (generally, ad calls) to be handled asynchronously. You don't need to edit this section of code.

7
21
22
23   

Line 23 (optional): style="width: 728px; height: 90px" is the initial size set on the div reserving space prior to the creative being rendered.

If using multi-size tags, we recommend either omitting this (in that case, the size of the element takes the size of the selected creative when rendered) or making both dimensions large enough to contain the largest eligible creative. For single size ad tags, use this parameter to expand the container element until the creative loads so that other page elements don't shift when the creative renders.

24     
29   
30     
31     
36   
37
38

View advanced GPT samples

If you can't edit the header of your web pages

You can use GPT without modifying your website's

.

Option 1: inline GPT

Use an inline tag to define where the ad unit appears on the page, rather than using the page header. With an inline tag, the entire GPT ad slot definition and request, including loading the GPT library, is contained within a single

Sample inline ad tag

Option 2: put all code in the web page body

Use conventional GPT implementation, but put the ad slot definitions in the body of your HTML, rather than the header.

The code that loads the library and defines the ad slots must be called before you request ads for those slots. Because the code isn't segmented into the head and body of your page and you must manage its sequence, this approach is more complex, but provides the flexibility of SRA.

Create a Tagless Request without JavaScript

A Tagless Request can be used in the place of an ad tag to request the raw creative code trafficked in Ad Manager. Tagless Requests are generally used when custom parsing and display is needed, such as set-top boxes or other environments without Google tagging or SDK support.

Was this helpful?

How can we improve it?
Search
Clear search
Close search
Google apps
Main menu
10470465725923618996
true
Search Help Center
false
true
true
true
true
true
148
false
false
false
false
false