Skip to content

Instantly share code, notes, and snippets.

@webhasan
Last active August 20, 2023 00:00
Show Gist options
  • Save webhasan/3da04331164ced8488b1e0350baba896 to your computer and use it in GitHub Desktop.
Save webhasan/3da04331164ced8488b1e0350baba896 to your computer and use it in GitHub Desktop.
GTM Data Layer for Google AdSense Ads Click
(function() {
var isAdsArea = false;
var add_link = '';
var page_link = window.location.href;
document.addEventListener('mouseover', function (e) {
var target = e.target.closest('iframe[src^="https://googleads.g.doubleclick.net"]');
var target2 = e.target.closest('iframe[src$="/html/container.html"]');
if(target) {
isAdsArea = true;
add_link = target.getAttribute('src');
}else if(target2) {
isAdsArea = true;
add_link = target2.getAttribute('src');
}
else {
isAdsArea = false;
add_link = '';
}
});
window.addEventListener('blur', function () {
if(isAdsArea) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'ads_click',
'add_link': add_link,
'page_link': page_link
})
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment