Implementing a Custom Google Tag Manager Trigger for Form Submission Tracking within an iFrame
In the dynamic world of digital marketing, tracking user interactions on websites is crucial for analyzing performance and optimizing strategies. Recently, I faced an interesting challenge: a client needed to track the form submissions occurring within an iframe on a third-party website to which they had no direct access. This post details the steps I took to solve this problem using Google Tag Manager (GTM).
The Challenge
The client attempted to use the standard “Element Visibility” trigger in GTM. However, this approach failed because it could not detect elements within the iframe. After some investigation, I discovered that the form submission within the iframe sent a specific postMessage with data.code = "success" upon successful form submission. This provided the necessary hook for our tracking solution.
Discovering the PostMessage
To identify the specific postMessage used by the iframe, I used Chrome’s Inspector tool. Here’s how I did it:
- Open Chrome Inspector: Right-click on the page and select “Inspect” or press
Ctrl+Shift+Ito open Chrome Developer Tools. - Access the Console: Navigate to the “Console” tab within Developer Tools.
- Add an Event Listener: To capture all
postMessageevents, I added the following script in the console:window.addEventListener('message', console.log);This script logs all incomingpostMessageevents to the console. - Submit the Form: I then interacted with the form within the iframe and observed the console to identify the
postMessagecontainingdata.code = "success".
Using this method, I discovered that the form submission sent a postMessage with the key information needed to create a custom event in GTM.
The Solution
To capture the form submission event, I implemented a custom HTML tag in GTM that listens for the specific postMessage from the iframe. Here is the step-by-step process:
- Identify the PostMessage: Upon form submission, the iframe sends a
postMessagecontainingdata.code = "success". We can leverage this message to trigger our custom event in GTM. - Create a Custom HTML Tag: Add a new tag in GTM of type “Custom HTML” with the following script:
<script> window.addEventListener('message', function(event) { if (event.origin === "https://converto.simplebooking.it" && event.data.code === "success") { window.dataLayer.push({ "event": "simplebooking_success" }); } }); </script>This script listens forpostMessageevents. When it detects a message from the specific originhttps://converto.simplebooking.itwithdata.codeequal to “success”, it pushes a custom event (simplebooking_success) to the Data Layer. - Configure the Custom HTML Tag:
- Trigger: Set the trigger to “All Pages” so that the script runs on every page where the iframe might be loaded.
- Tag Firing Priority: Ensure this tag fires before any related conversion tags to capture the event accurately.
- Create a Custom Event Trigger: To react to the
simplebooking_successevent, create a new trigger:- Trigger Type: Custom Event
- Event Name:
simplebooking_success - This trigger fires on: All Custom Events
- Link Conversion Tags: With the custom event trigger in place, you can now link your conversion tags (e.g., for Facebook and Google) to this trigger. This setup ensures that the conversion tags fire when the form submission event is detected.
Final Setup
After implementing the above steps, your GTM setup should look something like this:
- Custom HTML Tag: Contains the
postMessagelistener script. - Custom Event Trigger: Named
simplebooking_success. - Conversion Tags: Configured to fire on the
simplebooking_successevent.
Testing and Verification
- Preview Mode: Use GTM’s Preview mode to test the implementation. Submit the form within the iframe and check if the
simplebooking_successevent appears in the GTM Debug console. - Browser Console: Open the browser console and manually trigger the
postMessageevent to ensure the script correctly pushes the event to the Data Layer. - Verify Tag Firing: Ensure that the conversion tags fire as expected when the
simplebooking_successevent is detected.
By following these steps, you can successfully track form submissions within an iframe using Google Tag Manager, even when you do not have direct access to the iframe’s content. This approach ensures that you can capture valuable conversion data and optimize your marketing efforts effectively.