Tracking iframe Events with Google Tag Manager

The <iframe> HTML element represents a nested browsing context, embedding another HTML page into the current one.

Each embedded browsing context has its own session history and document. The browsing context that embeds the others is called the parent browsing context. The topmost browsing context—the one with no parent—is usually the browser window. See Mozilla’s iframe page for more information.

Tracking specific iframe events on a page with a universal pixel in GTM involves six steps:

  1. Verify that the same GTM container is installed in both the parent page and the child iframe.

  2. Create the iframe Listener tag.

  3. Push the custom event on the child iframe form submission.

  4. Trigger the custom event tag.

  5. Build the amended universal pixel and ruleset in Basis.

  6. Create the tag and trigger for the universal pixel in GTM.

In this scenario, you'll track Submit at the end of this iframe event:

The Submit button, which is highlighted with a red border, at the end of this iframe event that you'll be tracking

1. Verify That the Same GTM Container Is Installed in Both the Parent Page and the Child iframe.

Before creating an iframe Listener tag, install your GTM Container in both the parent page and the child iframe. See Google’s Tag Manager page for more information.

2. Create an iframe Listener Tag

You must install an iframe listener tag for the parent page to recognize events happening in the child iframe. The following script should be implemented across all pages and can be used without alteration:

Copy
<script type="text/javascript">
                (function(window) {
                addEvent(window, 'message', function(message) {
                try{
                var data = JSON.parse(message.data);
                var dataLayer = window.dataLayer || (window.dataLayer = []);
                if (data.event) {
                dataLayer.push({
                'event': data.event,
                'postMessageData': data
                });
                }
                }catch(e){}
                });
                 
                //Cross-Browser event listener
                function addEvent(el, evt, fn){
                if (el.addEventListener) {
                el.addEventListener(evt, fn);
                } else if (el.attachEvent){
                el.attachEvent('on' + evt, function(evt) {
                fn.call(el, evt);
                });
                } else if (typeof el['on' + evt] === 'undefined' || el['on' + evt] === null) {
                el['on' + evt] = function(evt) {
                fn.call(el, evt);
                };
                }
                }
                })(window);
            </script>

3. Push a Custom Event on the Child iframe Form Submission

Use the following script to push a custom event on the parent page that’s triggered on the child iframe form submission:

The script code used to push a custom event on the parent page. The red placeholders are capital letters

Update the "event:," "form;," and "postObject;" variables accordingly:

The script code with updated variables (green)

4. Triggering a Custom Event Tag

First, identify the triggering variables that uniquely cause the Form Fill in the iframe to fire.

5. Build the Amended Universal Pixel and Rule Set in Basis

Universal pixels can act as conversion pixels and audience pixels on multiple pages at once. This lets you use a single pixel on different pages on your website, and even place that pixel before your conversions or audiences are set up. See Universal Pixels for more information.

To track this iframe event with a universal pixel, you must use the Key/String rule set in Basis.

6. Create a Tag and Trigger for your Universal Pixel in GTM

After you install your GTM Container on all pages of your site, create a tag. A tag is made up of two components: tag configuration and triggering. Tags are segments of code provided by analytics, marketing, and support vendors to help you integrate their products into your websites or mobile apps. With GTM, you no longer need to add these tags directly to your projects. You configure and publish tags from within GTM. See Google’s About Tags page for more information.

QA – How to Check if the Universal Pixel Is Firing in Chrome

After the GTM container has been published, check if the universal pixel is firing on all pages correctly in a live environment.

To check if your universal pixel is firing in Chrome:

  1. In Google Chrome, select Chrome’s Menu icon > More tools > Developer tools (Ctrl+Shift+I) on the page being tracked to open the Developer tools panel.

  2. Select the Network tab.

  3. Search for the key value, iframe=submit in this case, then fill and submit the form to see if the universal pixel fires in real time.

    The iframe form's Submit button and Chrome's Developer tool's Network tab. The Submit button, key value, and submission event are highlighted with red borders