[精讚] [會員登入]
1111

設定Google analytics API #3 -- 查詢範例

讓你的網站能夠存取你的Google analytics上面的資料

此文完整連結 http://n.sfs.tw/11596

複製連結 設定Google analytics API #3 -- 查詢範例@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-24 09:51:07 最後編修
2017-08-10 21:07:39 By 張○○
 

自動目錄

此文分成三個部分

設定Google analytics API #1 -- Google網站上的設定

設定Google analytics API #2 -- PHP的程式安裝和撰寫

設定Google analytics API #3 -- 查詢範例

上面#1, #2兩篇為安裝、設定及基本的程式,此篇寫幾個查詢的範例

五、查詢範例

修改上一篇的 getResults() 函數,印出結果

1. 查詢一週來每日的訪客數

  private function getResults($analytics, $profileId) {
    $optParams = array(
      'dimensions' => 'ga:date', 
      'sort' => 'ga:date');
    return $analytics->data_ga->get(
      'ga:' . $profileId,
      '7daysAgo',
      'today',
      'ga:users',
      $optParams);
  }

結果:

Array ( [0] => Array ( [0] => 20170804 [1] => 850 ) [1] => Array ( [0] => 20170805 [1] => 484 ) [2] => Array ( [0] => 20170806 [1] => 537 ) [3] => Array ( [0] => 20170807 [1] => 900 ) [4] => Array ( [0] => 20170808 [1] => 870 ) [5] => Array ( [0] => 20170809 [1] => 879 ) [6] => Array ( [0] => 20170810 [1] => 881 ) [7] => Array ( [0] => 20170811 [1] => 53 ) )

2. 查詢最近一個月的SESSION數中,依國別排序由多至少取10筆

  private function getResults($analytics, $profileId) {
    $optParams = array(
      'dimensions' => 'ga:country',
      'sort' => '-ga:sessions',
      'max-results' => '10');
    return $analytics->data_ga->get(
      'ga:' . $profileId,
      '29daysAgo',
      'today',
      'ga:sessions',
      $optParams);
  }

結果:

Array ( [0] => Array ( [0] => Taiwan [1] => 21192 ) [1] => Array ( [0] => Hong Kong [1] => 1081 ) [2] => Array ( [0] => United States [1] => 232 ) [3] => Array ( [0] => Malaysia [1] => 198 ) [4] => Array ( [0] => China [1] => 139 ) [5] => Array ( [0] => Japan [1] => 97 ) [6] => Array ( [0] => Macau [1] => 91 ) [7] => Array ( [0] => (not set) [1] => 81 ) [8] => Array ( [0] => Singapore [1] => 66 ) [9] => Array ( [0] => Canada [1] => 45 ) )

3. 昨天到今天每個小時間的新訪客數量

要依小時列資料的話特別注意,沒辦法說要列「從現在起的24小時」這樣,只能把今天和昨天的「每個小時」列出來,並且在dimensions中要代入時間,以免相同小時被加總起來。

  private function getResults($analytics, $profileId) {
    $optParams = array(
      'dimensions' => 'ga:date,ga:hour',
      'sort' => 'ga:date,ga:hour',
     );
    return $analytics->data_ga->get(
      'ga:' . $profileId,
      'yesterday',
      'today',
      'ga:newUsers',
      $optParams);
  }

結果:

Array ( [0] => Array ( [0] => 20170810 [1] => 00 [2] => 15 ) [1] => Array ( [0] => 20170810 [1] => 01 [2] => 14 ) [2] => Array ( [0] => 20170810 [1] => 02 [2] => 17 ) [3] => Array ( [0] => 20170810 [1] => 03 [2] => 5 ) [4] => Array ( [0] => 20170810 [1] => 04 [2] => 6 ) [5] => Array ( [0] => 20170810 [1] => 05 [2] => 2 ) [6] => Array ( [0] => 20170810 [1] => 06 [2] => 7 ) [7] => Array ( [0] => 20170810 [1] => 07 [2] => 11 ) [8] => Array ( [0] => 20170810 [1] => 08 [2] => 26 ) [9] => Array ( [0] => 20170810 [1] => 09 [2] => 46 ) [10] => Array ( [0] => 20170810 [1] => 10 [2] => 41 ) [11] => Array ( [0] => 20170810 [1] => 11 [2] => 50 ) [12] => Array ( [0] => 20170810 [1] => 12 [2] => 30 ) [13] => Array ( [0] => 20170810 [1] => 13 [2] => 58 ) [14] => Array ( [0] => 20170810 [1] => 14 [2] => 52 ) [15] => Array ( [0] => 20170810 [1] => 15 [2] => 60 ) [16] => Array ( [0] => 20170810 [1] => 16 [2] => 59 ) [17] => Array ( [0] => 20170810 [1] => 17 [2] => 54 ) [18] => Array ( [0] => 20170810 [1] => 18 [2] => 27 ) [19] => Array ( [0] => 20170810 [1] => 19 [2] => 24 ) [20] => Array ( [0] => 20170810 [1] => 20 [2] => 36 ) [21] => Array ( [0] => 20170810 [1] => 21 [2] => 35 ) [22] => Array ( [0] => 20170810 [1] => 22 [2] => 42 ) [23] => Array ( [0] => 20170810 [1] => 23 [2] => 32 ) [24] => Array ( [0] => 20170811 [1] => 00 [2] => 20 ) [25] => Array ( [0] => 20170811 [1] => 01 [2] => 11 ) [26] => Array ( [0] => 20170811 [1] => 02 [2] => 16 ) [27] => Array ( [0] => 20170811 [1] => 03 [2] => 2 ) [28] => Array ( [0] => 20170811 [1] => 04 [2] => 0 ) [29] => Array ( [0] => 20170811 [1] => 05 [2] => 0 ) [30] => Array ( [0] => 20170811 [1] => 06 [2] => 0 ) [31] => Array ( [0] => 20170811 [1] => 07 [2] => 0 ) [32] => Array ( [0] => 20170811 [1] => 08 [2] => 0 ) [33] => Array ( [0] => 20170811 [1] => 09 [2] => 0 ) [34] => Array ( [0] => 20170811 [1] => 10 [2] => 0 ) [35] => Array ( [0] => 20170811 [1] => 11 [2] => 0 ) [36] => Array ( [0] => 20170811 [1] => 12 [2] => 0 ) [37] => Array ( [0] => 20170811 [1] => 13 [2] => 0 ) [38] => Array ( [0] => 20170811 [1] => 14 [2] => 0 ) [39] => Array ( [0] => 20170811 [1] => 15 [2] => 0 ) [40] => Array ( [0] => 20170811 [1] => 16 [2] => 0 ) [41] => Array ( [0] => 20170811 [1] => 17 [2] => 0 ) [42] => Array ( [0] => 20170811 [1] => 18 [2] => 0 ) [43] => Array ( [0] => 20170811 [1] => 19 [2] => 0 ) [44] => Array ( [0] => 20170811 [1] => 20 [2] => 0 ) [45] => Array ( [0] => 20170811 [1] => 21 [2] => 0 ) [46] => Array ( [0] => 20170811 [1] => 22 [2] => 0 ) [47] => Array ( [0] => 20170811 [1] => 23 [2] => 0 ) )

4. 七月訪客數依來源別分組,數量大到小排序

  private function getResults($analytics, $profileId) {
    $optParams = array(
      'dimensions' => 'ga:medium',
      'sort' => '-ga:users',
     );
    return $analytics->data_ga->get(
      'ga:' . $profileId,
      '2017-07-01',
      '2017-07-31',
      'ga:users',
      $optParams);
  }

Array ( [0] => Array ( [0] => organic [1] => 15799 ) [1] => Array ( [0] => (none) [1] => 1814 ) [2] => Array ( [0] => referral [1] => 1538 ) )

5. 七月訪客數、SESSION數連結網址,數量大到小排序

  private function getResults($analytics, $profileId) {
    $optParams = array(
      'dimensions' => 'ga:pagePath',
      'sort' => '-ga:users',
      'max-results' => '10'
     );
    return $analytics->data_ga->get(
      'ga:' . $profileId,
      '2017-07-01',
      '2017-07-31',
      'ga:users,ga:sessions',
      $optParams);
  }

結果

Array ( [0] => Array ( [0] => /content/index/10710 [1] => 477 [2] => 513 ) [1] => Array ( [0] => /content/index/10821 [1] => 451 [2] => 581 ) [2] => Array ( [0] => /content/index/10047 [1] => 447 [2] => 502 ) [3] => Array ( [0] => /content/index/10241 [1] => 342 [2] => 414 ) [4] => Array ( [0] => /content/index/10442 [1] => 316 [2] => 334 ) [5] => Array ( [0] => /content/index/10313 [1] => 314 [2] => 349 ) [6] => Array ( [0] => /content/index/10686 [1] => 302 [2] => 307 ) [7] => Array ( [0] => /content/index/11450 [1] => 298 [2] => 303 ) [8] => Array ( [0] => /content/index/10320 [1] => 293 [2] => 378 ) [9] => Array ( [0] => /content/index/10310 [1] => 267 [2] => 278 ) )

6. 七月SESSION數中連結網址帶有'u=note'的參數

  private function getResults($analytics, $profileId) {
    $optParams = array(
      'dimensions' => 'ga:pagePath',
      'filters' => 'ga:pagePath=~u=note',
      'max-results' => '10'
     );
    return $analytics->data_ga->get(
      'ga:' . $profileId,
      '2017-07-01',
      '2017-07-31',
      'ga:sessions',
      $optParams);
  }

結果

Array ( [0] => Array ( [0] => /?u=note [1] => 132 ) [1] => Array ( [0] => /?u=note&s=timeline [1] => 4 ) )

結論

google analytics的細節非常多,我知道的也很有限,只能見招拆招,取得了raw data後你可以把他交給前端的js去畫圖,Google chart[3]提供了非常完整的繪圖工具,但我了解的更有限無法提供範例。

上面提供幾個範例都是我會想用到的,其餘的請參考下面的連結。

參考資料

[1] 所有參數的說明文件 https://developers.google.com/analytics/devguides/reporting/core/v3/reference

[2] Dimensions and Metrics https://developers.google.com/analytics/devguides/reporting/core/dimsmets#view=detail&group=user

[3] Google chart https://developers.google.com/chart/interactive/docs/

你可能感興趣的文章

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

[PHP] 將字串的部分加上遮罩的函式 有些資料會想要隱藏部分的字元,例如身分證號或信用卡號、電話等。我寫了一個函式來完成這個功能。

[CI3] 使用和修改日曆類別 calendar library CI3有一個日曆圖書館 library,可以快速的建立一個日曆,但是想修改讓他更棒

[PHP] 使用browscap檢查瀏覽器版本 使用PHP內建函數browscap檢查瀏覽器版本

PHP程式經驗 #1 -- 靜態的物件比動態物件快 驗證 1. 使用動態物件函式 > 使用靜態物件函式。 2. 使用有宣告的靜態物件函式 > 使用動態無宣告的靜

[PHP] 檢查檔案是否是圖檔 使用getimagesize函數檢查檔案是否是圖檔

PHP判斷對方瀏覽器語系 多語系的網頁應該主動偵查瀏覽器的語系,配合使用者跳出合適的語系。

[PHP] 如何寫callback function 召回函數(回呼函數) PHP如何寫召回函數或回呼函數(callback function)?

[CodeIgniter 3] COOKIE的使用 PHP CodeIgniter 3 中COOKIE的使用超簡單

作業上傳程式 提供學生作業上傳的程式

我有話要說


限制:留言最高字數1000字,超過部分會被截掉。請注意:留言不可帶有網址,會被濾掉。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[Freebsd] 定時測試 ADSL 是否斷線並重連 中華電信 ADSL 雖有固定 ip,可是他卻會不定時「斷線」, 使用以下的 方法可以定時測試是否斷線,以及重新撥接。

PHP for sphinx 函式庫安裝 PECL/sphinx PHP>= 5.2.2 已經能原生支援 sphinx,可是預設的沒有裝,我們得自己裝才能用

[札記] 2016.7~12月札記 札記,只是札記

一個邏輯的錯誤刪了全部檔案的經驗 今天本來想做一件很簡單的事,但卻足足浪費我多一倍的時間,再加上刪掉我全部的檔案,原因只是因為我自己的邏輯錯誤。

好用的3+2碼郵遞區號查詢系統推薦 網路上找到用地址輸入判斷3+2碼郵遞區號的辨識率不高,除了這個網站…