Skip to content

Instantly share code, notes, and snippets.

@rafenden
Last active October 13, 2017 11:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rafenden/dab1d29c4504170b0d6ae2b5829f9870 to your computer and use it in GitHub Desktop.
Show targeting (key-values) of DFP adverts
var slots = googletag.pubads().getSlots();
var slotsOutput = {};
slots.forEach(function(slot) {
slotsOutput[slot.getSlotElementId()] = getSlotTargeting(slot);
});
function getSlotTargeting(slot) {
var url = slot.getContentUrl();
var parser = document.createElement('a');
parser.href = url;
var values = queryStringToObject(decodeURI(parser.search.substring(1)));
var cust_params = queryStringToObject(decodeURIComponent(values.cust_params));
var scp = queryStringToObject(decodeURIComponent(values.scp));
var targeting = Object.assign(cust_params, scp);
return targeting;
}
function queryStringToObject(queryString) {
queryString = (queryString);
return JSON.parse('{"' + queryString.replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}');
}
console.log(slotsOutput);
javascript:(function()%7Bvar%20slots%20%3D%20googletag.pubads().getSlots()%3Bvar%20slotsOutput%20%3D%20%7B%7D%3Bslots.forEach(function(slot)%20%7BslotsOutput%5Bslot.getSlotElementId()%5D%20%3D%20getSlotTargeting(slot)%3B%7D)%3Bfunction%20getSlotTargeting(slot)%20%7Bvar%20url%20%3D%20slot.getContentUrl()%3Bvar%20parser%20%3D%20document.createElement('a')%3Bparser.href%20%3D%20url%3Bvar%20values%20%3D%20queryStringToObject(decodeURI(parser.search.substring(1)))%3Bvar%20cust_params%20%3D%20queryStringToObject(decodeURIComponent(values.cust_params))%3Bvar%20scp%20%3D%20queryStringToObject(decodeURIComponent(values.scp))%3Bvar%20targeting%20%3D%20Object.assign(cust_params%2C%20scp)%3Breturn%20targeting%3B%7Dfunction%20queryStringToObject(queryString)%20%7BqueryString%20%3D%20(queryString)%3Breturn%20JSON.parse('%7B%22'%20%2B%20queryString.replace(%2F%22%2Fg%2C%20'%5C%5C%22').replace(%2F%26%2Fg%2C%20'%22%2C%22').replace(%2F%3D%2Fg%2C'%22%3A%22')%20%2B%20'%22%7D')%3B%7Dconsole.log(slotsOutput)%7D)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment