Skip to main content
Set Custom Properties Based on Cookie Values
Updated over a month ago

Using the following method, you can easily send cookie values set on your website to Wisepops as custom properties.
โ€‹

Helper to read cookies

First of all, we want to be able to read values inside cookies in JavaScript. Various solutions exists, we will use the one described in this post.

function getCookieValue(a) {
var b = document.cookie.match('(^|[^;]+)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
}

Map cookies to custom properties

You can now use the helper function to store the cookie values you are interested in as Wisepops custom properties.

wisepops('properties', {
cookie1: getCookieValue('cookie1'),
cookie2: getCookieValue('cookie2'),
cookie3: getCookieValue('cookie3')
});

With this snippet, we have read three different cookies, named cookie1 , cookie2 and cookie3 , and set their values in custom properties with the same name.
โ€‹

Place these two snippets just after the Wisepops setup code

You should end up with a script that looks like this:

<script type="text/javascript" data-cfasync="false">
// Wisepops setup code
(function(W,i,s,e,P,o,p){W['WisePopsObject']=P;W[P]=W[P]||function(){
(W[P].q=W[P].q||[]).push(arguments)},W[P].l=1*new Date();o=i.createElement(s),
p=i.getElementsByTagName(s)[0];o.async=1;o.src=e;p.parentNode.insertBefore(o,p)
})(window,document,'script','//loader.wisepops.com/get-loader.js?v=1&site=XXXXXXXXXX','wisepops');

// Set custom properties from cookies
function getCookieValue(a) {
var b = document.cookie.match('(^|[^;]+)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
}
wisepops('properties', {
cookie1: getCookieValue('cookie1'),
cookie2: getCookieValue('cookie2'),
cookie3: getCookieValue('cookie3')
});
</script>

If you need some help setting this up, get in touch via chat.

Did this answer your question?