2018年10月22日 星期一

[PHP] Dashboard 開發筆記 - 使用 Google Analytics 和 Google Adsense API 追蹤數據

AU_GroupByCountry

大概三個禮拜前已經做好了一些研究,一直偷懶沒寫點紀錄 :P 程式碼片段:

https://github.com/changyy/TrackingLibrary/tree/master/php/Codeigniter-3.1.9/application/libraries

取得 Google Analytics 數據 - Google_Service_Analytics 用法:

$this->load->library('GaQuery_lib', array(
'vendor/autoload.php' => 'vendor/autoload.php',
'access_token' => $access_token,
));

$ret = $output = $this->gaquery_lib->query(
$this->ga_profile_id,
'20180901', // $this->input->get_post('date_start'),
'20190930', // $this->input->get_post('date_end'),
'ga:users,ga:newUsers', // $this->input->get_post('metrics'),
[
'dimensions' => 'ga:countryIsoCode',
'sort' => '-ga:users'
], // $options
);


取得 Google Adsense 數據 - Google_Service_AdSense 用法:

$this->load->library('AdsenseQuery_lib', array(
'vendor/autoload.php' => 'vendor/autoload.php',
'access_token' => $access_token,
));

$ret = $output = $this->adsensequery_lib->getReport(
'20180901', // $this->input->get_post('date_start'),
'20190930', // $this->input->get_post('date_end'),
[
'dimension' => ['AD_UNIT_CODE'],
'metric' => [
'AD_REQUESTS',
'AD_REQUESTS_COVERAGE',
'AD_REQUESTS_CTR',
'CLICKS',
'COST_PER_CLICK',
'AD_REQUESTS_RPM',
'EARNINGS',
]
]
);


如此一來,光 GA 就可以組出不錯的數據,例如取出 60 天的數據,可以做出近30天與上個近30天的變化量,得知活躍用戶是否有對應成長,讓人決定要做哪個國家的客製化服務。

沒有留言:

張貼留言