Instantly share code, notes, and snippets.
Created
September 26, 2012 00:32
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save tnydwrds/3785294 to your computer and use it in GitHub Desktop.
Setting targetable OXE cookies client-side
This file contains 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
/* | |
* Setting a targetable cookie (custom variable) whose value is a list. | |
*/ | |
(function(){ | |
var cookie | |
, cookieString | |
, cookieValueDelimiter = ',' | |
, date; | |
cookie = { | |
// Name of the cookie to set. Remember that this will be what you target | |
// in the UI as a custom variable. You can also set this up such that the | |
// cookie name is dynamic and controlled in the ad request. Your ad tag | |
// would look similar to: | |
// <iframe src="http://example.com/w/1.0/afr?auid=1234&cookiename=mycookiename"... | |
// | |
// You would use the macro for the custom variable: | |
// name: '{c.cookiename}', | |
// And would be expanded to: | |
// name: 'mycookiename', | |
name: 'mycookiename', | |
// Value of the cookie. To target a value or set of values with you need to | |
// use <list op>: | |
// mycookiename <list op> contains any of "12345,5678" | |
// mycookiename <list op> contains only "12345" | |
// mycookiename <list op> contains none of "12345,5678" | |
// | |
// You can also set this up such that the cookie value is dynamic and | |
// controlled in the ad request. Similar to the cookie name, your ad tag | |
// would look like: | |
// <iframe src="http://example.com/w/1.0/afr?auid=1234&cookiename=mycookiename&cookievalue=12345"... | |
// | |
// Again, you would use the macro for the custom variable: | |
// value: '{c.cookievalue}', | |
// And in our example the macro would be expanded to: | |
// value: '12345', | |
value: '12345', | |
// Set how many days the cookie will live on its own if it isn't | |
// explicitly removed. Set to a negative amount to 'delete' cookie. You | |
// can set whatever interval you want. | |
daysToLive: 7 | |
} | |
// Since we're setting a list of possible values we need to make sure to add | |
// to any existing values. | |
cookieMatch = document.cookie.match(RegExp(cookie.name + '=(\\S+);*')); | |
if (cookieMatch) { | |
var vals = cookieMatch[1].split(cookieValueDelimiter) | |
, valsCount = vals.length; | |
while (valsCount--) { | |
// Return if the value is already included in our list. | |
if (vals[valsCount] == cookie.value) { return; } | |
} | |
// Update our cookie value with any existing values. | |
vals.push(cookie.value); | |
cookie.value = vals.join(cookieValueDelimiter); | |
} | |
// Create a date string corresponding to the days the cookie should 'live'. | |
date = new Date(); | |
date.setDate(date.getDate() + cookie.daysToLive); | |
date = date.toUTCString(); | |
// Set up the cookie string. | |
cookieString = [ | |
cookie.name + '=' + cookie.value, | |
'expires=' + date, | |
'path=/' | |
].join(';'); | |
document.cookie = cookieString; | |
})(); |
This file contains 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
/* | |
* Setting a targetable cookie (custom variable) whose value is a string. | |
*/ | |
(function(){ | |
var cookie | |
, cookieString | |
, date; | |
cookie = { | |
// Name of the cookie to set. Remember that this will be what you target | |
// in the UI as a custom variable. You can also set this up such that the | |
// cookie name is dynamic and controlled in the ad request. Your ad tag | |
// would look similar to: | |
// <iframe src="http://example.com/w/1.0/afr?auid=1234&cookiename=mycookiename"... | |
// | |
// You would use the macro for the custom variable: | |
// name: '{c.cookiename}', | |
// And in our example the macro would be expanded to: | |
// name: 'mycookiename', | |
name: 'mycookiename', | |
// Value of the cookie. If you set this to something like 'true' you | |
// need want to set up a line item to prevent serving if 'true', set up your | |
// targeting to read: | |
// mycookiename <string op> does not equal "true" | |
// | |
// You can also set this up such that the cookie value is dynamic and | |
// controlled in the ad request. Similar to the cookie name, your ad tag | |
// would look like: | |
// <iframe src="http://example.com/w/1.0/afr?auid=1234&cookiename=mycookiename&cookievalue=true"... | |
// | |
// Again, you would use the macro for the custom variable: | |
// value: '{c.cookievalue}', | |
// And in our example the macro would be expanded to: | |
// value: 'true', | |
value: 'true', | |
// Set how many days the cookie will live on its own if it isn't | |
// explicitly removed. Set to a negative amount to 'delete' cookie. You | |
// can set whatever interval you want. | |
daysToLive: 7 | |
} | |
// Create a date string corresponding to the days the cookie should 'live'. | |
date = new Date(); | |
date.setDate(date.getDate() + cookie.daysToLive); | |
date = date.toUTCString(); | |
// Set up the cookie string. | |
cookieString = [ | |
cookie.name + '=' + cookie.value, | |
'expires=' + date, | |
'path=/' | |
].join(';'); | |
document.cookie = cookieString; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment