Protect your AdSense account from invalid clicks
This post will discuss how to protect your AdSense account from invalid click activities by doing some minimal code changes on your web page.
Google AdSense is a popular advertising platform which is a great way to earn money online. Most bloggers prefer Google AdSense for monetizing their websites.
To have your AdSense account approved is a great deal in recent times, but one must strictly adhere to AdSense program policies. If you fail to comply with these policies, your account might get banned. The first point in AdSense program policies clearly states that Publishers may not click their own ads or use any means to inflate impressions and/or clicks artificially, including manual methods.
Google monitors the clicks on its AdSense ads very closely. To prevent getting your account banned, you need to save your website from click bombings. There is no easy way to monitor the ad clicks on your website. This post will show you how to protect your Google AdSense account from unusual invalid clicks.
The idea is actually straightforward. When a user clicks on an AdSense ad for the first time, store a cookie/local storage on his machine with some expiration date. When the user visits another page in the same session or a new session, don’t serve him AdSense ads if that cookie is found on his machine.
To demonstrate, let’s see some code:
1. Enclose all your AdSense ad codes inside a hidden div with ads class.
|
1 2 3 |
<div class="ads" style="display:none;"> <!-- Insert AdSense ad code.. --> </div> |
2. Then have the following code inside the <head> section of your web page, where your other scripts reside or include it just before the closing </body> tag of your HTML page.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> // get the current date var nowDate = new Date(); var date = nowDate.getDate()+'/'+(nowDate.getMonth()+1)+'/'+nowDate.getFullYear(); jQuery(function($) { // if any element with class ads is clicked $('.ads').on('click', function () { // store current date on user local storage – you can change this value! localStorage.setItem("date", date); }); }); $(document).ready(function() { // check whether the current date is recorded on the user's local storage var prevDate = localStorage.getItem("date"); if (!prevDate || prevDate != date) { // serve ads $('.ads').show(); } else { // don't serve ads on next page view (serve something else – e.g., show donations link) } }); </script> |
That’s all about protecting your AdSense account from invalid clicks.
That’s it, guys. We hope you have found this little piece of code helpful for protecting your AdSense account from invalid clicks.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)