【问题标题】:Google Analytics API date range request returning different data than native Google UIGoogle Analytics API 日期范围请求返回与本机 Google UI 不同的数据
【发布时间】:2015-10-14 23:46:36
【问题描述】:

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

我正在提取大量数据,所有这些数据都与我在 Google 原生 UI 中看到的结果相匹配……嗯,除了一个数据项,它是一个 30 天段的日期范围查询正好是 1 年前。

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

如果我var_dump() 我的结果返回,我会看到开始和结束日期与我在 GA UI 中输入的开始和结束日期正确匹配。有谁知道这里会发生什么?我已经看到其他 SO 线程,用户似乎能够通过 Google Analytics 原生 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是否可能使用不同的时间/日期区域?如果是这样,我是否错过了在请求中发送参数?

【问题讨论】:

    标签: php google-analytics google-api-php-client


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-14
        • 2018-10-05
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        相关资源
        最近更新 更多