使用Analytics API执行查询,通知dimensionsmetricsstart-dateend-date的值。
在anaytics面板中使用正确的数据获取查询结果,但返回的日期与上个月的日期相同。
在下面的示例中,步骤作为参数,以下日期作为documentation

'start-date' => string '2016-06-01' (length=10)
'end-date' => string '2016-06-07' (length=10)

代码咨询:
$lastDays     = $this->getAnalytics(
                $viewId     = $this->getDefaultView(),
                $startDate  = '2016-06-01',
                $endDate    = '2016-06-07',
                $metrics    = 'ga:sessions, ga:bounces, ga:goalCompletionsAll, ga:pageviews',
               [
                    'dimensions'    => 'ga:date',
                    'output'        => 'dataTable',
                ]
            );

API方法:
public function getAnalytics($viewId = 0, $startDate = null, $endDate = null, $metrics = null, $options = null){
        if(!$viewId){
            return false;
        }
        $GoogleAnalyticsAPI = Yii::$app->GoogleAnalytics->auth();

        return $GoogleAnalyticsAPI->data_ga->get(
            'ga:' . $viewId,
            $startDate,
            $endDate,
            $metrics,
            $options
        );
    }

返回对象:
object(backend\modules\aplicacao\lib\google\src\Google\Service\Google_Service_Analytics_Accounts)[156]
  protected 'collection_key' => string 'items' (length=5)
  protected 'internal_gapi_mappings' =>
    array (size=0)
      empty
  protected 'itemsType' => string 'Google_Service_Analytics_Account' (length=32)
  protected 'itemsDataType' => string 'array' (length=5)
  public 'itemsPerPage' => int 1000
  public 'kind' => string 'analytics#gaData' (length=16)
  public 'nextLink' => null
  public 'previousLink' => null
  public 'startIndex' => null
  public 'totalResults' => int 7
  public 'username' => null
  protected 'modelData' =>
    array (size=8)
      'id' => string 'xxxxxxxxxxxxxxxxxx' (length=190)
      'query' =>
        array (size=7)
          'start-date' => string '2016-06-01' (length=10)
          'end-date' => string '2016-06-07' (length=10)
          'ids' => string 'xxxxxxxxxxxxxxxxxx' (length=11)
          'dimensions' => string 'ga:date' (length=7)
          'metrics' =>
            array (size=4)
              ...
          'start-index' => int 1
          'max-results' => int 1000
      'selfLink' => string 'xxxxxxxxxxxxxxxxxx' (length=190)
      'profileInfo' =>
        array (size=6)
          'profileId' => string 'xxxxxxxxxxxxxxxxxx' (length=8)
          'accountId' => string 'xxxxxxxxxxxxxxxxxx' (length=8)
          'webPropertyId' => string 'xxxxxxxxxxxxxxxxxx' (length=14)
          'internalWebPropertyId' => string 'xxxxxxxxxxxxxxxxxx' (length=8)
          'profileName' => string 'xxxxxxxxxxxxxxxxxx' (length=16)
          'tableId' => string 'xxxxxxxxxxxxxxxxxx' (length=11)
      'containsSampledData' => boolean false
      'columnHeaders' =>
        array (size=5)
          0 =>
            array (size=3)
              ...
          1 =>
            array (size=3)
              ...
          2 =>
            array (size=3)
              ...
          3 =>
            array (size=3)
              ...
          4 =>
            array (size=3)
              ...
      'totalsForAllResults' =>
        array (size=4)
          'ga:sessions' => string '137' (length=3)
          'ga:bounces' => string '75' (length=2)
          'ga:goalCompletionsAll' => string '0' (length=1)
          'ga:pageviews' => string '447' (length=3)
      'dataTable' =>
        array (size=2)
          'cols' =>
            array (size=5)
              ...
          'rows' =>
            array (size=7)
              ...
  protected 'processed' =>
    array (size=0)
      empty

使用列值:
array (size=1)
  'c' =>
    array (size=5)
      0 =>
        array (size=1)
          'v' => string 'Date(2016, 05, 01)' (length=18)
      1 =>
        array (size=1)
          'v' => string '28' (length=2)
      2 =>
        array (size=1)
          'v' => string '15' (length=2)
      3 =>
        array (size=1)
          'v' => string '0' (length=1)
      4 =>
        array (size=1)
          'v' => string '100' (length=3)
array (size=1)
  'c' =>
    array (size=5)
      0 =>
        array (size=1)
          'v' => string 'Date(2016, 05, 02)' (length=18)
      1 =>
        array (size=1)
          'v' => string '20' (length=2)
      2 =>
        array (size=1)
          'v' => string '6' (length=1)
      3 =>
        array (size=1)
          'v' => string '0' (length=1)
      4 =>
        array (size=1)
          'v' => string '81' (length=2)
array (size=1)
  'c' =>
    array (size=5)
      0 =>
        array (size=1)
          'v' => string 'Date(2016, 05, 03)' (length=18)
      1 =>
        array (size=1)
          'v' => string '31' (length=2)
      2 =>
        array (size=1)
          'v' => string '21' (length=2)
      3 =>
        array (size=1)
          'v' => string '0' (length=1)
      4 =>
        array (size=1)
          'v' => string '89' (length=2)
array (size=1)
  'c' =>
    array (size=5)
      0 =>
        array (size=1)
          'v' => string 'Date(2016, 05, 04)' (length=18)
      1 =>
        array (size=1)
          'v' => string '20' (length=2)
      2 =>
        array (size=1)
          'v' => string '17' (length=2)
      3 =>
        array (size=1)
          'v' => string '0' (length=1)
      4 =>
        array (size=1)
          'v' => string '35' (length=2)
array (size=1)
  'c' =>
    array (size=5)
      0 =>
        array (size=1)
          'v' => string 'Date(2016, 05, 05)' (length=18)
      1 =>
        array (size=1)
          'v' => string '10' (length=2)
      2 =>
        array (size=1)
          'v' => string '5' (length=1)
      3 =>
        array (size=1)
          'v' => string '0' (length=1)
      4 =>
        array (size=1)
          'v' => string '36' (length=2)
array (size=1)
  'c' =>
    array (size=5)
      0 =>
        array (size=1)
          'v' => string 'Date(2016, 05, 06)' (length=18)
      1 =>
        array (size=1)
          'v' => string '11' (length=2)
      2 =>
        array (size=1)
          'v' => string '5' (length=1)
      3 =>
        array (size=1)
          'v' => string '0' (length=1)
      4 =>
        array (size=1)
          'v' => string '37' (length=2)
array (size=1)
  'c' =>
    array (size=5)
      0 =>
        array (size=1)
          'v' => string 'Date(2016, 05, 07)' (length=18)
      1 =>
        array (size=1)
          'v' => string '17' (length=2)
      2 =>
        array (size=1)
          'v' => string '6' (length=1)
      3 =>
        array (size=1)
          'v' => string '0' (length=1)
      4 =>
        array (size=1)
          'v' => string '69' (length=2)

最佳答案

你试过用相对法吗?如果且仅当希望更正上述查询时,请尝试使用

&start-date=9daysAgo
  &end-date=today

关于php - Analytics API返回不正确的日期和正确的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37991803/

10-12 04:23