Get adsense stats from PHP

Posted by admin on December-11-2009 Add Comments

Bookmark and Share


Get adsense stats from PHP

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 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 …
Here’s what to do:

  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:
    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`)
    )
  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.
  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 :-) For now, enjoy the script and let me know if you like it!
Here is the script:

PHP Code:
<?
// Change things from here!!!
$adsenseUsername = "yourname@yourcompany.se";
$adsensePassword = "yourpassword";

$dbHostname = "localhost";
$dbUsername = "dbUsername";
$dbPassword = "dbPasswd";
$dbName = "theDbName";

$reports = Array("1234" => "channel1",
"56215" => "channel4",
"35507" => "channel3");
// Stop changing things here!!!

$adsenseCookie = "adsenseCookie.txt";

// Google time
putenv('TZ=US/Pacific');
$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);

Post a Comment

You must be logged in to post a comment.