dongsimang4036
2015-10-14 23:46
浏览 74
已采纳

Google AnalyticsAPI日期范围请求返回的数据与本机Google UI不同

I'm currently working on an analytics web tool that pulls from various web analytics APIs and churns out various reports, one of which is a Google Analytics based report.

I'm pulling in a lot of data, and all of it matches up with the results I see in Google's native UI...well, all EXCEPT one data item, which is a date range query for a 30 day segment exactly 1 year ago.

I've spent time looking over Google forums and here on SO but I haven't found anyone else having data inaccuracies for date range queries. Most of the inaccuracies I've seen have related to data sample sets vs raw queries ( https://support.google.com/analytics/answer/1042498?hl=en )

If I var_dump() my result return I see that the start and end dates are correctly matching the start and end date I entered in the GA UI. Does anyone know what could be happening here? I've seen other SO threads where users have seemed able to get matched results with Google Analytics native UI for date range requests.

if(!$cached) {
        $analytics_svc = new Google_Service_Analytics($client);

        $metrics = [
            'ga:sessions',
            'ga:pageviews',
            'ga:bounces',
            'ga:avgSessionDuration',
            'ga:avgPageLoadtime',
            'ga:bounceRate',
            'ga:goalConversionRateAll',
            'ga:organicSearches'
        ];

        $dimensions = [
            'ga:date',
            'ga:source',
            'ga:medium',
            'ga:socialNetwork'
        ];

        $metrics = implode(',', $metrics);
        $dimensions = implode(',', $dimensions);

        $from = date('Y-m-d', strtotime('-31 days'));
        $to = date('Y-m-d', time() - 86400);
        $from_lm = date('Y-m-d', strtotime('-61 days'));
        $to_lm = date('Y-m-d', strtotime('-31 days'));
        $from_ly = date('Y-m-d', strtotime('-1 year -31 days'));
        $to_ly = date('Y-m-d', strtotime('-1 year -1 day'));

        try {
           $adwords_metrics = implode(','['ga:CPC','ga:CTR','ga:impressions','ga:adClicks']);
           $adwords_dimensions = implode(',', []);
           $adwords = $analytics_svc->data_ga->get('ga:' . $profile->profile, $from, $to, $adwords_metrics, ['dimensions' => $adwords_dimensions]);
           $analytics = $analytics_svc->data_ga->get('ga:' . $profile->profile, $from, $to, $metrics, compact('dimensions'));
           $analytics_lm = $analytics_svc->data_ga->get('ga:' . $profile->profile, $from_lm, $to_lm, $metrics, compact('dimensions'));
           $analytics_ly = $analytics_svc->data_ga->get('ga:' . $profile->profile, $from_ly, $to_ly, $metrics, compact('dimensions'));
        } catch(Exception $e) {
           Session::flush();
           Session::regenerate();

           Flash::error('An error was encountered while attempting to read from Google Analytics.<br/>If the problem persists, '.
           '<a href="https://accounts.google.com/logout?service=oz" target="_blank">log out of your Google account</a>'.
           ' and try again.<br/>' . $e->getMessage());
           return Redirect::to('/login');
       }
}

The two result sets I get aren't dramatically different, but enough to make my client worried. Is it possible that API requests vs the native GA UI could be using different time / date zones? And if so, have I missed sending a param in my request?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题