Skip to content

Instantly share code, notes, and snippets.

@RichardDillman
Created January 3, 2018 19:42
Show Gist options
  • Save RichardDillman/eedf9ebc2bd25d43c075316c63eea1a3 to your computer and use it in GitHub Desktop.
Save RichardDillman/eedf9ebc2bd25d43c075316c63eea1a3 to your computer and use it in GitHub Desktop.
Obtain label id's from loaded slots within GPT
/**
* Gets the label IDs from all currently loaded GPT slots.
*
* @return {Array)} The collected label ids.
*/
function getLabelIds() {
// declare our output array.
var labels = [];
// for looping over the slots
function getlabels(slot) {
// get our labelIds
var rsp = slot.getResponseInformation() && slot.getResponseInformation().labelIds || [];
// Filter for new ids in this slot.
var updates = rsp.filter(function(item) {
return labels.indexOf(item) < 0;
});
// Update the labels array.
labels = labels.concat(updates);
}
// Loop over the slots and get all the IDs.
window.googletag.pubads().getSlots().map(getlabels);
return labels;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment