Skip to content

Instantly share code, notes, and snippets.

@yogi87
Created May 18, 2017 17:50
Show Gist options
  • Save yogi87/fd6bc5311cbdb7b944a9665f19861611 to your computer and use it in GitHub Desktop.
Save yogi87/fd6bc5311cbdb7b944a9665f19861611 to your computer and use it in GitHub Desktop.
var nr_monitor_ads = function(){
var convertToString = ['advertiserId', 'campaignId', 'creativeId', 'lineItemId', 'labelIds', 'sourceAgnosticCreativeId', 'sourceAgnosticLineItemId'];
var NR_SUBMIT_PAGE_ACTION = function(ev, obj) {
if (typeof window.newrelic != 'undefined' && typeof window.newrelic.addPageAction == 'function') {
for (i in obj) {
if (convertToString.indexOf(i) > -1) {
obj[i] = String(obj[i]);
}
}
window.newrelic.addPageAction(ev, obj)
}
};
var NR_ONLOAD = Date.now();
var NR_LOADED_TIMESTAMP = Date.now();
var ready = false;
var containers = {};
var wrapFn = function(fn){
return function(){
try{
if (ready) {
NR_LOADED_TIMESTAMP = Date.now();
}
if (arguments && arguments.length > 0) {
var container = arguments[0];
containers[container] = Date.now();
}
return fn.apply(this, arguments);
}catch(ex){
ErrorHandler.Exception(ex);
}
};
};
function wrapRefresh() {
googletag.display = wrapFn(googletag.display);
};
window.googletag = window.googletag || {};
wrapRefresh();
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(detectAdLoad);
function detectAdLoad() {
if(window.googletag && googletag.pubadsReady) {
ready = true;
var NR_PUBADSREADY = Date.now();
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
if (event && event.slot) {
var container = event.slot.getSlotElementId();
NR_SUBMIT_PAGE_ACTION('pubAdsRendered', {
readyTime: Date.now(),
advertiserId: event.advertiserId,
campaignId: event.campaignId,
creativeId: event.creativeId,
isEmpty: event.isEmpty ? 'empty' : 'notEmpty',
lineItemId: event.lineItemId,
labelIds: event.labelIds,
elementId: event.slot.getSlotElementId(),
serviceName: event.serviceName,
width: (event.size && event.size.length > 0) ? event.size[0] : null,
height: (event.size && event.size.length > 1) ? event.size[1] : null,
slotContentChanged: event.slotContentChanged,
sourceAgnosticCreativeId: event.sourceAgnosticCreativeId,
sourceAgnosticLineItemId: event.sourceAgnosticLineItemId,
slotId: (event.slot && event.slot['$']) ? event.slot['$'] : null,
onloadTime: NR_LOADED_TIMESTAMP,
adsReady: NR_PUBADSREADY,
NR_ONLOAD: NR_ONLOAD,
displayTime: (containers[container]) ? containers[container] : NR_LOADED_TIMESTAMP
});
}
});
googletag.pubads().addEventListener('impressionViewable', function(event) {
if (event && event.slot) {
var container = event.slot.getSlotElementId();
NR_SUBMIT_PAGE_ACTION('pubAdsViewable', {
readyTime: Date.now(),
serviceName: event.serviceName,
elementId: event.slot.getSlotElementId(),
slotId: (event.slot && event.slot['$']) ? event.slot['$'] : '',
advertiserId: (event.slot && event.slot['H'] && event.slot['H'].advertiserId) ? event.slot['H'].advertiserId : null,
campaignId: (event.slot && event.slot['H'] && event.slot['H'].campaignId) ? event.slot['H'].campaignId : null,
creativeId: (event.slot && event.slot['H'] && event.slot['H'].creativeId) ? event.slot['H'].creativeId : null,
lineItemId: (event.slot && event.slot['H'] && event.slot['H'].lineItemId) ? event.slot['H'].lineItemId : null,
labelIds: (event.slot && event.slot['H'] && event.slot['H'].labelIds) ? event.slot['H'].labelIds : null,
onloadTime: NR_LOADED_TIMESTAMP,
adsReady: NR_PUBADSREADY,
NR_ONLOAD: NR_ONLOAD,
displayTime: (containers[container]) ? containers[container] : NR_LOADED_TIMESTAMP
});
}
});
}
}
};
if (window.addEventListener) {
window.addEventListener('load', nr_monitor_ads, false);
} else if (window.attachEvent) {
window.attachEvent('onload', nr_monitor_ads);
}
@jaguilar87
Copy link

jaguilar87 commented Sep 20, 2017

I've found this documentation from google:
https://developers.google.com/doubleclick-gpt/reference#googletag.events.slotVisibilityChangedEvent

I think this is a really interesting event, as it exposes the % of adbanner shown.

@fIa5h
Copy link

fIa5h commented Nov 15, 2017

Well look what we have here... ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment