2018年10月22日 星期一

[PHP] Dashboard 開發筆記 - 透過 date 和 strtotime 產生日期變化

開發 Dashboard 時,需要一些時間變化,這時可以靠 strtotime 來幫忙轉,十分方便

$ cat /tmp/t.php
<?php

echo date("Y/m/d")."\n";
echo date("Y/m/d", strtotime(date("Y/m/d")." -1 day"))."\n";
echo date("Y/m/d", strtotime(date("Y/m/d")." -2 day"))."\n";
echo date("Y/m/d", strtotime(date("Y/m/d")." -7 day"))."\n";
echo date("Y/m/d", strtotime(date("Y/m/d")." -8 day"))."\n";
echo date("Y/m/d", strtotime(date("Y/m/d")." -30 day"))."\n";
echo date("Y/m/d", strtotime(date("Y/m/d")." -31 day"))."\n";
echo date("Y/m/d", strtotime(date("Y/m/d")." -60 day"))."\n";


$ php /tmp/t.php
2018/10/22
2018/10/21
2018/10/20
2018/10/15
2018/10/14
2018/09/22
2018/09/21
2018/08/23

[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天的變化量,得知活躍用戶是否有對應成長,讓人決定要做哪個國家的客製化服務。

2018年10月20日 星期六

[SQL] 在 SQLite3 環境中,使用 Regular Expression @ Ubuntu 16.04

$ sudo apt-get install sqlite3-pcre
$ file /usr/lib/sqlite3/pcre.so
/usr/lib/sqlite3/pcre.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0afc1236e89d4a8b99746f231049101172714c2c, stripped
$ sqlite3
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> SELECT date();
2018-10-19
sqlite> SELECT date() AS d WHERE d REGEXP '[0-9]';
Error: no such function: REGEXP
sqlite> .load /usr/lib/sqlite3/pcre.so
sqlite> SELECT date() AS d WHERE d REGEXP '[0-9]';
2018-10-19
sqlite> SELECT date() AS d WHERE d REGEXP '^[0-9]$';
sqlite> SELECT date() AS d WHERE d REGEXP '^[0-9]+$';
sqlite> SELECT date() AS d WHERE d REGEXP '^[0-9\-]+$';
2018-10-19

2018年10月11日 星期四

PHP 開發筆記 - 判斷瀏覽器語系機制

就 Javascript 來說,很方便:

var userLanguage = navigator.language || navigator.userLanguage;

而 server site 就靠 $_SERVER['HTTP_ACCEPT_LANGUAGE'] 啦,但他的格式還會有語系偏好比重,需要小小處理一番:

//
// $_SERVER['HTTP_ACCEPT_LANGUAGE'] == 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7';
//
function _detect_browser_language($_SERVER_VAR, $system_prefer = array( 'en' => 1, 'cn' => 1 )) {
if (isset($_SERVER_VAR['HTTP_ACCEPT_LANGUAGE'])) {
$langs = array();
foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $entry) {
$t1 = explode(';', $entry);
$cnt = count($t1);
if ($cnt == 2) {
$t2 = explode('=', $t1[1]);
if (count($t2) == 2)
array_push($langs, array($t1[0], floatval($t2[1])));
else
array_push($langs, array($t1[0], 1.0));
} else if ($cnt == 1)
array_push($langs, array($t1[0], 1.0));
}
function lang_prefer_sort($a, $b) {
if( $a[1] == $b[1] ) return 0;
if( $a[1] > $b[1] ) return -1;
return 1;
}
usort($langs, 'lang_prefer_sort');
foreach($langs as $lang_info) {
if (is_array($lang_info)) {
if (isset($system_prefer[$lang_info[0]]))
return $lang_info[0];
$checker = explode('-', $lang_info[0]);
if (count($checker) == 2) {
if (isset($system_prefer[$checker[0]]))
return $checker[0];
else if ($checker[0] == 'zh')
return 'cn';
}
}
}
}
return 'en';
}