For a live example visit here
To view the read me visit here
What exactly does a "Saurdo Adblock Counter" do? Well it counts Adblocks! Using this program you can detect just how many visitors on your website are blocking ads using adblock and compile some adblock statistics. Not only that, but you can set the program up to automatically display a message in the place of a blocked add. All you have to do is put all of your ads in a div container with a special class name, embed the script into the page, and start gathering your visitor data!
What inspired such a creation? Well, i've been playing with the underlying methodology behind this program for quite some time. In a previous post I went into some detail about how exactly the program works and shared my rather surprising results. Since then I have gotten a number of e-mails requesting the very program I am releasing today. So here it is! Using my handy guide, I was able to make it so that a message is displayed in the place of any blocked ads! That way you can kindly ask them to whitelist you and explain to them how ads keep your site running.
To view the recorded results you must navigate to the "adblocks.txt" file which will be in the same directory as the "adblocks.php" file. The first number is the amount of impressions, the second number is the amount of impressions that had adblock on. To get the percentage of impressions using adblock, divide the second number by the first.
Instructions:
1. The first thing you need to do is modify the webpage you plan to embed this script on. Wrap all the ads you wish to get statistics of in a div tag and give them all the same unique class name. View the "example.html" file for an example of this. Make sure that the ad script is the only thing inside the DIV tags and that all the DIV tags have the same class name. I recommend not using a name like "ads" because some adblockers tend to completely disable those divs, thus preventing the blocked message system from working.
2. Add the script tag to the head section of your website's page. Make sure you have the proper path to the javascript file. You may choose to use a direct path if necessary (eg src="http://www.saurdo.com/files/adblocks/adblocks.js")
3. Open the "adblocks.js" file in a regular text editor like notepad. On the first line set the "ads" variable to the same class name that you gave the DIV's in the first part of the instructions.
4. On the next line change the "msg" variable to the message that will display in the place of any ads that are being blocked. You may choose to make this blank if you don't want to use thsi feature.
5. The next line provides the path to the PHP file that will record your statistics for you. You do not need to set this if you plan to keep everything in the same directory as the page the script is embedded on. Even if the "adblocks.js" file and "adblocks.php" file are in the same directory, the javascript file won't be able to find the PHP file if the page the javascript file is on is in a different directory.
If you choose to provide a direct URL path for the PHP file use 'http://"+document.location.hostname+' for the main part of your website's name. For example if the direct URL path to my php file was located at "http://www.saurdo.com/files/adblocks/adblocks.php". I would put "var php_url = 'http://'+document.location.hostname+'/files/adblocks/'"
6. Save everything, upload it, and test! If it just won't work then please contact me, not only will I probably get it to work for you, but you will be providing me with information that will help me make it easier for everyone else.
If you have any problems, suggestions, or comments please do not hesitate to contact me. I am more than happy to help.
Thanks for downloading!
So why am I updating it? Well, as I've already mentioned, it was one of the very first things I created after learning JavaScript, and while tens of thousands of people have apparently downloaded it, the script is, in my now expert opinion, terrible! While it works, it could work better. So I've used the knowledge that I've gained in the last four years since release (holy crap) to make the script something that I wouldn't be embarrassed to show another programmer.
First off, the size: The old script is about 55KB uncompressed. The new script is 5KB. That is 9% of its original size! Keep in mind that I am using all of the same images and the new script has every bit of functionality the old one had. How did I trim so much off? Well, the old script uses the prototype JavaScript library, which was completely unnecessary because not only did I use maybe 3 functions out of the 1 billion it provides, but the functions aren't even that hard to recreate. So getting rid of that file really slimmed it down.
The second thing I did was make it easier to implement onto your site.
Here's what I presented people with in the old script:
if (getcookie("email") != null){
var message = getcookie("email").split(",");
document.write("You have signed up for an e-mail alert succesfully with " +message)
}
else {
document.write('Enter your e-mail address:
')
document.write('
')
document.write('')
document.write('')
}
I somehow expected them to find a way to implement that on their site. I can't imagine the number of people that got scared and ran the other way when they saw that. Even I get a little scared, and I wrote it! That's not what I want. I want the opposite of that. I want the process to be friendly!
My new version looks like this:
Sign up for an e-mail alert!
![]()
If you're scared by that then you have no business running websites! This is plain old HTML (and some basic JavaScript). This is a massive improvement from before, not only is it easier to implement, but it makes me want to implement it. It's just begging to be pasted somewhere. In fact, I might do that on this site if I find a place where I like it. I bet you're wondering how I could make it so simple but keep the functionality the same. The old script was ugly because it had to decide if it should write the actual form or display a message that said you already signed up. In my new script I moved all that code to an external JavaScript file and improved it so it wrote the form no matter what, but if the script needed to replace the form the script replaced it before you could even think about using it.
Not only that, but I lied about having no more features than before. I get a number of e-mails asking about how one would go about disabling the very functionality I was just talking about; how to disable the feature that prevented people from submitting multiple times. Of course, in my old script it was a horribly complicated process trying to modify it so it worked the way you wanted. Now, all you have to do is set the "enable_cookies" variable to true or false. If that's too complicated for you then you can pay me and I'll take the microsecond of time to do it for you.
The lies continue: I actually have another feature that I implemented; in such the case that you chose to disable the option to prevent multiple submissions, I added a checking mechanism to the PHP file that prevents duplicate e-mails from being submitted. It will even spit an "E-mail already exists in list!" message at you.
The lies continue even more: I have yet another feature that I implemented while writing this! In the old script I instructed people to chmod their e-mail storage text files to something that couldn't be accessed by passers by. Nobody knew how to do this and the method for doing so differed depending on your host. So what I did was have the PHP script that writes the e-mails CHMOD it for you!
I've turned this script into something I might be proud to show another programmer. This is really something I should have done a long time ago. How everyone managed, I'll never know.
For an example form visit here
To download click here
View the original release page (with a lot more information) here
View the read me file (with a whole lot more information!) here
The whole reason I resorted to rejecting large images was because the PHP function that causes the memory error doesn't handle its errors very well. What I would have liked to do was recover from one of these memory errors and display a different image in its place, but instead the error kills the script.
So there is no way I could have done something like this:
// loading image!
$im = @imagecreatefromjpeg("test-image.jpg");
// oh no! image too big to load into memory!
if(!$im){
// special function that creates new image and puts the filename in it
imageCenterString( $THUMBNAIL_WIDTH, $THUMBNAIL_HEIGHT, basename($image), $imageFontSize );
}
Unfortunately, PHP would have none of this! The script will die at the first line and ignore everything after it. It makes sense because if your PHP script is exceeding its memory limit it's not going to continue down the script, it doesn't have enough memory too! Still, I wish the "imagecreatefromjpeg" function had a feature that makes it purge the image when it hit the memory limit.
Someone e-mailed me the other day, asking about why some of their larger images weren't thumbnailing. I kindly explained to him that he'd need to adjust PHP's memory limit to get them to thumbnail, or modify the script to prevent it from trying, but then this got me thinking; is there a way that I can dynamically adjust the memory limit to conform to the needs of the image?
Thankfully, someone had already doled out the details of the algorithm used to calculate memory usage over in the PHP documentation community:
// starting memory usage
$start_mem = memory_get_usage();
// Place the images to load in the following array:
$ffs = array();
// loop through array of images to test
foreach ($images as $image) {
// get array of image data
$info = getimagesize($image);
// load image
$im = imagecreatefromjpeg($image);
// get current memory usage (after loading image)
$mem = memory_get_usage();
// calculate fudge factor
$ff = (($mem - $start_mem) /
($info[0] * $info[1] * ($info['bits'] / 8) * $info['channels']));
// stick fudge factor in cumulative array
$ffs[] = $ff;
// remove image from memory
imagedestroy($im);
// get new starting memory
$start_mem = memory_get_usage();
}
// calculate the mean of all the fudge factors
echo 'Mean fudge factor: ' . (array_sum($ffs) / count($ffs));
What do you do with this number? Well, we can now calculate the estimated memory that an image will take to thumbnail. To do that you simply take the (width * height * bpp * the fudge factor), and to set PHP to use that you simply use the ini_set function!
All in all my final implementation into my directory lister looks like this:
// if it is set
if($fudge_factor){
// set memory limit
ini_set('memory_limit', (($imgsize[0] * $imgsize[1] * ($imgsize['bits']/8) * $fudge_factor)+5).'M');
}
That's right, it's only 3 lines! It could easily be one if I sacrificed readability. On top of all this, because I know this is sounding complicated for those that aren't programmers, I made a quick and easy form to randomly select images from a directory and calculate the mean fudge factor. I call it the "fudge factor calculator!"
You can download the new version here!

