Skip to content

Instantly share code, notes, and snippets.

@stefanatcroud
Forked from shadybones/CMAPI_Integration.js
Last active November 24, 2023 11:07
Show Gist options
  • Save stefanatcroud/cd103ecfabf7dbe37d6a13d01557775f to your computer and use it in GitHub Desktop.
Save stefanatcroud/cd103ecfabf7dbe37d6a13d01557775f to your computer and use it in GitHub Desktop.
Enable TrustArc integrations. Sends a "TrustArc Consent" data layer event, which contains "trustarc.consent" as a string indicating each consent status, much like how OneTrust reports consent updates.
<script>
var __i__ = self.postMessage && setInterval(function () {
if (self.PrivacyManagerAPI && __i__) {
var apiObject = {
PrivacyManagerAPI:
{
action: "getConsentDecision",
timestamp: new Date().getTime(),
self: self.location.host
}
};
self.top.postMessage(JSON.stringify(apiObject), "*");
__i__ = clearInterval(__i__);
}
}, 50);
self.addEventListener("message", function (e, d) {
try {
if (e.data && (d = JSON.parse(e.data)) && (d = d.PrivacyManagerAPI) && d.capabilities && d.action == "getConsentDecision") {
var newDecision = self.PrivacyManagerAPI.callApi("getGDPRConsentDecision", self.location.host).consentDecision.toString();
self.dataLayer && self.dataLayer.push(
newDecision==="0" ? {"event": "TrustArc Pre-Consent"}
: {"event": "TrustArc Consent", "trustarc": {"consent": newDecision}}
);
}
} catch (xx) { /** not a cm api message **/ }
});
</script>
@stefanatcroud
Copy link
Author

The idea of this gist is to send a single data layer push containing the consent agreed as metadata within the push.

image

@chieldeleeuw
Copy link

Amazing, just what I needed as well.

Thanks for this 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment