How does the code work?
The code shared in this article is designed to control clicks on Google AdSense ads by setting cookies in the visitor's browser with a value based on the number of times they have clicked on the ads. By doing so, the code helps to prevent invalid clicks and click bombing, which can lead to AdSense account suspension or termination.
The code works by adding JavaScript to your Blogger template. When a visitor clicks on an AdSense ad on your site, the code checks if the visitor has clicked on the ad more than the maximum allowed times (specified in the code). If the visitor has reached the maximum clicks, the code will execute a callback function that prevents the ads from showing up and displays a message to the visitor.
To set the maximum number of clicks and the timeout before the ads reappear, you can modify the values in the code. The code also allows you to customize the cookie key, ads selectors, and iframe selectors according to your site's needs.
The Complete Guide to Install the AdSense Invalid Click Protector Script
<script>
/*<![CDATA[*/
/* Cookie functions */
const Cookie = {
get: (e) => { e = document.cookie.match(new RegExp("(?:^|; )" + e.replace(/([.$?*|{}()[\]\\/+^])/g, "$1") + "=([^;]*)")); return e ? decodeURIComponent(e[1]) : void 0 },
set: (e, n, o = {}) => { o = { path: "/", ...o }, o.expires instanceof Date && (o.expires = o.expires.toUTCString()); let c = unescape(encodeURIComponent(e)) + "=" + unescape(encodeURIComponent(n)); for (var t in o) { c += "; " + t; var a = o[t]; !0 !== a && (c += "=" + a) } document.cookie = c },
rem: (e) => { Cookie.set(e, "", { "max-age": -1 }) }
}
/* Anti bomb config */
const antiBombSet = {
timeOut: 3600, /* Timeout in seconds, when to ads appear after maximum clicks */
maxClick: 3, /* No of maximum clicks */
cookieKey: "MAX_CLICKED", /* Cookie key to set */
adsSelectors: "ins.adsbygoogle", /* Ads selectors */
iframeSelectors: "ins.adsbygoogle iframe", /* Ads iframe selectors */
callback: () => {
/* Runs only one time if/when clicked maximum times on ads */
if (antiBombSet.executed === undefined) {
antiBombSet.executed = !0;
/* Prevent clicks on ads placement with pointer-events:none | You can also try display:none */
if (document.getElementById("mxAds_stl") == null) {
var stl = document.createElement("style");
stl.id = "mxAds_stl";
stl.innerHTML = (antiBombSet.adsSelectors || ".adsbygoogle") + "{pointer-events:none}";
document.head.appendChild(stl);
}
/* Add your js below to execute if/when clicked maximum times on ads */
/* console.warn("You have clicked the maximum times on ads. Don't click on ads if you don't want to support us."); */
}
}
};
if (Cookie.get(antiBombSet.cookieKey || "ADS_CLICK") != undefined && parseInt(Cookie.get(antiBombSet.cookieKey || "ADS_CLICK")) >= (antiBombSet.maxClick || 3)) {
antiBombSet.callback()
};
/*]]>*/
</script>{codeBox}
Now, search for the code </body> and paste the following JavaScript codes just above it.
<script>
/*<![CDATA[*/
! function () {
function n(e, o) {
return null != (e = Cookie.get(e)) && parseInt(e) >= o
}
var l = antiBombSet.cookieKey || "ADS_CLICK",
e = antiBombSet.adsSelectors || ".adsbygoogle",
i = antiBombSet.timeOut || 7200,
c = antiBombSet.maxClick || 3;
0 < document.querySelectorAll(e).length && document.querySelectorAll(e).forEach(e => {
e.addEventListener("click", function () {
var e, o;
n(l, c) ? antiBombSet.callback() : (e = l, null != (o = Cookie.get(e)) ? (o = parseInt(o) + 1, Cookie.set(e, o.toString(), {
secure: !0,
"max-age": i
})) : Cookie.set(e, "1", {
secure: !0,
"max-age": i
}))
})
}), window.addEventListener("blur", function () {
n(l, c) && antiBombSet.callback();
for (var e, o, t = document.querySelectorAll(antiBombSet.iframeSelectors || ".adsbygoogle iframe"), a = 0; a < t.length; a++) document.activeElement == t[a] && (n(l, c) ? antiBombSet.callback() : (e = l, null != (o = Cookie.get(e)) ? (o = parseInt(o) + 1, Cookie.set(e, o.toString(), {
secure: !0,
"max-age": i
})) : Cookie.set(e, "1", {
secure: !0,
"max-age": i
})))
})
}();
/*]]>*/
</script>{codeBox}
Step 7: Save the template
Click on save icon to save "HTML" template.
Once the script is installed, it will start monitoring your AdSense ads for invalid clicks. If it detects any, it will automatically block them and prevent them from being registered on your AdSense account. This can help ensure that your account remains in good standing and that you continue to earn revenue from your ads.
Features
- Prevents Invalid Clicks
- Customizable Click Limits
- Easy to Implement
0 Comments