Google Adsense statistics by using PHP
1. 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.
2. 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.
3. Create a database and assign a user with a password for that.
4. Run this sql code:
5. 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.
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`)
6. 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 Smile For now, enjoy the script and let me know if you like it!
Here is the script:
“56215″ => “channel4″,
“35507″ => “channel3″);
// Stop changing things here!!!
$adsenseCookie = “adsenseCookie.txt”;
// Google time
$now = date(”Y-m-d H:i:s”);
$dbLink = mysql_pconnect($dbHostname, $dbUsername, $dbPassword) or die(’Could not connect to database: ‘.$dbHostname);
mysql_select_db($dbName, $dbLink) or die(’Could not find database: ‘.$dbName);
// Handle every report separately
foreach($reports as $id => $name)
$result = getAdsenseReport($id);
logChannel($name, $result, $now);
// Only functions below, to do all the magic.
// This function takes care of the sql part, that stores channel information.
function logChannel($channelName, $data, $time)
$sql = “INSERT INTO adsense( id,
VALUES ( NULL,
// Gets the report for one channel (reportId).
// Formatting the respons in a pretty way, so we can work with it easily.
$reportUrl = “/adsense/report/view-custom.do?reportId=”;
$result = getAdsenseInfo($reportUrl . $reportId);
// the next two rows must be on one row!!!