6. Return Stored Identity Envelopes

For troubleshooting purposes and as required by vendors, such as Prebid.js, you will need to implement a mock function and expose it where you are resolving users to envelopes, preferably on every page.

Other vendors on the publishers page should be able to make a call to window.ats.retrieveEnvelope() and receive a JSON string that contains the envelope, e.g.:

{timestamp":1702647333235,"version":"1.3.0","envelope":"Aqs3wtOuqIRic78_s4Nqilcr0MGn0cDvKzvpEhSGVjwV8Ci0Jy73B1bkfpv03GwK2uyKbME4kSAtVFn_5sCLWstyHTnEWYurbjLXLpgtf9MfLP1AeDTUQ_0ESnD3x1pr6gYfBOOUH8BPhbfscxdaGUA-_sacPab3nzc05koLhbqrJIQC7JBraEbpOJPDAHp9f44DXIXm"}

If no envelope is available, it will return as undefined.

📘

If the function is called without a callback, a promise will be returned. If the function is called with a callback, an envelope value will be returned to your callback.

In a server-side ATS implementation, your backend stores the envelope. If a browser-based integration needs the envelope, make the current user’s envelope available to that integration on your page.

  • Define window.ats.retrieveEnvelope on your pages and support the calling convention the integration expects.
  • For integrations that use the callback form and expect a JSON string (e.g. Prebid.js), call callback(JSON.stringify({envelope})) when an is available for the current user. Do not pass the raw value or a Base64-encoded wrapper directly to the callback.
  • If no eligible envelope is available, call callback(undefined).

Return an Identity Envelope From a First-Party Cookie

Assuming your Identity Envelope is stored in the first-party cookie _lr_env, the below function will look up the envelope (if it exists) and return it to the caller.

📘

When calling the functions below, the returned envelopes will be enclosed with additional single quotation marks (') around them. This is due to the stringify function which turns the string into a JSON object.

function retrieveEnvelope(callback) {
  let env = undefined;
  try {
    env =
 decodeURIComponent(document.cookie.match('(^|;) *_lr_env=([^;]*)')[2]);
      env = {
          'envelope': JSON.parse(atob(env)).envelope
      };
env = JSON.stringify(env);
  } catch (e) {
    console.log(e);
  } finally {
    if(callback){
      callback(env);
    } else {
      return env;
    }
  }
}
window['ats'] = {};
window.ats.retrieveEnvelope = retrieveEnvelope;

Return an Identity Envelope From Local Storage

Assuming your identity envelope is stored in local storage under key _lr_env, the below code snippet will look up the envelope (if it exists) and return it to the caller.

Note that the returned envelopes will be enclosed with additional single quotation marks around them.

function getItem(name) {
  if (localStorage.hasOwnProperty(name)) {
    return localStorage.getItem(name);
  }
}

function getCookie(cname) {
  const name = cname + '=';
  const attr = document.cookie.split(';');

  // Loop through the cookies and return the cookie value if it find the cookie name
  for (let i = 0; i < attr.length; i++) {
    const c = attr[i].trim();
    // If the name is the cookie string at position 0, we found the cookie and return the cookie value
    if (c.indexOf(name) === 0) return decodeURIComponent(c.substring(name.length, c.length));
  }

  // If we get to this point, that means the cookie wasn't found and we return an empty string.
  return '';
}

function retrieveEnvelope() {
let rawLiveRampEnvelope = getItem('_lr_env') || getCookie('_lr_env');
if(rawLiveRampEnvelope) {
	return JSON.parse(window.atob(rawLiveRampEnvelope))
}
  return undefined;
}

window['ats'] = {}
window.ats.retrieveEnvelope = retrieveEnvelope


Did this page help you?