You are currently viewing as a guest which gives you limited access to some domain name discussions and other features. By registering for free you will have access to post topics, buy & sell domains, communicate privately with other members, and access many other special features. Registration is fast, simple and free so please, register to join our community today!

DotSauce Domain Forums » Web Development Forums » Web Development » Get Google Adsense statistics by using PHP

Web Development Discuss tips, tricks and resources for developing your domain names.

Reply
 
Thread Tools Display Modes
  #1  
Old 01-14-2008, 10:49 AM
thestudent thestudent is offline
Junior Member
 
Join Date: Jan 2008
Posts: 11
thestudent is on a distinguished road
Default Get Google Adsense statistics by using 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:
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);
}

// 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)
{
global $dbLink;

$sql = ?INSERT INTO adsense( id,
channel,
reportedWhen,
impressions,
clicks,
rate,
cpm,
earnings)
VALUES ( NULL,
?$channelName?,
?$time?,
?$data[impressions]?,
?$data[clicks]?,
?$data[rate]?,
?$data[cpm]?,
?$data[earnings]?)?;

mysql_query($sql, $dbLink);
}

// Gets the report for one channel (reportId).
// Formatting the respons in a pretty way, so we can work with it easily.
function getAdsenseReport($reportId)
{
$reportUrl = ?/adsense/report/view-custom.do?reportId=?;
$result = getAdsenseInfo($reportUrl . $reportId);

// the next two rows must be on one row!!!
preg_match(?/\<tr class\=?totals?\>.*\<td.*\>.*\<\/td\>.*\<td.*\>(.*)\<\/td\>.*\<td.*\>(.*)\<\/td\>
.*\<td.*\>(.*)\<\/td\>.*\<td.*\>(.*)\<\/td\>.*\<td.*\>(.*)\<\/td\>.*<\/tr>/simU?, $result, $array);

foreach ($array as $key => $value)
{
$array[$key] = str_replace(Array(?USD?, ?%?, ?.?), ??, $value);
$array[$key] = str_replace(?,?, ?.?, $array[$key]);
}

$info = Array( ?impressions? => $array[?1″],
?clicks? => $array[?2″],
?rate? => $array[?3″],
?cpm? => $array[?4″],
?earnings? => $array[?5″]);

return $info;
}

// Accesses adsense to get the information.
// This is where all the magic happens, login and requesting the asked report.
function getAdsenseInfo($destination)
{
global $adsenseUsername, $adsensePassword, $adsenseCookie;

// Add login information to the url
$postdata = ?destination=? . urlencode($destination) . ?&username=? . urlencode($adsenseUsername) . ?&password=? . urlencode($adsensePassword) . ?&null=Login?;

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,?https://www.google.com/adsense/login.do?);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, ?Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)?);
curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);

return $result;
}
?>
Reply With Quote
  #2  
Old 08-19-2008, 10:55 AM
whogetdomain.com whogetdomain.com is offline
Junior Member
 
Join Date: Aug 2008
Posts: 11
whogetdomain.com is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

Thanks for your info.
But will it against their TOS ?
Reply With Quote
  #3  
Old 09-22-2008, 08:59 AM
ashiq011 ashiq011 is offline
Junior Member
 
Join Date: Sep 2008
Posts: 3
ashiq011 is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

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
Reply With Quote
  #4  
Old 10-30-2008, 12:01 PM
sryk sryk is offline
Junior Member
 
Join Date: Oct 2008
Posts: 6
sryk is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

Great. Do this really work? i will give it a try.
Thanks buddy. Well done.


Poke Your Name!
Reply With Quote
  #5  
Old 11-07-2008, 11:32 AM
akash011 akash011 is offline
Junior Member
 
Join Date: Nov 2008
Posts: 7
akash011 is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

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
Reply With Quote
  #6  
Old 11-10-2008, 05:29 AM
tecktalk tecktalk is offline
Member
 
Join Date: Sep 2008
Posts: 54
tecktalk is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

hey its very nice of to give us this very much needed information..thanks..!!

________________________
los angeles seo slimming patches
Reply With Quote
  #7  
Old 11-19-2008, 06:13 AM
jodiboli jodiboli is offline
Junior Member
 
Join Date: Nov 2008
Posts: 2
jodiboli is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

Thank you very much. Keep posting
Reply With Quote
  #8  
Old 01-07-2009, 01:47 AM
limons limons is offline
Junior Member
 
Join Date: Jan 2009
Posts: 3
limons is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

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.
Reply With Quote
  #9  
Old 04-19-2009, 02:44 AM
manish07 manish07 is offline
Member
 
Join Date: Apr 2009
Posts: 38
manish07 is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

Great information, Thanks for sharing.
Reply With Quote
  #10  
Old 07-12-2009, 02:59 PM
maandan maandan is offline
Junior Member
 
Join Date: Jul 2009
Posts: 2
maandan is on a distinguished road
Default Re: Get Google Adsense statistics by using PHP

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.
_________________
Property Developers
Werbeartikel
Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Google Adsense vs. Parking domainflipper Domain Parking & Monetization 6 01-28-2010 02:01 AM



AQDN.com WHOIS.de DomainTools MarketTheme Above.com Register .CO Domains

All times are GMT. The time now is 06:13 PM.

Powered by vBulletin

Domain name forum recommended by Domaining.com


Copyright © 2007-2010 Mark Fulton|DotSauce Magazine