Other Use Cases for Retrieving Envelopes
Learn other ways you can retrieve envelopes in different scenarios that are applicable to your use case.
Retrieving Envelopes From an Iframe
You can call for envelopes from within an iframe (or nested iframe) using postMessage. ATS needs to be included in host site (parent) where iframe will be placed. Publisher needs to add this code to the iframe html page, in order to get envelope in that iframe.
As an example, implementing the following will request an envelope from the parent.
IFrame Envelope Request:
function dispatchEnvelopeRequestEvent() { try{ top.postMessage('ats-modules-liveramp-envelope-request','*'); }catch(e){ if(typeof(top.contentWindow) != 'undefined') { top.contentWindow.postMessage('ats-modules-liveramp-envelope-request','*'); } } }
You will then need to receive and process the result from the parent.
Note
If you retrieve an envelope with this method, you should expect a response when:
ATS is loaded and there is an envelope, in which case the response contains an envelope.
ATS is loaded but there is no envelope, in which case the response is
undefined
.
If ATS is not on the page or not yet loaded, no response will be returned from ATS.
Envelope response:
function receiveEnvelope(event) { if(event && event.data && event.data.message === 'ats-modules-liveramp-envelope-result'){ console.log(event.data.result,event.origin); } } window.addEventListener('message', receiveEnvelope, false);
Retrieving Envelopes Before ATS is Loaded
function getEnvelopeFromLocalStorage() { if (localStorage.getItem("_lr_env") !== null) { return JSON.parse(atob(localStorage.getItem("_lr_env"))) } } function getEnvelopeFromCookie() { let cookieName = "_lr_env"; let name = cookieName + "="; let attr = document.cookie.split(';'); for (let i = 0; i < attr.length; i++) { let c = attr[i].trim(); if (c.indexOf(name) === 0) { return JSON.parse(atob(decodeURIComponent(decodeURIComponent(c.substring(name.length, c.length))))); } } return ""; }
Caution
Use this piece of code with caution since the envelope might be expired and since ATS is not yet loaded there is no way of knowing if that is indeed the case.