How to collect respondent cookies using attributes tracking

You might want to collect cookie data as attributes. It's safe & secure feature that will still allow you to collect respondent's cookie values and attach them to CSV or XLS export.

Case 1: If you already have the attribute tracking code

If you have the tracking code added to track users' attribute looking like this:

<script type="text/javascript">
(function(opts) {
opts.traits = {
"user_id": user.id,
"plan": user.plan
};
})(window._sva = window._sva || {});
</script>

Then all you need to do is to add a trait to this code, and pass cookie value to it. A sample cookie tracking code looks like this:

 "CookieName": document.cookie.replace(/(?:(?:^|.*;\s*)CookieName\s*\=\s*([^;]*).*$)|^.*$/, "$1")

Where "CookieName" is the attribute name that's filled with cookie value using regexp. Please change both to the name of the cookie.

So your final code should resemble this one:

<script type="text/javascript">
(function(opts) {
opts.traits = {
"user_id": user.id,
"plan": user.plan,
"CookieName": document.cookie.replace(/(?:(?:^|.*;\s*)CookieName\s*\=\s*([^;]*).*$)|^.*$/, "$1") //change both CookieName occurences to the name of the cookie you want to track.
};
})(window._sva = window._sva || {});
</script>

Of course, you can track multiple cookies at once:

<script type="text/javascript">
(function(opts) {
opts.traits = {
user_id": user.id,
"plan": user.plan,
"cookie1": document.cookie.replace(/(?:(?:^|.*;\s*)cookie1\s*\=\s*([^;]*).*$)|^.*$/, "$1"),
"cookie2": document.cookie.replace(/(?:(?:^|.*;\s*)cookie2\s*\=\s*([^;]*).*$)|^.*$/, "$1")
};
})(window._sva = window._sva || {});
</script>

Case 2: You don't track users' attributes yet

If you don't have the code added to track users' attribute looking like this:

<script type="text/javascript">
(function(opts) {
opts.traits = {
"user_id": user.id,
"plan": user.plan
};
})(window._sva = window._sva || {});
</script>

Then all you need to do, is modify the code above and add it to your website just before Survicate tracking code:

<script type="text/javascript">
(function(opts) {
opts.traits = {
"CookieName": document.cookie.replace(/(?:(?:^|.*;\s*)CookieName\s*\=\s*([^;]*).*$)|^.*$/, "$1") //change both CookieName occurences to the name of the cookie you want to track.
};
})(window._sva = window._sva || {});
</script>

It allows you to send some attributes along with the responses when someone answers a survey. "CookieName" is the attribute name that's filled with cookie value using regexp.

Of course, you can track multiple cookies at once.

<script type="text/javascript">
(function(opts) {
opts.traits = {
user_id": user.id,
"plan": user.plan,
"cookie1": document.cookie.replace(/(?:(?:^|.*;\s*)cookie1\s*\=\s*([^;]*).*$)|^.*$/, "$1"),
"cookie2": document.cookie.replace(/(?:(?:^|.*;\s*)cookie2\s*\=\s*([^;]*).*$)|^.*$/, "$1")
};
})(window._sva = window._sva || {});
</script>


Do you find this helpful?


Powered by HelpDocs