Skip to content

Instantly share code, notes, and snippets.

@vigikaran
Last active October 13, 2016 11:19
Show Gist options
  • Save vigikaran/8f3462301c3d0be769d8e7bdf7fde044 to your computer and use it in GitHub Desktop.
Save vigikaran/8f3462301c3d0be769d8e7bdf7fde044 to your computer and use it in GitHub Desktop.
<script>
// Load GPT
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function(){
var gads = document.createElement('script');
gads.async = true;
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>
/*
in : inventory
sz : size
sl :slot
pr : is preload ?
re : is replace ?
rs : is responsive ?
rf : refreshed ?
*/
var ads =[{
in: '/25671928/lankasri//video.banner//feed//infinite',
sz: [300, 250],
sl : 'dfp_banner_feed_infinite_1',
pr: true,
re: false,
rs:false,
rf : false
}
];
var slots = [];
var pub;
googletag.cmd.push(function() {
pub = googletag.pubads();
var preload = [];
googletag.pubads().setCentering(true);
googletag.pubads().disableInitialLoad();
googletag.pubads().enableSingleRequest();
googletag.enableServices();
ads.forEach(function(e,i){
if(e.rs[0]){
if(e.rs[1] == 0)
{
var mapping = googletag.sizeMapping().addSize([100, 100], [320, 100]).addSize([320, 400], [320, 100]).
addSize([320, 700], [320, 100]).addSize([750, 200], [728, 90]).addSize([1050, 200], [970, 90]).build();
}
else if(e.rs[1] == 1)
{
var mapping = googletag.sizeMapping().addSize([100, 100], [320, 100]).addSize([320, 400], [300, 250])
.addSize([320, 700], [300, 250]).addSize([750, 200], [728, 90]).addSize([1050, 200], [970, 90]).build();
}
else
{
var mapping = googletag.sizeMapping().addSize([100, 100], [320, 100]).addSize([320, 400], [300, 250])
.addSize([320, 700], [300, 250]).addSize([750, 200], [336, 280]).addSize([1050, 200], [728, 90]).build();
}
slots[i] = googletag.defineSlot(e.in, e.sz, e.sl).defineSizeMapping(mapping).addService(googletag.pubads()).setCollapseEmptyDiv(true, true);
}
else{
slots[i] = googletag.defineSlot(e.in, e.sz, e.sl).addService(googletag.pubads()).setCollapseEmptyDiv(true, true);
}
googletag.display(ads[i].sl);
if(e.pr){
preload.push(slots[i]);
ads[i].rf = true;
}
else if ( verge.inViewport(document.querySelector('#'+e.sl+'_panel'),300) && !e.rf && $('#'+e.sl+'_panel').is(':visible'))
{
preload.push(slots[i]);
ads[i].rf = true;
}
});
pub.addEventListener('slotRenderEnded', function(event){
var divId = event.slot.getSlotElementId();
if(event.isEmpty === false){
if( $("#"+divId+"_panel").has("#"+divId+"_replace").length > 0 )
{
$("#"+divId+"_replace").hide();
}
}
else
{
$("#"+divId).hide();
}
});
googletag.pubads().refresh(preload);
});
var refreshDFP = function()
{
console.log("hi");
ads.forEach(function(e,i)
{
var pub = googletag.pubads();
if ( verge.inViewport(document.querySelector('#'+e.sl+'_panel'),300) && !e.rf && $('#'+e.sl+'_panel').is(':visible'))
{
googletag.cmd.push(function() {
googletag.pubads().refresh([slots[i]]);
});
ads[i].rf = true;
}
});
}
window.addEventListener('scroll', refreshDFP);
window.addEventListener('resize', refreshDFP);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment