GTM and external iframe

postMessage and dataLayer

Pushing dataLayer object from iframe on third party domain to the GTM on the parent site.

Sample dataLayer used:

 'event': 'buttonClick',
 'Button clicked': 'A'

In this example dataLayer object is pushed from iframe to parent (this article).

Parent (this page)

window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
 if (event.origin == "") {

<iframe src=""></iframe>

The JavaScript above could be more conveniently delivered as a Custom HTML tag in GTM.

Iframe (from other domain)

function dlPushToParent(target,object) {
 if(target = new URL((parent !== window) ? document.referrer : document.location).origin) {

<button onclick="dlPushToParent('', {'event': 'buttonClick','Button clicked': 'A'});">Button A</button>
<button onclick="dlPushToParent('', {'event': 'buttonClick','Button clicked': 'B'});">Button B</button>
<button onclick="dlPushToParent('', {'event': 'buttonClick','Button clicked': 'C'});">Button C</button>