Skip to content

Instantly share code, notes, and snippets.

@hyebahi-trustarc
Last active March 8, 2024 14:11
Show Gist options
  • Save hyebahi-trustarc/4e6cac855217ebe7beb0537915130e5f to your computer and use it in GitHub Desktop.
Save hyebahi-trustarc/4e6cac855217ebe7beb0537915130e5f to your computer and use it in GitHub Desktop.
CMP - Generic Event Listener
window.addEventListener("message", (event) => {
let eventDataJson = null;
// We only care about TrustArc Events at this point. And TrustArc's even it encoded in JSON
try {
eventDataJson = JSON.parse(event.data);
} catch {
// Some other event that is not JSON.
// TrustArc encodes the data as JSON
// console.log(event.data);
}
// Safeguard to make sure we are only getting events from TrustArc
if (eventDataJson && eventDataJson.source === "preference_manager") {
// Means that the user has submited their preferences
if (eventDataJson.message === "submit_preferences") {
// Reload the page
}
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment