Skip to content

Instantly share code, notes, and snippets.

@mpjura
Created June 16, 2014 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpjura/d76cfb1ba2193c3d4ce4 to your computer and use it in GitHub Desktop.
Save mpjura/d76cfb1ba2193c3d4ce4 to your computer and use it in GitHub Desktop.
Ad Manager
/* Rule Id: 418180 */
/* Rule Id: 404574 */
(function doAdTargeting() {
//wait for jquery
if (undefined === window.$) {
setTimeout(doAdTargeting, 10);
return;
}
var waitTime = 0, //time spent waiting for crap to load
maxWaitTime = 2500; //maximum time to wait in milliseconds
if (HRST.targeting === undefined) {
HRST.targeting = {};
}
var initialLoad = true;
//Listen to the ad:notify event. This event is triggered when ads have been added to the page.
$(window).on("ad:notify", adCallback);
/**
adCallback
@description - This function will handle the "ad:notify" event coming from Ads.js.
@params - tags - jQuery collection of ad tags. You can grab a reference to the gpt slot object with tag.data("gptSlot")
**/
function adCallback(evt) {
var tags = evt.tags;
if (!tags) {
return;
}
if (initialLoad) {
//you can move stuff in here if you only want to do it on the first set of ads coming in
initialLoad = false;
}
//do page level stuff out here
//any key/value pair in HRST.targeting will be set
setAdInt();
setAdKeyword();
setAdReferrer();
doAmazonTargeting();
doAdSenseTargeting();
doMediaNetTargeting();
doCoreAudienceTargeting();
tags.each(function() {
//do slot level stuff in here
//any key/value pair in tag.data("targeting") will be set
var tag = $(this);
var targeting = tag.data("targeting") || {};
doSonobiTargeting(tag, targeting);
doYieldbotTargeting(targeting);
doAudienceScienceTargeting(tag, targeting);
tag.data("targeting", targeting);
});
HRST.Ads.deliver(tags);
}
/**
setAdInt - gets/sets the ad int cookie
**/
function setAdInt() {
var int1 = getCookie("al_show"),
int2 = getCookie("al_show2");
var adInt = int1;
if (int2 == "no") {
adInt = int2;
}
if (!int1) {
adInt = "yes";
setCookie("al_show", adInt, 1 / 24);
}
HRST.targeting.int = adInt;
}
/**
setAdKeyword - sets the ad keyword
**/
function setAdKeyword() {
var kw = getParam("kw");
HRST.targeting.kw = kw;
}
/**
setAdReferrer - sets the referring url
**/
function setAdReferrer() {
var referrer;
if (document.referrer && document.referrer.split("/", 3)[2].indexOf(location.hostname) > -1) {
referrer = "int";
} else {
referrer = "ext";
}
HRST.targeting.refer = referrer;
}
/**
doAmazonTargeting - amazon page level targeting
**/
function doAmazonTargeting() {
if (window.amznads === undefined) {
return false;
}
window.amznads.setTargetingForGPTAsync('amznslots');
}
/**
doAdSenseTargeting - ad sense page level targeting
**/
function doAdSenseTargeting() {
HRST.targeting.page_url = window.location.href.split('?')[0];
}
/**
doMediaNetTargeting - media.net page level targeting
**/
function doMediaNetTargeting() {
if (window._mNInslDisplay === true) {
HRST.targeting.mNInslDisplay = ["1"];
}
if (window._mNInslMobDisplay === true) {
HRST.targeting.mNInslMobDisplay = ["1"];
}
}
/**
doCoreAudienceTargeting - core audience page level targeting
**/
function doCoreAudienceTargeting() {
if (window.CA === undefined) {
return false;
}
var segmentID, CA = window.CA;
CA.doTargeting({
id: 85443
});
segmentID = CA.getTargetKeyValues().replace(/cas=/g, "");
HRST.targeting.cas = segmentID.split(";");
}
/**
private method doSonobiTargeting - sets sonobi targeting on certain div ids
@param tag - ad slot element
@param targeting - targeting object
**/
function doSonobiTargeting(tag, targeting) {
if (window.sbi_trinity === undefined) {
return false;
}
var id = tag.attr("id"),
sbi = window.sbi_trinity;
//check if this slot's id is in the sonobi list
if (sbi[id] !== undefined) {
sbi = sbi[id];
//if it is, set the targeting keys/values
for (var key in sbi) {
targeting[key] = sbi[key];
}
}
}
/**
private method doYieldbotTargeting - sets yieldbot targeting on certain div ids
@param tag - ad slot element
@param targeting - targeting object
**/
function doYieldbotTargeting(tag, targeting) {
if (window.yieldbot === undefined) {
return false;
}
var id, type = (HRST.article) ? HRST.article.type || "" : "";
if (/728,90/.test(data.sizemap) || /300,250/.test(data.sizemap)) {
id = (type == "gallery") ? "fb-" + tag.attr("id") : tag.attr("id");
targeting.ybot_ad = yieldbot.adAvailable(id);
targeting.ybot_slot = id;
}
}
/**
private method doAudienceScienceTargeting - sets audience science targeting on certain slot sizes
@param tag - ad slot element
@param targeting - targeting object
**/
function doAudienceScienceTargeting(tag, targeting) {
var data = tag.data();
//set this for any slot that can serve a 728x90
if (/728,90/.test(data.sizemap) && window.ASPQ_1lNiOU !== undefined) {
targeting.gwd = window.ASPQ_1lNiOU;
}
//set this for any slot that can serve a 300x250
if (/300,250/.test(data.sizemap) && window.ASPQ_rtXU6U !== undefined) {
targeting.gwd = window.ASPQ_rtXU6U;
}
}
/**
getParam - gets a parameter
@param param - param to get
**/
function getParam(key) {
var param, kvArray,
params = {},
paramString = window.location.search;
if (!paramString) {
return (typeof key !== "undefined") ? undefined : params;
}
kvArray = paramString.slice(1).split("&");
for (var i = 0, len = kvArray.length; i < len; i++) {
param = kvArray[i].split("=");
if (key && param[0] == key) {
return param[1];
}
params[param[0]] = decodeURIComponent(param[1]);
}
return (key) ? undefined : params;
}
/**
getCookie - gets a cookie
@param name - cookie name
**/
function getCookie(name) {
if (!name) {
return false;
}
var nameEQ = name + "=",
c, ca = document.cookie.split(';');
for (var i = 0, len = ca.length; i < len; i++) {
c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
}
}
/**
setCookie - sets a cookie
@param name - cookie name
@param value - cookie value
@param expire - cookie ttl
**/
function setCookie(name, value, expire) {
if (!name || typeof val === "undefined") {
return false;
}
var date,
expires = "";
if (days) {
date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
path = (path) ? "; path=" + path : "; path=/";
document.cookie = name + "=" + encodeURIComponent(val) + expires + path;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment