Google Tag Manager
By:
Raj Gurung
Published On :
February 8, 2024
If you have ever embedded an iframe on your website, you might have wondered how to track its performance and interactions. For example, you might want to know how many people clicked on a button inside the iframe, or how long they stayed on the iframe page. Tracking iframe can be challenging, especially if you do not have access to the iframe source code or analytics. Fortunately, there is a solution: Google Tag Manager (GTM).
Google Tag Manager is a free tool that allows you to manage and deploy various tags (such as Google Analytics, Facebook Pixel, etc.) on your website without modifying the code. GTM can also help you track iframe events, such as page views, clicks, form submissions, and more. In this blog post, we will show you how to set up GTM to track iframe on your website, and what benefits you can get from it.
Iframe is the secondary frame also called inline frame is the kind of HTML element that has another HTML page within the mail parent HTML.
To set up this tracking, firstly you need to set a few things-
It is mainly common for advertisements, embedded videos, web analytics, and interactive content.
To check if the page has an iframe you can right-click on the frame and it will show you
OR
If you see the URL of the frame using the source code it is different from the parent frame
Another way to find if you have an iframe is to go to the inspect page and see if you have an <iframe />
element
To track the iframe in your website, you need to have access to the iframe itself and if you do not have access then you cannot track the content inside the iframe
First of all, you need to create a new container on your existing account
And you need to install the Head and Body GTM code on your iframe element.
To check if the GTM is installed on your iframe you can preview the container of the iframe (not the parent container)
And if the GTM preview opens and says connected then you have successfully installed the GTM on your iframe.
Now you can track the content inside the iframe but you need to use the iframe container
Please refer to the following pages to see the
==Important==
To send the data to Google Analytics you don’t create a Google Analytics event because it will create a conflict of the data because of the 2 different GTM codes so you need to send the iframe data to your parent GTM first and and send the data to your analytics.
You need to install 2 different scripts to send and receive the data to your parent container.
<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>
<script>
try {
var postObject = JSON.stringify({
event: 'iframeFormSubmit',
form: 'Contact Us'
});
parent.postMessage(postObject, 'https://domain.com');
} catch(e) {
window.console && window.console.log(e);
}
</script>
Now you need to copy the above send code onto your Tag by choosing the Custom HTML tag.
In the above code, you need to change a couple of things.
First of all, you need to change;
Click_text: ‘{{Click Text}}’
Now you need to install the post message listener which will receive the data sent to from the iframe.
!important! You need to have your GA4 base code installed in your parent container.
In creating the trigger for the parent container
You need to choose the page where your iframe is installed
Page path Contains iframe
And now on your website you can trigger the event and see if your parent container has received the post-message listener for the iframe.
In the preview, it will come out as an Outbound Click because the click and the link if coming from a different website.
Finally, you need to send the data to your GA4
You need to
Now you need to create a Custom Variable
postMessageData.click_text
(this is from the data layer) postMessageData and dot(.) goes inside the bracket and click_text (case sensitive)
Now you need to put the custom variable on your tag.
Property Name: click_text
Value: {{dlv – postMessageData – click_text}} (this is the custom variable name that you created)
Choose the Tigger as a custom event trigger
After configuring the GA4 tag, use the “Preview” mode in GTM to test its functionality.
Tracking iframe using Google Tag Manager can help you gain valuable insights into your website performance and user behavior. You can measure how your iframe content affects your website goals, conversions, and engagement. You can also optimize your iframe content based on the data you collect.
We hope this blog post has helped you understand how to track iframe using Google Tag Manager. If you have any questions or feedback, please leave a comment below. Thank you for reading!
==Don’t forget to Publish the iframe version first==
==Then Publish the Parent container==
You can check the data in the debug view of Google Analytics 4 and it will take 24 to 48 hours to get processed in GA4
Let’s talk about your business goal and align the solution we provide with our digital marketing services.
FREE strategy call© Humming Web. All Rights Reserved