How to Track ‘Get Direction/Location’ Click as Event inside a Google Map iFrame

Black and white photo of Ron Geronimo Posted by: Ronel Geronimo Date Posted: March 23, 2026
Back to Case Studies

Issue

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.

Solution

We added custom code in GTM to track user interactions (see sample code below).

Achievement

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.

Snippet/Screenshot/Link

<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>