Write a Javascript to Generate Key-Values for DFP Targeting

Here’s an overview about a client’s tasks having some of the details omitted.

We use Doubleclick for Publishers (small business – ie. free edition) and WordPress.

We need to target ads to visitors with key/values through DFP. We need you to provide javascript code which adds these key/values into the DFP code at the correct location.

We need you to provide two pieces of javascript code:

1) For all posts, pages and category and tag archives, we need you to provide javascript to add the key of ‘p’ and the value is the url for that page.

We have a tool (WP Advanced Ads) that can deliver separate javascript code to posts, pages and archives if it needs to be different codes for each type of page.

2) We also need you to provide a javascript to add a specific key/value. These will be for specific batches of pages and, again we will use WP Advanced Ads to deliver this code. We need to be able to manually change the key and value in the code you provide.

An example of these batches are pages that identify readers below 34 years old. We would give these pages a key of ‘a’ (for Age) and a value of ‘u34’ (for under 34).

We also use WP Advanced Ads to deliver cookies to these batches of pages at the same time, but don’t think this would affect your code.

Please also note that we already have javascript code that is reading cookies and adding the key/values so your code must not affect this code.

Please note that we currently have some javascript that currently inserts key/values based on cookies.

You will need to make sure your code does not conflict with this. I’ve copied the code below.

function getKeyByValue(object,value){return Object.keys(object).find(key=>object[key]===value);}
function sg_targeting(){var ca=document.cookie.split(';');var keys=[];var vals={};var uniquekeys=[];for(var i=0;i<ca.length;i++){matches=null;var regex=/sg_c_([a-zA-Z0-9]+)_([a-zA-Z0-9]+)/gi;cookietext=ca[i];matches=regex.exec(cookietext);if(matches&&matches.length>0){targeting='';k=null;v=null;found=null;k=matches[1];v=matches[2];keys.push(k);found=[k,v];vals[i]=found;}}
var keyslength=keys.length;for(var i=0;i<keyslength;i++){if(uniquekeys.indexOf(keys[i])==-1){uniquekeys.push(keys[i]);}}
var uniquekeyslength=uniquekeys.length;for(var i=0;i<uniquekeyslength;i++){thisvals=null;thisvals=[];thisvalslength=null;for(var kvals in vals){if(vals.hasOwnProperty(kvals)){if(vals[kvals][0]==uniquekeys[i]){thisvals.push(vals[kvals][1]);}}}
googletag.pubads().setTargeting(uniquekeys[i],[thisvals.toString()]);}}
sg_targeting();

We use WP Advanced Ads to deliver our ads and cookies and some scripts. We are in the process of switching some over to Google Tag Manager. You can see our DFP header scripts here:

Please let me know if you have any more questions at this stage.

Regarding #2 above, for example, we have a batch of articles.

We would like these pages to have the key of “a” (for “Age”) and the value of “u34” (which signifies “under 34”).

Please note that WP Advanced Ads drops a cookie here (see in the “Ad Parameters” section) which is read by the other javascript code I mentioned. That means on subsequent pages readers of these pages are still identified as being under 34. The reason that we need your code is that we also need the key/value added on these initial page views.

If you’re interested about how I managed to solve the client concerns, drop us a message.

Add comment

E-mail is already registered on the site. Please use the Login form or enter another.

You entered an incorrect username or password

Sorry, you must be logged in to post a comment.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Done