Skip to main content
All CollectionsWebsite surveysMake the most out of website surveys
How to collect respondent cookies using attributes tracking
How to collect respondent cookies using attributes tracking

This article will guide you through the steps of collecting respondent cookie data as attributes

Agnieszka avatar
Written by Agnieszka
Updated over 8 months ago

You can collect cookie data as attributes. It's a safe & secure feature that will allow you to:

  • collect respondents' cookie values;

  • see them in CSV or XLSX exports next to the respondent and their answers,

  • use them to filter responses of the survey.

In this article, you'll learn how to collect respondent cookie values that can later be used for analysis or for targeting a survey to your chosen audience.

Before implementing code that allows collecting respondent cookies, please ensure you have the Survicate tracking code installed on your website.

Case 1: You already have the attribute tracking code

1. If you have the tracking code added to track users' attributes similar to this one:

<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 the 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>


2. 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>

❗️Your developer has to adjust the code in order to work correctly. We have prepared developers' documentation, which will be helpful for your team to set it up.

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

1. If you don't have the code added to track users' attributes, that looks like:

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


2. Then, your development team will have to modify the code above and add it to your website just before the 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 filled with cookie value using regexp.

❗️Please note that you will have to adjust the code to your website, so it works properly. We have prepared developers' documentation, which will be useful for your team to set up.

3. 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>

Best practices

Target your surveys based on cookies

Once the respondent cookies are collected, you can use cookie-based targeting to trigger surveys only to a particular group of visitors.

Define other filters in your Audience

Show the survey only to users from a chosen visit source, visitors speaking a particular language, or using a mobile or desktop device. Learn how to achieve this here.

📞 If you have any questions about collecting respondent cookies, please reach out to us at support@survicate.com or strike up a chat conversation 👉

Did this answer your question?