Tracking iframe done right

Written by

Alex Held

Updated Jul 9, 2025 comments

Detect iframe

Detect iframe

Track events in iframe

1. Web API

2. Add GTM to the iframe

Detect iframe
Detect iframe

Send data from iframe to parent window

1. Limit the work of tags so that they do not trigger in an iframe

Detect iframe

2. Configure tracking of required events in an iframe

Detect iframe
<script type="text/javascript">   window.addEventListener("message", function(event) {     if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {       window.dataLayer.push({         'event': 'hubspot-form-success',         'form_data': {           'form_id': event.data.id,           'first_name': event.data.data.submissionValues.firstname,           'last_name': event.data.data.submissionValues.lastname,           'email': event.data.data.submissionValues.email,           'phone': event.data.data.submissionValues.phone,         }       });     }   }); </script>
Detect iframe
Detect iframe

3. Pass the event to the parent window

Custom HTML tag script - data to send
<script> // Data to be sent to the parent window var dataToSend = {     event: 'hubspot_form',     data: {{DLV - form_data}}, }; // Send message to parent window window.parent.postMessage(dataToSend, 'https://www.your-parent-domain.com');  // Specify the parent's domain for security reasons. You can use '*' to send data to any domain, but this is not recomended by security reason. </script>
Custom HTML tag script - data to send
<script> window.addEventListener('message', function(event) { // Check if the message came from a trusted source (for security reasons). Specify your iframe domain    if (event.origin !== 'https://iframe.filesusr.com') {        return;     }     var data = event.data; // Map data and push it to data layer     dataLayer.push({       event: 'hubspot_form_submitted',       form_data: {         form_id: data.data.form_id,         first_name: data.data.first_name,         last_name: data.data.last_name,         email: data.data.email,         phone: data.data.phone       }     }) }); </script>
Custom HTML tag script - data to send

4. Testing

Custom HTML tag script - data to send

Final thoughts

Want to start using server-side tags?

Stape has lots of tags for server GTM! Click on Try for free to register and check them all.

Try for free

Written by

Alex Held

Content writer

Alex, a tracking expert, specializes in first-party data, enrichment, and measurement. With GTM and sGTM expertise, he helps businesses enhance data accuracy, attribution, and audience insights.

Was this article helpful?

Comments

Recommended for you

Blog

Attribution tracking guide

Learn how attribution tracking works, why it matters, and how Stape improves analytics with server-side tracking for accurate multi-channel attribution.

Apr 22, 2025

Turn better tracking into better marketing performance

Try for free