tracking iframes on multiple URLs in google tag manager

Issue

I have a script I’m using to track a form in an iframe on a specific page in Google Tag Manager. My question is how do I modify it so I can track it across multiple pages? Do I need to duplicate the tag and trigger or do I need to modify the script?

<script>
try {
var postObject = JSON.stringify({
event: 'GAEvent',
eventCategory: 'Form',
eventAction: 'Form Submission',
eventLabel: 'Form'
});
parent.postMessage(postObject, 'https://www.example.com/page');
} catch(e) {
window.console && window.console.log(e);
}
</script>

GTM Setup as follows:

Solution

The second argument of window.postMessage() is the URL of the parent. It’s made to avoid firing the message for parents that shouldn’t get the messages. If you want every parent to get the message regardless of the parent URL, the "*" would be the option to go with.

More information on postMessage is here: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

Looks like it’s not much related to GTM, but more to the mechanics of postMessage.

Answered By – BNazaruk

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.