Skip to content

Instantly share code, notes, and snippets.

@betancourtl
Created March 13, 2019 15:48
Show Gist options
  • Save betancourtl/78335635c1bc9943742da3bf26327bb4 to your computer and use it in GitHub Desktop.
Save betancourtl/78335635c1bc9943742da3bf26327bb4 to your computer and use it in GitHub Desktop.
import { setSlotTargeting } from '../vendors';
import jsCookie from 'js-cookie';
import keywords from '../util/keywords';
import latencyUtil from '../util/latency';
import {
isAtlas,
isArticle,
page_type,
utag_data,
isSlideshow,
profile_type,
rankinglist_type,
pharma_campaign_id
} from '../util/validatedUtag';
function setTargetingWithUDO(dfpOptions, attribute) {
// Remove potentially dangerous characters from targeting values
const removeChars = /\&|\'|\"|\:/g,
hideChars = /\s|\/|\\/g;
if (typeof utag_data[attribute] !== 'undefined' && utag_data[attribute] !== null) {
let udoValue = utag_data[attribute];
// lowercase, no special chars, hyphens for spaces
udoValue = udoValue
.replace(/(,|;)\s/g, '$1') // Remove space in lists
.replace(removeChars, '')
.replace(hideChars, '-')
.replace(/\-+/g, '-') // Remove multiple -'s in a row
.toLowerCase();
dfpOptions.setTargeting[attribute] = udoValue;
}
}
export const setPharmaTargeting = (pageType, profileType, pharma_campaign_id, options = {}, obj = []) => {
const matchesPageTypes = options.pageTypes.some(x => x === pageType);
const matchesProfileTypes = options.profileTypes.some(x => x === profileType);
const matchesConditions = [matchesPageTypes, matchesProfileTypes].every(x => x === true);
if (matchesConditions) obj['pharma_campaign_id'] = pharma_campaign_id;
return obj;
};
/**
* This function will look for cookies set by DFP custom creatives and will
* return an object containing the cookie as a key-value pair.
*
* @param {Object} targeting
* @param {String[]} cookieNames - Cookies that we want to find in the page.
*/
export const setCustomCreativesTargeting = (targeting = {}, cookieNames = []) => {
return cookieNames.reduce((acc, cookieName) => {
const cookieValue = jsCookie.get(cookieName);
const val = cookieValue;
if (val) acc[cookieName] = val;
return acc;
}, targeting);
};
function setPageLevelTargeting(dfpOptions) {
dfpOptions.setTargeting = {
'kw': keywords.getKeywords(),
'pagetype': page_type,
'rankinglisttype': rankinglist_type,
'ksg': window.Krux ? window.Krux.segments : [],
'kuid': window.Krux ? window.Krux.user : ""
};
dfpOptions.setTargeting = setPharmaTargeting(
page_type,
profile_type,
pharma_campaign_id,
{ pageTypes: ['profile'], profileTypes: ['doctor', 'hospital'] },
dfpOptions.setTargeting
);
dfpOptions.setTargeting = setCustomCreativesTargeting(
dfpOptions.setTargeting,
['content_carousel_user'],
);
if (isArticle) {
const heroimage = (typeof utag_data.article_template !== "undefined" &&
utag_data.article_template.indexOf("article-wide") === 0);
dfpOptions.setTargeting['heroimage'] = heroimage.toString();
}
if (isAtlas) {
// Atlas Specific Targeting Key-Values
dfpOptions.setTargeting['siteversion'] = "atlas";
if (isSlideshow) {
const queryString = (window.location) ? window.location.search : '';
const onepage = queryString.match(/onepage/) ? "true" : "false";
dfpOptions.setTargeting['onepage'] = onepage;
}
const queryString = (window.location) ? window.location.search : '';
const disable = queryString.match(/usn_disable=(.+?)(?=&|\?|$)/) || false;
if (disable && disable[0] && disable[0].match("creatives")) {
dfpOptions.setTargeting['usn_disable'] = "creative";
}
} else {
dfpOptions.setTargeting['siteversion'] = "responsive";
}
//Some custom ad targeting based on Tealium tags
const tealiumTags = [
'doctor_hospital_affiliations',
'doctor_location',
'doctor_specialization',
'doctor_sub_specialization',
'has_photo', // A bunch of custom targeting for doctor profiles (DR-1012)
'has_doctor_type',
'has_board_certifications',
'has_languages',
'has_appointments',
'has_insurance',
'has_hospital',
'has_procedures',
'has_education',
'has_certifications_and_licensure',
'has_awards',
'has_publications',
'hospital_specialty', // For both doctors and hospitals as cross-reference (HOSP-1873)
'hospital_city', // For hospitals (HOSP-1863)
'hospital_state',
'fund_family', // For fund/etf profile and search pages -> MON-10
'usn_pageview_id',
'usn_session_id',
'usn_visitor_id',
];
const optLength = tealiumTags.length;
let tag = 0;
for (tag; tag < optLength; tag++) { setTargetingWithUDO(dfpOptions, tealiumTags[tag]); }
}
function setSlotLevelTargeting(dfpAds, excludedVendors) {
latencyUtil.log('Bid-Targeting', null, null, true);
setSlotTargeting(dfpAds, excludedVendors);
latencyUtil.log("Bid-Targeting");
}
export { setPageLevelTargeting, setSlotLevelTargeting };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment