Hello bloggers! Welcome to daily technologies
If you have an Adsense approved site, you might have faced ad limit issues due to invalid clicks. Adsense may restrict the display of ads on your site for a limited time. If you want to control clicks on Adsense by your visitors, you must try the codes that I will share today.
How does it work?
Basically, we will set Cookies in the browser of visitors with value based on the number of times they have clicked on the ads. Let's understand with an example.
Suppose you have set 3 clicks max. Whenever you click Ads, a cookie with the desired key (ie "MAX_CLICKS") will be set with a value based on clicks in your browsers, and the value will increase by 1 after each click. So when they have reached the limit (i.e. 3 times) and click the ads again, the ads will disappear. Now, they won't see any ads for a while, you set it.
Clearing cookies will still allow users to click on ads. So it won't totally prevent click bombardment, but it will help you control clicks.
Before we start, let's take a look at its demo.
Starting
Important!
Before you start adding code in XML, I'll recommend that you make a backup of your current theme. By any chance, if any problem occurs, you can restore it later.
Step 1: First of all, login to your Blogger dashboard.
Step 2 – In the Blogger Dashboard, click Theme.
Step 3: Click the down arrow icon next to the 'customize' button.
Step 4: Click Edit HTML, you will be redirected to the edit page.
Step 5 – Now find the </head> code and paste the following JavaScript codes just above it.
<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>
Step 6 – Find the </body> code 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>
Tags:
Blogger Widgets