Get adsense stats from PHP
I am a statistics freak. I can’t say anything else ![]()
But I guess that most of you, my fellow webmasters love to check your adsense stats every now and then every every day, and probably every hour also…
Why not let your own script fetch the statistics directly from google adsense reporting site? It’s always much easier to have the data locally in your own database, and then create scripts to display the stats in the way you are interested in.
I will now – totally free, no strings attached- give you the code that collects the present stats from adsense, for any channel you specify. Because I also love to work with databases, we will store the results in mysql.
The basics behind the script is that you choose what channels you want to fetch stats for, and then you let your script do that as often as you which. You can schedule this script with crontab or something similar as often you which. Don’t do it more often than every 15 minutes, because I have heared something from google that you are not allowed to script things mroe often against adsense…
Well,no more talking … lets get to work!
Here’s what to do:
- Login to adsense and click on “advanced reports”. Create channels if you don’t have any yet, because this script shows stats for separate channels.
- This part might be the most complicated, but still very easy. “View source” for the page and search for select name=”reportId”. Scroll further out on that row in the source and look for your channel names. You will find something similar to this Cholesterol-week . Write down the number and the name on a paper or somewhere. Do this for every channel you want to track.
- Create a database and assign a user with a password for that.
- Run this sql code: CREATE TABLE `adsense` (
`id` int(11) NOT NULL auto_increment,
`channel` varchar(50) NOT NULL,
`reportedWhen` datetime NOT NULL,
`impressions` int(11) NOT NULL,
`clicks` int(11) NOT NULL,
`rate` decimal(10,3) NOT NULL,
`cpm` decimal(10,3) NOT NULL,
`earnings` decimal(10,3) NOT NULL,
PRIMARY KEY (`id`)
) - Save the file you see in the blue box below as adsense.php or similar. Look at the first lines where you need to fill in your details. Both for your adsense account and your db details.
- In the file you need to enter all channelId’s and channelNames in the array. Just increase that array if you have more channels!
I have not created any script to display the stats yet, but that might come in the future
For now, enjoy the script and let me know if you like it!
Here is the script:
| <? // Change things from here!!! $adsenseUsername = “yourname@yourcompany.se”; $adsensePassword = “yourpassword”; $dbHostname = “localhost”; $reports = Array(”1234″ => “channel1″, $adsenseCookie = ”adsenseCookie.txt”; // Google time $dbLink = mysql_pconnect($dbHostname, $dbUsername, $dbPassword) or die(’Could not connect to database: ‘.$dbHostname); // Handle every report separately // Only functions below, to do all the magic. // This function takes care of the sql part, that stores channel information. $sql = “INSERT INTO adsense( id, mysql_query($sql, $dbLink); // Gets the report for one channel (reportId). // the next two rows must be on one row!!! foreach ($array as $key => $value) $info = Array( ”impressions” => $array["1"], return $info; // Accesses adsense to get the information. // Add login information to the url $ch = curl_init(); return $result; |
That’s all for today… Let me know if you find it useful, because I love the result
If people are interested I will consider to create a function that displays the stats also?!?

May 15th, 2009 at 2:29 pm
Brilliant post, thanks a lot for the information – I dont really ever write comments but found the post as a mistake. Keep posting more like this!, I added you to my favorites!
May 17th, 2009 at 10:19 am
Thanks for the comment! We will soon start writing a lot new tutorials with high quality, so stay tuned!
June 13th, 2009 at 3:39 pm
I was just about to write this to put in a wordpress plugin that I’m writing. Thanks a bunch for saving me the trouble.