5. Store the Identity Envelope With a Timestamp
When you successfully retrieve an Identity Envelope, store it with a timestamp so you can refresh it when needed. Where you store it depends on your implementation method:
- Server-side: Store the envelope and track its lifecycle on your backend with a method of your choice.
- Browser-side: Store the envelope with a timestamp in a first-party cookie or local storage using LiveRamp keys.
Envelope Expiration Times
To be GDPR and GPP compliant, envelopes are generated with a 15-day TTL ("Time to Live" / expiration time) for California (U.S.) and a 30-day TTL for all other countries and regions.
Store a timestamp next to the envelope to avoid passing expired envelopes that cannot be decrypted by the SideCar (an API that is implemented on the SSP side to read and decrypt envelopes) or other envelope APIs. With timestamps in place, you can more easily determine if the expiration time has passed and refresh the envelope if needed.
Serve-Side Storage
For a server-side ATS API integration, your backend owns envelope retrievals and refreshes. Store envelopes with explicit lifecycle metadata to ensure only the current eligible envelope are made available to downstream partners.
A practical serve-side record could look like the following:
{
"envelope": "<envelope_value>",
"envelopeType": "<envelope_type>",
"issuedAt": "<timestamp>",
"expiresAt": "<timestamp>",
"lastRefreshTime": "<timestamp>",
"userSessionKey": "<userSession_reference>",
"privacyStateVersion": "<privacy_state_version>"
}
- Store the envelope value with an expiration timestamp based on the user's region.
- Set the expiration timestamp to the time you received the envelope plus the lifetime for the user's region.
- Associate the envelope value and the timestamp with the correct user/session.
After a successful refresh, replace both the value and the timestamp. For more information, see "7. Implement the ATS Refresh Envelope API".
Browser-Side Storage
In a browser-side implementation, the browser calls the ATS API and stores the envelope in a first-party cookie or local storage.
Encode The Entire Envelope API Response
When you successfully retrieve an Identity Envelope from the Envelope API, the Envelope API response will look like:
{"envelopes":[{"type":19,"source":"envelopeLiveramp","value":"Aqs3wtOuqIRic78_s4Nqilcr0MGn0cDvKzvpEhSGVjwV8Ci0Jy73B1bkfpv03GwK2uyKbME4kSAtVFn_5sCLWstyHTnEWYurbjLXLpgtf9MfLP1AeDTUQ_0ESnD3x1pr6gYfBOOUH8BPhbfscxdaGUA-_sacPab3nzc05koLhbqrJIQC7JBraEbpOJPDAHp9f44DXIXm","err":null},{"type":25,"source":"pairIds","value":"WyJBeVhiNUF0dmsvVS8xQ1d2ejJuRVk5aFl4T1g3TVFPUTJVQk1BMFdiV1ZFbSJd","err":null}]}The response will contain at least one LiveRamp envelope (within "envelopeLiveramp" object) and possibly other types of online identifiers such as PAIR ID as shown in the above example. Before storing the LiveRamp envelope response, you must modify the response so it contains an envelope object.
The result should look like this:
{"envelope":"Aqs3wtOuqIRic78_s4Nqilcr0MGn0cDvKzvpEhSGVjwV8Ci0Jy73B1bkfpv03GwK2uyKbME4kSAtVFn_5sCLWstyHTnEWYurbjLXLpgtf9MfLP1AeDTUQ_0ESnD3x1pr6gYfBOOUH8BPhbfscxdaGUA-_sacPab3nzc05koLhbqrJIQC7JBraEbpOJPDAHp9f44DXIXm"}As a final step, you need to Base64 encode the entire JSON object as shown above (not just the value of the envelope).
After it is encoded into Base64, the result will look like:
eyJlbnZlbG9wZSI6IkFxczN3dE91cUlSaWM3OF9zNE5xaWxjcjBNR24wY0R2S3p2cEVoU0dWandWOENpMEp5NzNCMWJrZnB2MDNHd0sydXlLYk1FNGtTQXRWRm5fNXNDTFdzdHlIVG5FV1l1cmJqTFhMcGd0ZjlNZkxQMUFlRFRVUV8wRVNuRDN4MXByNmdZZkJPT1VIOEJQaGJmc2N4ZGFHVUEtX3NhY1BhYjNuemMwNWtvTGhicXJKSVFDN0pCcmFFYnBPSlBEQUhwOWY0NERYSVhtIn0=The Base64 encoded envelope value can then be stored in a first-party cookie or local storage. Different cookie and storage keys are used depending on the type of envelope/identifiers you receive.
Storage Keys
Use the following keys based on the type of identifiers (or segments in the case of ATS Direct) you want to store. If you are working with CAPI products (with the exception of Meta CAPI), you only need to store the identity envelope.
- Identity envelope:
_lr_env - Meta envelope:
_lr_fb_env - PAIR ID:
_lr_pairId - ATS Direct:
_lr_atsDirect - Google SSP:
_lr_google_env
Storing the Envelope in a First-Party Cookie
function setCookieForRampIDEnvelope(envelope) {
document.cookie = '_lr_env=' + encodeURIComponent(envelope);
}Storing the Envelope in Local Storage
function setLocalStorageForRampIDEnvelope(envelope) {
localStorage.setItem('_lr_env' , envelope);
}Storing the Envelope TTL
The following examples set a timestamp for first-party cookies and for local storage. Set the correct expiration time based on the location of the user. Each time you refresh the envelope, you will must update the expiration time or set a new one.
In the example below, we use the first party cookie _lr_env_exp and the local storage key name _lr_env_exp:
function calculateEnvelopeTTL(countryCode, regionCode) {
let expirationTime = 2592000000;
if (countryCode === 'US') {
if (regionCode === 'CA') {
expirationTime = 1296000000;
}
}
expirationTime += +new Date();
return expirationTime;
}
// Example: set a cookie to track envelope expiration in 15 days
document.cookie = '_lr_env_exp=' + calculateEnvelopeTTL('US', 'CA');
// Example: set a cookie to track envelope expiration in 30 days
document.cookie = '_lr_env_exp=' + calculateEnvelopeTTL();
// Example: set localStorage to track envelope expiration for 15 days
localStorage.setItem('_lr_env_exp' , calculateEnvelopeTTL('US', 'CA'));
// Example: set localStorage to track envelope expiration for 30 days
localStorage.setItem('_lr_env_exp' , calculateEnvelopeTTL());You can then read out the appropriate timestamp like so for cookies:
// Example helper function to get cookie by name
function getCookieValue(cookieName) {
cookieName += "=";
var tCookieArr = document.cookie.split(';');
for (var i = 0; i < tCookieArr.length; i++) {
var tCookie = tCookieArr[i].trim();
if (tCookie.indexOf(cookieName) === 0) {
var cookieVal = tCookie.substring(cookieName.length, tCookie.length)
return decodeURIComponent(cookieVal);
}
}
return "";
}
getCookieValue("_lr_env_exp");
/* Then, call the Envelope Refresh API as required */And for local storage:
localStorage.getItem("_lr_env_exp");
/* Then, call the Envelope Refresh API as required */Updated 2 days ago