Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
asynchronous Google DFP for Publishers Tag code with explanations
<!-- the id must match the id defined in the slot -->
<!-- style attribut is optional, but useful to prevent to shift layout after ad is loaded -->
<div id="banner1" style="width:300px; height:250px;">
<script type="text/javascript">
// id 'banner1' is used here, again
googletag.cmd.push(function() { googletag.display('banner1'); });
<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:") + "//";
var node =document.getElementsByTagName("script")[0];
node.parentNode.insertBefore(gads, node);
<script type="text/javascript">
// js pushed like this will be added to the gdfp queue and processed asynchronously
googletag.cmd.push(function() {
// 6355419 - your individual network code you find in the admin panel
// Travel/Europe/France/Paris - name of your target ad unit
// [300, 250] - allowed sizes of the creatives (should be associated with the ad unit)
// can also have multiple values, e.g. [[300,250],[468,60]]
// banner1 - unique id matching the banner loaded in the header with the one in the body
// even though the ids generated by GDFP might look complicated, its just their pattern, but not to identify the ad unit on their server
googletag.defineSlot('/6355419/Travel/Europe/France/Paris',[300, 250], "banner1")
// (optional) slot-level key-value targeting
// can also have multiple values, e.g. ("category", ["games","tests"])
.setTargeting("pagetype", "article")
// (optional) page-level key-value targeting; will be inherited by all ad units
// (optional) include this line if all ads should be loaded in a single request (only disable for specific use cases)
// wenn, yes, you really want to get the ads defined above, right?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment