Skip to content

Instantly share code, notes, and snippets.

@rdillmanCN
Created December 30, 2017 09:23
Show Gist options
  • Save rdillmanCN/a70ec955d9a982127fefadabe8b898b5 to your computer and use it in GitHub Desktop.
Save rdillmanCN/a70ec955d9a982127fefadabe8b898b5 to your computer and use it in GitHub Desktop.
This code will display page level targeting and slot level data including sizes, targeting, outOfPage, elementId, advertiserId, campaignId, creativeId, isBackfill, labelIds, lineItemId, outOfPage, sourceAgnosticCreativeId, sourceAgnosticLineItemId, and a URL to this creative at DFP.
/**
* Gathers the page targeting data.
*
* @private
* @param {Object} page - The internal object to append data to.
* @returns {undefined}
*/
function getPageTargeting(page) {
window.googletag.pubads().getTargetingKeys().forEach(function(keys) {
page.pageTargeting[keys] = window.googletag.pubads().getTargeting(keys);
});
}
/**
* Gather the slots.
*
* @private
* @param {Object} page - The internal object to append data to.
* @returns {undefined}
*/
function getSlots(page) {
window.googletag.pubads().getSlots().forEach(function(slot) {
page.slots[slot.getSlotElementId()] = getSlotData(slot);
});
}
/**
* Gather individual slots data.
*
* @private
* @param {Object} slot - The slot data returned from an ad call to DFP.
* @returns {Object} The cleaned slot data.
*/
function getSlotData(slot) {
var result = {
sizes: [],
outOfPage: slot.getOutOfPage(),
targeting: slot.getTargetingMap(),
elementId: slot.getSlotElementId()
};
var data = slot.getResponseInformation();
result.advertiserId = data && data.advertiserId || "";
result.campaignId = data && data.campaignId || "";
result.creativeId = data && data.creativeId || "";
result.isBackfill = data && !!data.isBackfill;
result.labelIds = data && data.labelIds || "";
result.lineItemId = data && data.lineItemId || "";
result.outOfPage = data && !!data.outOfPag;
result.sourceAgnosticCreativeId = data && data.sourceAgnosticCreativeId || "";
result.sourceAgnosticLineItemId = data && data.sourceAgnosticLineItemId || "";
result.DFP = data && data.creativeId && "https://www.google.com/dfp/3379#delivery/CreativeDetail/creativeId=" + data.creativeId || "";
slot.getSizes().forEach(function(size) {
result.sizes.push(typeof size === 'object' ? size.getWidth() + "x" + size.getHeight() : size);
});
return result;
}
/**
* Gets the ads data fro googletag.
*
*
* @private
* @returns {undefined}
*/
function getAdsData() {
var page = {
pageTargeting: {},
slots: {}
};
getPageTargeting(page);
getSlots(page);
console.info('## Ad Data', page);
}
/**
* Simply tick till the data is available.
*/
(function waitForGPT(){
if(window.googletag && window.googletag.pubadsReady){
getAdsData();;
}
else{
setTimeout(waitForGPT, 250);
}
}());
@adentify
Copy link

Thanks for this, it's a very handy script.

One addition I'd suggest would be to include the 'data-google-query-id' value for each slot, this enables end-users to identify the exact call for that adslot in GAM/DFP... very handy when trying to identify or block specific AdX delivered ads. Adding the following line around line 52 adds a 'queryId' element to each slot object.

result.queryId = document.getElementById(slot.getSlotElementId()).getAttribute("data-google-query-id") || "";

@kunjan97
Copy link

How can i get slot is empty or not ?
Can you please tell me?

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