dongsimang4036 2015-10-14 23:46 采纳率: 100%
浏览 76
已采纳

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器