Google Analytics Report API串接

2021/09/02

套件使用

PHP的套件使用的是google/apiclient

所有的google api串接都可以透過此套件來使用

https://github.com/googleapis/google-api-php-client

 

Google Api Client初始化

授權方式

使用setAuthConfig method來設定service account金鑰

 

viewID

google的中文翻譯為資料檢視 ID

這個ID可在GA報表的管理>資料檢視設定中找到

 

初始化範例

<?php
    protected function init()
    {
        $this->client = new Google_Client();
        $this->client->setApplicationName('GoogleAnalyticReport');
        $this->client->setAuthConfig('/path/to/service-account-key.json'); // 設定service account json金鑰位置
        $this->client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); // 設定scope
        $this->viewId = '123456'; // 設定資料檢視 ID
    }

 

開始撈資料

情境:篩選時間區間內的指定維度、指標,並做排序

維度:事件類型(eventCategory), 事件標籤(eventLabel), 自訂維度1(dimension1)

指標:自訂指標1(metric1)

 

<?php
    public function getReport($start, $end)
    {
        $this->init();
        $this->analytics = new \Google_Service_AnalyticsReporting($this->client);

        $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
        $dateRange->setStartDate($start);
        $dateRange->setEndDate($end);

        // dimension - 事件類型
        $eventCategory = new \Google_Service_AnalyticsReporting_Dimension();
        $eventCategory->setName('ga:eventCategory');

        // dimension - 事件標籤
        $eventLabel = new \Google_Service_AnalyticsReporting_Dimension();
        $eventLabel->setName('ga:eventLabel');

        // dimension - 自訂維度
        $userType = new \Google_Service_AnalyticsReporting_Dimension();
        $userType->setName('ga:dimension1');

        $this->reportRequest = new \Google_Service_AnalyticsReporting_ReportRequest();
        $this->reportRequest->setViewId($this->viewId);
        $this->reportRequest->setDateRanges($dateRange);
        $this->reportRequest->setDimensions([$eventLabel, $userType]);

        // metrics
        $time = new \Google_Service_AnalyticsReporting_Metric();
        $time->setExpression('ga:metric1');
        $this->reportRequest->setMetrics([$time]);
        $this->reportRequest->setPageSize(300);

        $this->setFilter(); // 設定filter
        $this->setOrder(); // 設定排序

        $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
        $body->setReportRequests([$this->reportRequest]);
        $responses = $this->analytics->reports->batchGet($body);
        $report = $responses->getReports();
        dd($report);
    }

 

篩選

詳細維度篩選方式

可參考官方文件DimensionFilter段落

<?php
    protected function setFilter()
    {
        // 篩選維度 - eventCategory符合videoWatchDuration的資料
        $dimensionFilterClauses = new \Google_Service_AnalyticsReporting_DimensionFilterClause();
        $filterVideoWatch = new \Google_Service_AnalyticsReporting_DimensionFilter();
        $filterVideoWatch->setDimensionName('ga:eventCategory');
        $filterVideoWatch->setOperator('IN_LIST');
        $filterVideoWatch->setExpressions(['videoWatchDuration']);
        $dimensionFilterClauses->setFilters([$filterVideoWatch]);
        $this->reportRequest->setDimensionFilterClauses($dimensionFilterClauses);
    }

 

排序

排序類型(遞增、遞減)設定

可參考官方文件SortOrder段落

<?php
    protected function setOrder()
    {
        $orderBy = new \Google_Service_AnalyticsReporting_OrderBy();
        $orderBy->setFieldName('ga:metric1');
        $orderBy->setSortOrder('DESCENDING');
        $this->reportRequest->setOrderBys([$orderBy]);
    }

 

Google Analytics Query Explorer

https://ga-dev-tools.web.app/query-explorer/

GA的維度、指標很多

篩選的方式都不同

因此可使用Google Analytics Query Explorer來找維度、指標的值

或是預覽搜尋的結果

 

filter

query explorer的filter可使用==來做篩選

多個filter條件則使用分號(;)即可

 

情境1: 篩選eventCategory為foobar的資料

ga:eventCategory==foobar

 

情境2: 篩選eventCategory為foobar且自訂維度1為abc的資料

ga:eventCategory==foobar;ga:dimension1==abc

 

參考

https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php