Using Cookies to Identify Visitors | Yext Hitchhikers Platform

Cookies are a popular method for identifying visitors that come to your site. Cookies allow you to assign a unique ID to a user which can be used to identify a visitor each time they visit your search experience. Note that depending on your local regulations (e.g., GDPR), you may be required to request consent from your users before using a cookie to track them.

Oftentimes you will already have your own cookie you’re using to identify visitors but if not then you can easily generate one yourself.

If you’re already generating a cookie, you’ll need to retrieve the value of that cookie (your visitor ID) so you can use it for attribution with your searches and clicks.

Below is an example of how you can do this in JavaScript:

function getCookie(cookie_name) {
  let name = cookie_name + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(";");
  for (let i = 0; i < ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == " ") {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

This function will take the name of a cookie provided by you and return the value for that cookie.

For example, if a cookie was present on a visitor’s browser called _yfpc with the following value 1404629222047, this could be retrieved as follows:

function getCookie(cookie_name) {
  let name = cookie_name + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(";");
  for (let i = 0; i < ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == " ") {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

getCookie("_yfpc")

If you are getting your visitor ID from a cookie, the visitor object you create may look like:

var visitor = {
   "id": getCookie("_yfpc");
   "idMethod": "YEXT_CONVERSION_TRACKING_COOKIE"
 };

If you’d like to generate your own cookie to identify visitors there are two important steps in doing so:

  1. Generate a unique ID for your visitor
  2. Generate a cookie using that unique ID

Generate a Unique ID for your Visitor

If you’d like to generate a cookie the first step is creating a unique ID which can be associated with your visitor.

You can do so by generating what is called a UUID (universally unique identifier) which you can associate with your visitor.

Below is an example of how you can create a UUID in JavaScript:

function create_UUID() {
  var dt = new Date().getTime();
  var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (
    c
  ) {
    var r = (dt + Math.random() * 16) % 16 | 0;
    dt = Math.floor(dt / 16);
    return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
  });
  return uuid;
}

Once you’ve generated a unique ID, you’ll then use this ID to generate a cookie to track your visitor.

Before you generate a new cookie, the first thing you’ll want to do is check to see if a cookie already exists for this visitor (e.g. this visitor has already visited your site and had a cookie generated for them). If no cookie has been generated, either because this is a new visitor or because the visitor has cleared their cookies you’ll then generate a new cookie.

You can do so with the following JavaScript code:

function generateCookie(cookie_name) {
  let cookie = getCookie(cookie_name);
  if (cookie == "") {
    let cookie_id = create_UUID();
    let cookie_expiration = 365;

    const d = new Date();
    d.setTime(d.getTime() + cookie_expiration * 24 * 60 * 60 * 1000);
    let expires = ";expires=" + d.toUTCString();
    document.cookie =
      cookie_name +
      "=" +
      cookie_id +
      ";" +
      cookie_expiration +
      ";path=/" +
      expires;
  }
}

This function will take the name of a cookie provided by you and generate a new cookie, which will expire in 365 days, if one does not already exist.

Feedback