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条回答 默认 最新

  • dqv2743 2015-10-20 16:13
    已采纳

    I did eventually fix this. What turned out to be the case was there are several very similar analytics results and we were simply showing the wrong key.

    Specifically, using the organic subkey to the 'ga:sessions' key produced matching results with the native GA interface. We had orignally been applying our date range filter directly to the 'ga:organicSearches' key which showed very similar results but it did have minor differences and therein was our problem.

    For anyone reading this later: There was no problem with our PHP calls to the Google API as shown in original post. As it turned out, we already had all the correct data but were merely calling up the wrong value on the frontend.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douju4278 2015-10-15 08:43

    May be Sampling is the case. Try to narrow your segment to days or weeks.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题