API Analytics returns incorrect date with correct values

Asked

Viewed 69 times

2

Using the Analytics SDK to perform queries, I enter the values for the dimensions, metrics, start-date and end-date.

I get the result of the query with the correct data as I checked in the panel Anaytics but the date is returned as if it were from the previous month.

In the example below, step as parameters the following dates conforms to documentation:

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

The consultation API:

$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',
                ]
            );

getAnalytics method:

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
        );
    }

returns the object:

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

The returned columns contain:

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)

I couldn’t find anything to indicate that there have been changes in API. Are there any parameters that should be passed?

  • Marcelo strange this, really! For the purpose of testing, did you try to change the parameter format? try to pass this way and see if it works start-date=7daysAgo end-date=Today to know if the cause is really the date range. Is the correct Timezone set up in your Analytics dashboard? https://support.google.com/analytics/answer/1010249?hl=en

  • The problem also occurs using the nomenclatures today, yesterday and NdaysAgo, @Matthew. I checked Timezone and it’s correct GMT -3 - MACEIÓ, I did a test pulling values of the month of January and the month returned is 00

  • 1

    To solve the problem, use the value of dates in timestamp format (always coming from PHP to ensure that it is loaded with the server Timezone), and remember that in javascript, Sunday is 0, Monday is 1, and so on. and the months are also counted from 0 to 11. So always add "+ 1".

  • An example of output: $dtimeFinal = DateTime::createFromFormat("d/m/Y H:i:s", "2016-02-01");
$saidaFinal = $dtimeFinal->getTimestamp(); If you use timestamp, you don’t need to do any of that. You just need to compare dates. Important, if dates are generated at different times, it is necessary to reset the milliseconds: DateTime::createFromFormat("d/m/Y H:i:s.uO", "2016-02-01");

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.