The site is under maintenance.

Prevent Adsense from Click Bombing using Vanilla JavaScript - Cookie Based

Hello, Bloggers! Welcome to Fineshop Design.

If you have an Adsense approved site, you might have faced the issues of Ad-limits due to invalid clicks. Adsense may restrict showing Ads on your site for a limited time. If you want to control clicks on Adsense by your visitors, you should try the codes I am going to share today.

Adsense Click Anti Bomb Script
© Fineshop Design | Adsense Click Anti Bomb Script

How does it work?

Basically, we will set Cookies in visitors' browser with value based on how may times they have clicked on Ads. Let's understand with an example.

Suppose you have set 3 maximum clicks. Whenever, he/she will click on Ads, a cookie with desired key (i.e. "MAX_CLICKS") with value based on the clicks will set in their browsers, and the value will increment 1 after every click. So, when they will have reached the limit (i.e. 3 times) and again clicks on the Ads, the ads will disappear. Now, they will not see any Ads for a time, you set.

Clearing cookies will still allow users to click on Ads. So, it will not totally prevent from Click Bombing but it will help you to control the clicks.

Before we start let's take a look at its Demo.

Getting Started

Important!Before we start adding codes in XML, I will recommend you to take a Backup of your current theme. By chance if any problem occurs, you can restore it later.

Step 1: First of all Login to your Blogger Dashboard.

Step 2: On Blogger Dashboard, click Theme.

Step 3: Click the arrow down icon next to 'customize' button.

Step 4: Click Edit HTML, you will be redirected to editing page.

Step 5: Now search the code </head> and paste the following JavaScript Codes just above to 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
        const declarations = "{pointer-events:none}";
        if (document.getElementById("mxAds_stl") == null) {
          var stl = document.createElement("style");
          stl.id = "mxAds_stl";
          stl.innerHTML = (antiBombSet.adsSelectors || ".adsbygoogle") + declarations;
          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: Search the code </body> and paste the following JavaScript Codes just above to it.

<script>
  /*<![CDATA[*/
  !function(){function b(a,b){return null!=(a=Cookie.get(a))&&parseInt(a)>=b}var d=antiBombSet.cookieKey||"ADS_CLICK",a=antiBombSet.adsSelectors||".adsbygoogle",f=antiBombSet.timeOut||7200,g=antiBombSet.maxClick||3;0<document.querySelectorAll(a).length&&document.querySelectorAll(a).forEach(a=>{a.addEventListener("click",function(){var a,c;b(d,g)?antiBombSet.callback():(a=d,null==(c=Cookie.get(a))?Cookie.set(a,"1",{secure:!0,"max-age":f}):(c=parseInt(c)+1,Cookie.set(a,c.toString(),{secure:!0,"max-age":f})))})}),window.addEventListener("blur",function(){b(d,g)&&antiBombSet.callback();for(var c,h,i=document.querySelectorAll(antiBombSet.iframeSelectors||".adsbygoogle iframe"),j=0;j<i.length;j++)document.activeElement==i[j]&&(b(d,g)?antiBombSet.callback():(c=d,null==(h=Cookie.get(c))?Cookie.set(c,"1",{secure:!0,"max-age":f}):(h=parseInt(h)+1,Cookie.set(c,h.toString(),{secure:!0,"max-age":f}))))})}();
  /*]]>*/
</script>

About the Author

Hey! I'm Deo Kumar, a passionate web developer skilled in JavaScript, Node.js, Cloudflare Workers, KV stores, and Next.js. With a focus on dynamic web applications, I excel in JavaScript and Node.js, ensuring robust websites. As a front-end expe…

4 comments

  1. from next upload minified js sor
    1. You can use any minification tool for it 🫣.
  2. not working , malipal time click for testing your website.
    you are useing this code in your website . tell me
    1. No, currently I am not using it. For testing there is a 'Demo' link button.
To avoid SPAM, all comments will be moderated before being displayed.
Don't share any personal or sensitive information.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.