Last active
May 27, 2020 11:04
-
-
Save RichardDillman/d7f2e45aa0d2ca55dd72db72ac190a31 to your computer and use it in GitHub Desktop.
Retrieve all slot data from performance.getEntries. Fire it once all the slots have been requested. If you page happens to have a LOT of network traffic. you may want to expand the number of entries allowed using the following script before the first GPT request. window.performance.setResourceTimingBufferSize(10000);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* We collect this data from the performance entries instead of directly from GPT because the targeting | |
* can can change between requests. Pulling data from googletag after a refresh will only give you the | |
* cuurent state of GPT. | |
*/ | |
(() => { | |
function getUrlParams(search) { | |
let hashes = search.split('&').filter(item => { | |
return /^enc_prev_ius|^iu_parts|^prev_iu_szs|^prev_scp|^cust_params|^correlator/.test(item); | |
}); | |
let params = {}; | |
hashes.map(hash => { | |
let [key, val] = hash.split('='); | |
return params[key] = decodeURIComponent(val); | |
}); | |
return params; | |
} | |
function arrayToObject(obj, entry) { | |
obj[entry[0]] = entry[1]; | |
return obj; | |
} | |
function getPageTargeting(entry) { | |
return ((entry.cust_params && entry.cust_params.split('&')) || []) | |
.map(entry => entry.split('=')) | |
.reduce(arrayToObject, {}); | |
} | |
function createSlots(entry) { | |
const slots = []; | |
entry.prev_iu_szs.split(',').forEach(slot => { | |
slots.push({ | |
sizes: slot.split('|') | |
}); | |
}); | |
return Object.assign(entry, { | |
slots: slots | |
}); | |
} | |
function getSlotData(entry) { | |
const slots = []; | |
const pageTargeting = getPageTargeting(entry); | |
(entry.prev_scp.split('|') || []).forEach(slot => { | |
slot = (slot.split('&') || []).map(el => el.split('=')).reduce(arrayToObject, {}); | |
slots.push(slot); | |
}); | |
entry.slots.forEach((slot, i) => { | |
const parts = entry.iu_parts.split(',') | |
const ius = entry.enc_prev_ius | |
.split(',') | |
.map(iu => iu.split('/')) | |
.map(iu => iu.map(part => parts[part]).join('/')); | |
slot.targeting = slots[i]; | |
Object.assign(slot.targeting, pageTargeting); | |
slot.adUnit = ius[i]; | |
slot.correlator = entry.correlator; | |
}); | |
return { | |
slots: entry.slots, | |
}; | |
} | |
function combineEntries(prev, curr) { | |
return prev.concat(curr.slots); | |
} | |
function getGptData() { | |
const regex = new RegExp('pubads.g.doubleclick.net/gampad/ads'); | |
return window.performance | |
.getEntries() | |
.filter(entry => regex.test(entry.name)) | |
.map(entry => entry.name.split('?')[1]) | |
.map(getUrlParams) | |
.map(createSlots) | |
.map(getSlotData) | |
.reduce(combineEntries, []); | |
} | |
console.log('GptData', getGptData()); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment