2011年7月12日 星期二

利用 Google Analytics API 抓取資料

資料來源:
1.http://tech.ed65.com/2010/12/02/%E5%88%A9%E7%94%A8-google-analytics-api-%E6%8A%93%E5%8F%96%E8%B3%87%E6%96%99-php/
2.http://www.askaboutphp.com/63/google-analytics-api-class-for-php.html

利用 Google Analytics API 抓取資料-PHP


<?php
// include the Google Analytics PHP class
include "googleanalytics.class.php";
try {
// create an instance of the GoogleAnalytics class using your own Google {email} and {password}
$ga = new GoogleAnalytics('email','密碼');

// set Google Analytics ID - 格式為 'ga:123456';
$ga->setProfile('google analytics ID');

// set the date range we want for the report - format is YYYY-MM-DD
$ga->setDateRange('2010-11-29','2010-12-01');

// get the report for date and country filtered by Australia, showing pageviews and visits
$report = $ga->getReport(
array('dimensions'=>urlencode('ga:date,ga:country'),
'metrics'=>urlencode('ga:pageviews,ga:visits'),
'filters'=>urlencode('ga:country=@Taiwan'),
'sort'=>'-ga:pageviews'
)
);

//print out the $report array
print_r($report);

} catch (Exception $e) {
print '錯誤: ' . $e->getMessage();
}
?>
其中 Google Analytics ID 為網址中xxxxx的部份 >>
https://www.google.com/analytics/reporting/?reset=1&id=xxxxx&pdr=20101101-20101201
請於此下載需要呼叫的檔案: googleanalytics.class

沒有留言: