I have so problem to run responsive DFP ads in my website. I read all the google specs but still cant figure it out.
<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() {
googletag.defineSlot('/4421777/Textlink', [468, 60], 'div-gpt-ad-1431019765846-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.companionAds().setRefreshUnfilledSlots(true);
googletag.pubads().enableVideoAds();
googletag.enableServices();
});
and here is my mapping code :
var mapping = googletag.sizeMapping().
addSize([1024, 768], [970, 250]).
addSize([980, 690], [[1000, 300],[320, 50]]).
addSize([640, 480], [[120, 60],[1000, 300],[320, 50], [468, 60]]).
addSize([0, 0], [88, 31],).
// Fits browsers of any size smaller than 640 x 480
build();
adSlot.defineSizeMapping(mapping);
can you please give some clear way to make it run perfect on mobile etc? thanks
You can detect browser width using javascript
Full example:
here is my code now its that correct?
It look like you are still missing from each gptAdSlots[x]
Here is a simplified version of what you are trying to achieve (considering your size mapping is weird, as you will display a 1000x300 banner on a device with viewport of 640x480?).
Take a note that there are few minor changes: 1. An array of the ad slots is created starting with the first being 0. But if you want to call a second slot, you should add something like this after the first one (note the [1], next would be [2] and so on):
Your code is incorrect as it doesn't link the size mapping with ad unit. Note the bold part in my code: 'div-gpt-ad-1431019765846-0').defineSizeMapping(mapping).addService(googletag Also, I think you need different size mappings for each ad slot. I am giving an example: 1. You have a big banner on the top of your website, let's say the Ad Unit (Ad Slot) is called Billboard. So, if it is desktop, you want to serve a big banner - 1000x300, if large tablet or small desktop - 728x90 and if it is a mobile device a mobile leaderboard - 320x100. 2. Than you have another ad unit, let's say it is called Rectangle. You want this to serve 300x250 or 300x600 on desktop. 300x250 on tablets and phones. 3. Then you have a third ad unit, let's say it is called Smallbanner. You want this to serve 468x60 on desktop and tablets and no banner on phones.
You will need to create a size mapping for each ad unit and then attach it when called. So in my example case, the output code will be something like this. I am adding inline comments so you can probably understand it better.