When a Google Map is embedded via an iframe, tracking link clicks within the map is not possible using the built-in click listeners in GTM.
We added custom code in GTM to track user interactions (see sample code below).
To track interactions within an embedded Google Map via an iframe, we added custom code in GTM from TeamAI to track user interactions. It captures general clicks on the map container and pushes a map_interaction event to the dataLayer with details like the map’s source and location.
Additionally, it detects when a user clicks inside the map’s iframe (indicated by the window losing focus) and sends a map_iframe_focus event to GTM for more specific interaction tracking.
<script>
// Track any click on the map iframe
document.addEventListener(‘click’, function(e) {
var mapElement = e.target.closest(‘.fl-map’);
if (mapElement) {
console.log(‘Map clicked’);
// Get the iframe src to extract map info
var iframe = mapElement.querySelector(‘iframe’);
if (iframe) {
var iframeSrc = iframe.getAttribute(‘src’);
console.log(‘Iframe src:’, iframeSrc);
// Push a generic “map interaction” event
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
‘event’: ‘map_interaction’,
‘map_location’: ‘Green Air Concepts’,
‘interaction_type’: ‘click’,
‘iframe_src’: iframeSrc
});
console.log(‘Pushed map_interaction to dataLayer’);
}
}
}, true);
// Also track iframe focus (when user clicks inside)
window.addEventListener(‘blur’, function() {
var activeElement = document.activeElement;
if (activeElement && activeElement.tagName === ‘IFRAME’) {
var mapContainer = activeElement.closest(‘.fl-map’);
if (mapContainer) {
console.log(‘Iframe received focus – user clicked inside map’);
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
‘event’: ‘map_iframe_focus’,
‘map_location’: ‘Green Air Concepts’
});
}
}
});
</script>