DFP Adverts - How to show adverts in different places depending on screen size - google-dfp

I am using Google DFP adverts and I want to change the position they are in when the site is viewed on mobile.
I have tried duplicating the advert and showing one/ hiding the other but that still shows both adverts.
Is there any way to accomplish this, please see attached image for an example of what I am after.

Well, this is kind of hard, but not impossible. I will give you even more complicated example, where not only the banner could be in different places, but could be with different sizes depending on the device.
Scenario: You have an Ad Unit called adunit1. You want to show in the content on desktop, but on top on mobile. On desktop you want to be 300x250, and on mobile you want to 320x100 or 320x50. We consider desktop everything with viewport width of 970px and mobile anything smaller (could add more cases, but let's not make it too complicated).
Step 1: Go to the Ad Unit options in DFP and add all sizes that this ad unit will display (in our scenario: 300x250, 320x100, 320x50).
Step 2: Create size mappings variable for each sub-scenario. As bellow:
var adunit1desktop = googletag.sizeMapping().addSize([970, 0], [300, 250]).addSize([0, 0], []).build();
Which means: If viewport width is more than 970 AND viewport height is more than 0, serve banner with size 300x250. If smaller, serve nothing.
Now let us make the same for the mobile version with different name of the variable:
var adunit1mobile = googletag.sizeMapping().addSize([970, 0],[]).addSize([0, 0], [[320,100],[320,50]]).build();
Which means: If viewport width is more than 970 AND viewport height is more than 0, serve nothing. If smaller serve either 320x100 OR 320x50.
We will now define the Ad Units, but will make this twice as will assign different size mapping for desktop and mobile.
For desktop:
gptAdSlots[0] = googletag.defineSlot('/XXX/adunit1', [[300, 250], [320, 100], [320, 50]], 'div-banner-desktop').defineSizeMapping(adunit1desktop).addService(googletag.pubads());
Two details to watch here: div-banner-desktop - this is the div id where we want to show the desktop banner. defineSizeMapping(adunit1desktop) - this is how we tell DFP to show this based on the size rules above (in brief: show the desktop banner, only if viewport width is at least 970px).
Now for the mobile:
gptAdSlots[1] = googletag.defineSlot('/XXX/adunit1', [[300, 250], [320, 100], [320, 50]], 'div-banner-mobile').defineSizeMapping(adunit1mobile).addService(googletag.pubads());
Pay attention to three details: div-banner-mobile, defineSizeMapping(adunit1mobile) and gptAdSlots[1] - we increased with 1 for the next slot rendering. If there are more, each should be increased by 1 as well.
Now go to your web site and put the following code where you want to display the desktop banner:
<div id="div-banner-desktop">
<script>
googletag.cmd.push(function() { googletag.display('div-banner-desktop'); });
</script>
</div>
Now place this code where you want to display the mobile banner:
<div id="div-banner-desktop">
<script>
googletag.cmd.push(function() { googletag.display('div-banner-desktop'); });
</script>
</div>
The whole code block should look something like this
<script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script>
<script>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
</script>
<script>
var gptAdSlots = [];
googletag.cmd.push(function() {
var adunit1desktop = googletag.sizeMapping().addSize([970, 0], [300, 250]).addSize([0, 0], []).build();
var adunit1mobile = googletag.sizeMapping().addSize([970, 0],[]).addSize([0, 0], [[320,100],[320,50]]).build();
gptAdSlots[0] = googletag.defineSlot('/XXX/adunit1', [[300, 250], [320, 100], [320, 50]], 'div-banner-desktop').defineSizeMapping(adunit1desktop).addService(googletag.pubads());
gptAdSlots[1] = googletag.defineSlot('/XXX/adunit1', [[300, 250], [320, 100], [320, 50]], 'div-banner-mobile').defineSizeMapping(adunit1mobile).addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();
});
</script>

Related

How to show a advert depending on screen size

I have a page with a banner advert that will shrink/grow depending on screen size or if it's desktop or mobile.
The banner will be displayed with in one div.
The adverts are in three different advert ID's that each have their own size:
Example:
ad-1 is 1220x350
ad-2 is 650x190
ad-3 is 300x250
How do I make the correct advert display in a div depending on the div size?
You could use sizeMapping to dispatch your sizes based on the viewport size.
First : define the sizeMapping
var mapping = googletag.sizeMapping()
.addSize([1024, 768], [1220, 350])
.addSize([980, 690], [650, 190])
.addSize([0, 0], [300, 250])
.build();
Usage : addSize([screenWidth, screenHeight], [width,height])
Second : add the mapping to your slot definition
var slot = googletag.defineSlot('/adUnitPath/', [[1220, 350], [650, 190], [300, 250]], 'yourTargetId')
.defineSizeMapping(mapping)
.addService(googletag.pubads());
Full documentation here and here

google-dfp enable lazy loading only on certain ads

I am using google-dfp's lazyLoading. When this is enabled, all Ads are lazy loaded. Is there a way to enable it only for specific ads and not for all?
googletag.pubads().enableLazyLoad({
// Fetch slots within 6 viewports.
fetchMarginPercent: 500,
// Render slots within 1.04 viewports.
renderMarginPercent: 4,
// Double the above values on mobile, where viewports are smaller
// and users tend to scroll faster.
mobileScaling: 10,
});
There is a way of doing it by splitting your adrequests in 2 batchs :
googletag.cmd.push(function() {
// Define ad slots.
var slots = [
googletag.defineSlot('/6355419/Travel/Europe', [728, 90], 'slot-1')
.addService(googletag.pubads()),
googletag.defineSlot('/6355419/Travel/Europe', [728, 90], 'slot-2')
.addService(googletag.pubads()),
googletag.defineSlot('/6355419/Travel/Europe', [300, 250], 'slot-3')
.addService(googletag.pubads()),
googletag.defineSlot('/6355419/Travel/Europe', [300, 250], 'slot-4')
.addService(googletag.pubads()),
googletag.defineSlot('/6355419/Travel/Europe', [300, 250], 'slot-5')
.addService(googletag.pubads())
];
// Disable initial load to precisely control when ads are requested.
googletag.pubads().disableInitialLoad();
// Enable SRA and services.
googletag.pubads().enableSingleRequest();
googletag.enableServices();
// Issue first SRA request (slots 1 and 2) immediately.
googletag.pubads().refresh([slots[0], slots[1]]);
// Enable lazyLoading after 1st SRA batch
googletag.pubads().enableLazyLoad({
// Fetch slots within 6 viewports.
fetchMarginPercent: 500,
// Render slots within 1.04 viewports.
renderMarginPercent: 4,
// Double the above values on mobile, where viewports are smaller
// and users tend to scroll faster.
mobileScaling: 10,
});
// Issue second SRA request (slots 3, 4 and 5) once lazyLoading has been enabled
googletag.pubads().refresh([slots[2], slots[3], slots[4]]);
});
See here for details about SRA batching.

Blank responsive banner on cellphone and tablet (sizeMapping)

I have a custom 1600x433 banner, I've created it as responsive and I'm trying to use sizeMapping, on the desktop the banner works, however, on the tablet and mobile the banner area just goes blank.
Set custom sizes in settings> sizes. The original image has exactly. 1600x433 What am I doing wrong that the banner is not responsive as I customize it?
googletag.cmd.push(function() {
var mapping = googletag.sizeMapping().
// Accepts both common mobile banner formats
addSize([320, 400], [320, 50]).
// Same width as mapping above, more available height
addSize([320, 700], [360, 100]).
// Desktop
addSize([1050, 200], [1600, 433]).build();
googletag.defineSlot('/id/sitenovo',[320, 50], 'div-gpt-ad-0').defineSizeMapping(mapping).addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
Size [320, 400] matches a viewport greater than or equal to 320, 400 pixels. Use a size [0,0

Google Piechart

i try to create a pie chart on my Website, i found the example code
<div id="piechart"></div>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 8],
['Eat', 2],
['TV', 4],
['Gym', 2],
['Sleep', 8]
]);
// Optional; add a title and set the width and height of the chart
var options = {'title':'My Average Day', 'width':550, 'height':400};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
(https://www.w3schools.com/howto/tryit.asp?filename=tryhow_google_pie_chart)
But i only get a Error Message :
Table has no columns.
Can someone help me pls?
Thanks
unfortunatelly I cannot comment.
This code works - fiddle. Could you give more details about this project?
sample of code for fiddle
<div id="piechart"></div>
edit:
Table has no columns.
Is this error message from browser console?

DFP: Getting the current click-through URL

I am setting up a DFP for multiple site, we have a set of line items and for each of it's creatives, the current click through is going to example.com (our own .com site), but since we are running multiple TLDs, we also want the click through URL to change accordingly. For example, when the ads is being displayed in the .jp, the click through should go to .jp.
In the DFP API reference, there's a function to change the click-through URL: http://support.google.com/dfp_premium/bin/answer.py?hl=en&answer=1650154&expand=adslot_details#setClickUrl
But in order to change our click-through URL, we also need to know what's the current URL. Example case: we need to get http://www.example.com/products/1 from the DFP adSlot in order to change it to http://www.example.jp/products/1.
I ran through trial and error using chrome web JS console and found a getClickUrl() function in the adSlot class, but it keeps returning empty string, for example:
googletag.defineSlot("/1234/Test_300x250", [300, 250], 'div-1').getClickUrl();
googletag.defineSlot("/1234/Test_300x250", [300, 250], 'div-1').addService(googletag.pubads()).getClickUrl();
Anyone have experience with this?
If you have control over the creative, I think it would be a lot easier to do by passing custom variables. You use the setTargeting method to pass a custom variable. So your client-side code to display the ad would look like this:
googletag.defineSlot("/1234/Test_300x250", [300, 250], 'div-1').setTargeting('region','jp');
And then in your HTML creative you use a pattern macro to replace part of the click-through URL.
<a href="http://www.example.%%PATTERN:region%%">
<img src="http://www.example.%%PATTERN:region%%/image.jpeg">
</a>
And DFP will replace the macro with whatever value you pass via setTargeting.
I haven't found a super simple way to do this - but it is possible.
Basically you can override an internal function in DFP and capture the content of the ad (and the URL) by getting into the DOM of the iframe.
Here is an example which should alert the URL of the ad (I've only tested this in chrome, so it may need tweaking to work in multiple browsers)
<html>
<head>
<title>DFP test</title>
<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>
<script type="text/javascript">
googletag.cmd.push(function() {
var slot1 = googletag.defineSlot('/12345678/Test_300x250', [300, 250], 'div-gpt-ad-1340819095858-0').addService(googletag.pubads());
slot1.oldRenderEnded = slot1.renderEnded;
slot1.renderEnded = function(){
alert(document.getElementById('div-gpt-ad-1340819095858-0').getElementsByTagName('iframe')[0].contentWindow.document.getElementsByTagName('a')[0].href.replace(/^.*&adurl=/,''));
slot1.oldRenderEnded();
};
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
</head>
<body>
<div id='div-gpt-ad-1340819095858-0' style='width:266px; height:115px;'>
<script type='text/javascript'>
googletag.cmd.push(function() {
googletag.display('div-gpt-ad-1340819095858-0');
});
</script>
</div>
</body>
</html>
If you are using jQuery its a bit nicer to use something like:
$(adUnit).find('iframe:first').contents().find('a')
Any questions let me know.