Example of an asynchronous Google Publisher Tag
Below is a sample of the full code you would use to create an asynchronous Google Publisher Tag. This tag illustrates many of the common features and syntax used in an asynchronous GPT ad request.
For more information about how to generate Google Publisher Tags, read our article on generating tags within DFP.
1 |
<html> |
|
2 |
<head> |
|
3 |
<script type="text/javascript"> |
|
4 |
var googletag = googletag || {}; |
|
5 |
googletag.cmd = googletag.cmd || []; |
|
6 |
(function() { |
|
7 |
var gads = document.createElement("script"); |
|
8 |
gads.async = true; |
|
9 |
gads.type = "text/javascript"; |
|
10 |
var useSSL = "https:" == document.location.protocol; |
|
11 |
gads.src = (useSSL ? "https:" : "http:") + "//www.googletagservices.com/tag/js/gpt.js"; |
|
12 |
var node =document.getElementsByTagName("script")[0]; |
|
13 |
node.parentNode.insertBefore(gads, node); |
|
14 |
})(); |
|
15 |
</script> |
|
16 |
<script type="text/javascript"> |
|
17 |
googletag.cmd.push(function() { |
|
18 |
googletag.pubads().set("adsense_background_color", "FFFFFF"); |
|
19 |
}); |
|
20 |
</script> |
|
21 |
<script> |
|
22 |
googletag.cmd.push(function() { |
|
23 |
googletag.defineSlot("/1234/travel/asia/food", [728, 90], "div-gpt-ad-123456789-0") |
|
24 |
.addService(googletag.pubads()) |
|
25 |
.setTargeting("interests", ["sports", "music", "movies"]); |
|
26 |
googletag.defineSlot("/1234/travel/asia/food", [[468, 60], [728, 90], [300, 250]], "div-gpt-ad-123456789-1") |
|
27 |
.addService(googletag.pubads()) |
|
28 |
.setTargeting("gender", "male") |
|
29 |
.setTargeting("age", "20-30"); |
|
30 |
googletag.pubads().setTargeting("topic","basketball"); |
|
31 |
googletag.pubads().enableSingleRequest(); |
|
32 |
googletag.enableServices(); |
|
33 |
}); |
|
34 |
</script> |
|
35 |
</head> |
|
36 |
<body> |
|
37 |
<div id="div-gpt-ad-123456789-0" style="width: 728px; height: 90px"> |
|
38 |
<script type="text/javascript"> |
|
39 |
googletag.cmd.push(function() { |
|
40 |
googletag.display("div-gpt-ad-123456789-0"); |
|
41 |
}); |
|
42 |
</script> |
|
43 |
</div> |
|
44 |
<div id="div-gpt-ad-123456789-1"> |
|
45 |
<script type="text/javascript"> |
|
46 |
googletag.cmd.push(function() { |
|
47 |
googletag.display("div-gpt-ad-123456789-1"); |
|
48 |
}); |
|
49 |
</script> |
|
50 |
</div> |
|
51 |
</body> |
|
52 |
</html> |
Example of a synchronous Google Publisher Tag
Below is a sample of the full code you would use to create a synchronous Google Publisher Tag. This tag illustrates many of the common features and syntax used in a synchronous GPT ad request.
For more information about how to generate Google Publisher Tags, read our article on generating tags within DFP.
1 |
<html> |
|
2 |
<head> |
|
3 |
<script type="text/javascript"> |
|
4 |
(function() { |
|
5 |
var useSSL = 'https:' == document.location.protocol; |
|
6 |
var src = (useSSL ? 'https:' : 'http:') + |
|
7 |
'//www.googletagservices.com/tag/js/gpt.js'; |
|
8 |
document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>'); |
|
9 |
})(); |
|
10 |
</script> |
|
11 |
<script type="text/javascript"> |
|
12 |
googletag.pubads().set("adsense_background_color", "FFFFFF"); |
|
13 |
</script> |
|
14 |
<script> |
|
15 |
googletag.defineSlot("/1234/travel/asia/food", [728, 90], "div-gpt-ad-123456789-0") |
|
16 |
.addService(googletag.pubads()) |
|
17 |
<.setTargeting("interests", ["sports", "music", "movies"]); |
|
18 |
googletag.defineSlot("/1234/travel/asia/food", [[468, 60], [728, 90], [300, 250]], "div-gpt-ad-123456789-1") |
|
19 |
.addService(googletag.pubads()) |
|
20 |
.setTargeting("gender", "male") |
|
21 |
.setTargeting("age", "20-30"); |
|
22 |
googletag.pubads().setTargeting("topic","basketball"); |
|
23 |
googletag.pubads().enableSingleRequest(); |
|
24 |
googletag.pubads().enableSyncRendering(); |
|
25 |
googletag.enableServices(); |
|
26 |
</script> |
|
27 |
</head> |
|
28 |
<body> |
|
29 |
<div id="div-gpt-ad-123456789-0" style="width: 728px; height: 90px"> |
|
30 |
<script type="text/javascript"> |
|
31 |
googletag.display("div-gpt-ad-123456789-0"); |
|
32 |
</script> |
|
33 |
</div> |
|
34 |
<div id="div-gpt-ad-123456789-1"> |
|
35 |
<script type="text/javascript"> |
|
36 |
googletag.display("div-gpt-ad-123456789-1"); |
|
37 |
</script> |
|
38 |
</div> |
|
39 |
</body> |
|
40 |
</html> |
Define ad units inline in each ad tag
You can define ad units inline in each ad tag as the page loads. Although this is not the recommended tagging methodology, you may need to do this if you don't have ability to edit the header.
Because the ad tag uses the GPT JavaScript library, you'll need to include the code that loads the library before you include the ad tag code. The code that loads the library is included in the below example.
To define an ad slot as the page loads, use the following examples:
Asynchronous GPT exampleCall the GPT JavaScript library
<script type="text/javascript">
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement("script");
gads.async = true;
gads.type = "text/javascript";
var useSSL = "https:" == document.location.protocol;
gads.src = (useSSL ? "https:" : "http:") + "//www.googletagservices.com/tag/js/gpt.js";
var node =document.getElementsByTagName("script")[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
Sample ad tag 1: Asynchronous inline tag (this method does not support SRA)
<div id="div-gpt-ad-1234567891234-0">
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.defineSlot('/1234/sports/football', [728, 90],'div-gpt-ad-1234567891234-0')
.addService(googletag.pubads())
.setTargeting("Gender", "Male");
googletag.enableServices();
googletag.display('div-gpt-ad-1234567891234-0');
});
</script>
</div>
Sample ad tag 2: Asynchronous condensed inline tag (this method does not support SRA or ad-slot level targeting)
<div id='div-gpt-ad-1234567891234-0'>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.pubads().display('/1234/sports/football', [728, 90], 'div-gpt-ad-1234567891234-0');
});
</script>
</div>
Synchronous GPT example
Call the GPT JavaScript library
<script type="text/javascript">
(function() {
var useSSL = 'https:' == document.location.protocol;
var src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
document.write('');
})();
</script>
Sample ad tag 1: Synchronous inline tag (this method does not support SRA)
<div id='div-gpt-ad-1234567891234-0'>
<script type='text/javascript'>
googletag.defineSlot('/1234/sports/football', [728, 90],'div-gpt-ad-1234567891234-0')
.addService(googletag.pubads())
.setTargeting("Gender", "Male");
googletag.pubads().enableSyncRendering();
googletag.enableServices();googletag.display('div-gpt-ad-1234567891234-0');
</script>
</div>
Sample ad tag 2: Inline tags, condensed form (this method does not support SRA or ad slot-level targeting)
<div id='div-gpt-ad-1234567891234-0'>
<script type='text/javascript'>
googletag.pubads().display('/1234/sports/football', [728, 90], 'div-gpt-ad-1234567891234-0');
</script>
</div>
Override AdSense formatting at the page level (optional)
In most cases, you'll have already set formatting for your AdSense ads in DFP or AdSense. However, if you want to override this formatting at the individual page level, you can insert the following code into your tags.
For asynchronous tags:
<script>
googletag.cmd.push(function() {
googletag.pubads().set("adsense_background_color", "FFFFFF");
});
</script>
For synchronous tags:
<script>
googletag.pubads().set("adsense_background_color", "FFFFFF");
</script>
Use a non-JavaScript GPT tag to create a 1x1 tracking pixel
To create an impression tracker with GPT:
Create a new ad unit or choose an existing one with which to track the impression count.
Create a 1x1 line item and add a transparent pixel custom or third-party creative to it.
Target the 1x1 line item to the tracking ad unit.
Create a non-JavaScript GPT tag that calls the impression pixel.
Below is a sample of the code you would add to the creative code to create a 1x1 impression tracker using the non-JavaScript tag format:
<img src='http://pubads.g.doubleclick.net/gampad/ad?iu=/1234/ad_unit&sz=1x1&t=&c=12345678'/>Use the impression pixel tag in one of two ways:
Add the tag to a custom or third-party creative that will be delivered onto the page: In the creative's "Settings" tab, add the tag to the top of the "Code snippet" box.
Add the tag directly to the web page.
For more information about how to generate non-JavaScript based URLs, read our article about serving ads to a non-JavaScript environment.
