Visible stat from Google Analytics
From my old blog Extract data from google analytic with PHP makes me wanna write another script to show number of pageviews on the web for all the world to see. It’s actually an old school “web counter” as we know it and since normally you can’t just extract pageviews data from google analytics to display on your website I have to make use of my old GAAPI class to extract data to create the counter, see example below.
How to use?
- Load GAAPI from my old blog though we’ll use only gaapi.class.php here.
- Construct gaapi class and call method All Time Page Views (total number of page views)
include_once('gaapi.class.php'); $ga=new gaApi('google username','password','ga:site id'); $allTimeSummery=$ga->getAllTimeSummery(); - Show the counter using HTML with CSS
CSS:#vGA{ width: 100px; position: relative; font-size: 9px; font-family: Verdana, Verdana, Geneva, sans-serif; padding-bottom: 15px; background: url(poweredBy.gif) no-repeat bottom left; } #vGA div{ text-align: right; border: 2px solid #4f6db0; color: #555; line-height: 15px; } #vGA #vGALabel{ background: #4f6db0; text-align: center; color: white; float: right; padding: 0 0 0 2px; margin: 0 0 0 2px; }HTML:
<?= $allTimeSummery['ga:pageviews'] ?> Views - Save this Powered By Google image into the same folder as others files
And now we’ve got easy Visible Stat from Google Analytic. If you wish to show other data such as Unique Visits you can do so by follow instruction from my old blog Extract data from google analytic with PHP
Put it all together
<html>
<head>
<style type="text/css">
#vGA{
width: 100px;
position: relative;
font-size: 9px;
font-family: Verdana, Verdana, Geneva, sans-serif;
padding-bottom: 15px;
background: url(poweredBy.gif) no-repeat bottom left;
}
#vGA div{
text-align: right;
border: 2px solid #4f6db0;
color: #555;
line-height: 15px;
}
#vGA #vGALabel{
background: #4f6db0;
text-align: center;
color: white;
float: right;
padding: 0 0 0 2px;
margin: 0 0 0 2px;
}
</style>
</head>
<body>
<?
include_once('gaapi.class.php');
$ga=new gaApi('google username','password','ga:site id');
$allTimeSummery=$ga->getAllTimeSummery();
?>
<?= $allTimeSummery['ga:pageviews'] ?>
Views
</body>
</html>
ขอบคุณมากครับ
ลองเอาไปใช้ดูแล้ว เข้าใจว่าค่าสถิติจาก google มันไม่ real time แต่ก็ดีที่ใช้ข้อมูลจาก analytic ได้เลย
@don_maley
ใช่แล้ว ข้อเสียของการดึงข้อมูลสถิติจาก google มาใช้ก็คือมันจะประมวลผลเป็นรายชั่วโมง ไม่ real time แต่ก็ประหยัดทรัพยากรบนเซิฟเวอร์ของเราไปได้มากที่ไม่ต้องนั่งเก็บและประมวลผลสถิติเอง ^^