Powered By

Powered by Blogger

Sabtu, 02 Januari 2010

Display Google Analytics Stats On Your Website [Public Stats]

We use GAPI (gapi-google-analytics-php-interface) to display Google Analytics page views and visits to everyone, without requiring a password - it's all done by using the Google Analytics API.

To make some Google Analytics stats public, you need a PHP5 web host but it you don't have one, there are lots of free alternatives out there and you can then embed the stats using in iframe (very easy).

I have modified the example report which comes with GAPI to display the month, year, number of page views, visits and so on, and sort it by year and then month. It basically looks like this:

google analitycs public stats.

You can view a demo right here, on Web Upd8. As you can see, Google Analytics public stats can be easily integrated into Blogger too (I saw lots of requests of using this in Blogger blogs).

Download GAPI + my Google Analytics public stats example


To use GAPI, simply download the archive and extract it to your web host, then save the following file under any name you want (let's say gapistats.php) and place it in the same folder as GAPI:
<?php
define('ga_email','google_analytics_google_email@address.com');
define('ga_password','your_Google_Analytics_password');
define('ga_profile_id','YOUR GOOGLE ANALYTICS PROFILE ID');

require 'gapi.class.php';

$ga = new gapi(ga_email,ga_password);

$today = date('Y-m-d');
$search = array("2009","2010","01","02","03","04","05","06","07","08","09","10","11","12");
$replace = array("","","January","February","March","April","May","June","July","August","September","October","November","December");
$search2 = array ("<");
$replace2 = array ("");

$ga->requestReportData(ga_profile_id,array('month','year'),array('pageviews','visits','bounces','entrances'),array('-year','-month') ,$filter=null,$start_date='2009-03-01',$end_date=$today,$start_index=1,$max_results=1000);
?>
<table width="99%" align="center">
<tr>
<th>Month</th>
<th>Year</th>
<th>Pageviews</th>
<th>Visits</th>
</tr>
<?php
foreach($ga->getResults() as $result):
?>
<tr valign="center" align="center">
<td width="25%"><?php echo str_replace($search,$replace,$result) ?></td>
<td width="20%"><?php echo $result->getYear(str_replace($search2,$replace2,$result)) ?>
<td width="30%"><?php echo $result->getPageviews() ?></td>
<td width="25%"><?php echo $result->getVisits() ?></td>
</tr>
<?php
endforeach
?>
</table>
<br/><br/>
<table>
<tr>
<th>Total Months:</th>
<td><?php echo $ga->getTotalResults() ?></td>
</tr>
<tr>
<th>Total Pageviews</th>
<td><?php echo $ga->getPageviews() ?>
</tr>
<tr>
<th>Total Visits</th>
<td><?php echo $ga->getVisits() ?></td>
</tr>
<tr>
<th>Results Updated</th>
<td><?php echo $ga->getUpdated() ?></td>
</tr>
</table>


Then simply link to this page, or embed it in an iframe. Example:
<iframe allowtransparency="true" scrolling="no" frameborder="0" style="border: none; width: 550px; height:600px;" src="http://path.to/gapistats.php"></iframe>


What you can change in this script:

a) Obviously, enter your Google Analytics email address (GMAIL), password and Google Analytics profile ID on top of the page, as suggested. To find out your Google Analytics profile ID, select a profile, then click "Edit" and you will see the profile ID in the top left corner:

google analytics profile id

b)
requestReportData(ga_profile_id,array('month','year'),array('pageviews','visits','bounces','entrances'),array('-year','-month') ,$filter=null,$start_date='2009-03-01',$end_date=$today,$start_index=1,$max_results=1000)

This part basically contains all the variables. Edit the start date, replace 'pageviews' and 'visits' with another variable if you want to display some other stats, etc. Or leave it as is. You can find more info on the Google Analytics API docs.

Tidak ada komentar:

Posting Komentar