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

Google Analytics API date range request returning different data than native Google UI

我目前正在开发一种分析网络工具,该工具从各种网络分析 API 中提取并生成各种报告,其中之一是基于 Google 分析的报告。

我正在提取大量数据,所有数据都与我在 Google 的本机 UI 中看到的结果相匹配......好吧,除了一个数据项之外,这是针对 1 年前的 30 天段的日期范围查询。

我花了一些时间查看 Google 论坛和此处的 SO,但我没有发现任何其他人的日期范围查询数据不准确。我见过的大多数错误都与数据样本集和原始查询有关 (https://support.google.com/analytics/answer/1042498?hl=en)

如果我 var_dump() 我的结果 return 我看到开始和结束日期与我在 GA UI 中输入的开始和结束日期正确匹配。有谁知道这里会发生什么?我见过其他 SO 线程,其中用户似乎能够通过 Google Analytics native UI 获得与日期范围请求匹配的结果。

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

我得到的两个结果集并没有太大的不同,但足以让我的客户担心。 API 请求与本机 GA UI 是否可能使用不同的时间/日期区域?如果是这样,我是否错过了在我的请求中发送参数?

可能Sampling是这样的。尝试将您的细分范围缩小到几天或几周。

我最终解决了这个问题。事实证明,有几个非常相似的分析结果,我们只是显示了错误的密钥。

具体来说,使用 'ga:sessions' 键的 organic 子键会产生与本机 GA 界面匹配的结果。我们最初一直将日期范围过滤器直接应用于 'ga:organicSearches' 键,结果显示非常相似,但确实存在细微差异,这就是我们的问题。

对于稍后阅读本文的任何人: 我们 PHP 对 Google API 的调用没有问题,如原始 post 所示。 结果证明,我们已经拥有所有正确的数据,但只是在前端调用了错误的值。