如果不想使用這個工具取得資料,Google Analytics 也有 Protocol 的連接教學 Data Export API - Protocol
在取得資料的過程中,我在一個觀念上問題花了不少時間,主要是下面這兩個參數上的設定:
- dimensions : 特性類似 SQL 的 GROUP BY
- metrics : 類似 SQL SELECT 的資料欄位
其他的資料欄位:Dimensions & Metrics Reference
工具的連結範例:
<?php
require_once('gapi.class.php');
/*建立與帳戶的連結*/
$ga = new gapi('email@yourdomain.com','password');
/*取得統計報告*/
$ga->requestReportData(
145141242,
array('browser','browserVersion'),
array('pageviews','visits')
);
foreach($ga->getResults() as $result){
echo $result;
echo 'Pageviews:',$result->getPageviews();
echo 'Visits:',$result->getVisits();
}
echo 'Total pageviews:',$ga->getPageviews();
echo 'total visits:',$ga->getVisits();
函數的參數說明:
<?php
requestReportData(
$report_id,
$dimensions,
$metrics,
$sort_metric=null,
$filter=null,
$start_date=null,
$end_date=null,
$start_index=1,
$max_results=30
)
| 屬性 | 型態 | 描述 | 範例 |
|---|---|---|---|
| $report_id | String | 統計報告的ID | 1892302 |
| $dimensions | Array | 尺寸欄位,類似 SQL 的群組 | array('browser') |
| $metrics | Array | 結果欄位,類似 SQL 的顯示欄位 | array('pageviews') |
| $sort_metric | Array(選擇性) | 資料排序依據,"visits" 為 ASC,"-visits" 為 DESC | array('-visits') |
| $filter | String(選擇性) | 過濾的邏輯條件 | |
| $start_date | String(選擇性) | 報告的起始時間'YYYY-MM-DD' | '2009-04-30' |
| $end_date | String(選擇性) | 報告的起始時間'YYYY-MM-DD' | '2009-06-30' |
| $start_index | Int(選擇性) | 資料的起始指標 | 1 |
| $max_results | Int(選擇性) | 資料的起始指標.最大 1000 筆 | 30 |
0 回應:
張貼留言