Last active
August 20, 2023 00:00
-
-
Save webhasan/3da04331164ced8488b1e0350baba896 to your computer and use it in GitHub Desktop.
GTM Data Layer for Google AdSense Ads Click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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