php - 谷歌分析 API : Why is the API data different than what's being seen on the Analytics Dashboard?

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

我已经研究了一段时间了,据我所知,它与 有关。采样级别 .

我从大多数其他 stackoverflow 问题中收集到的问题是,除非我拥有高级帐户,否则数据将始终作为采样返回。

值得一问,无论如何可以更改我的 Google API 查询以使数据更准确一些吗?

我的查询代码:

$profiles = $analytics->management_profiles
    ->listManagementProfiles('myID', '~all');

foreach ($profiles->getItems() as $profile) {
    $IDvalue = $profile->getId();
    array_push($profilesArray, $IDvalue);
}

foreach ($profilesArray as $p) {
    $results = $analytics->data_ga->get(
        'ga:' . $p,
        '7daysAgo',
        'today',
        'ga:sessions');

    $profileName = $results->getProfileInfo()->getProfileName();
    $rows = $results->getRows();
    $sessions = $rows[0][0];

    print "Profile Name: $profileName";
    echo "<br>";
    print "Total Sessions: $sessions";
    echo "<br><br>";
}

我尝试更改我的 get()到:
    $results = $analytics->data_ga->get(
        'ga:' . $p,
        '7daysAgo',
        'today',
        'ga:sessions',
        'samplingLevel:HIGHER_PRECISION');

我也试过:
    $results = $analytics->data_ga->get(
        'ga:' . $p,
        '7daysAgo',
        'today',
        'ga:sessions',
        'ga:samplingLevel==HIGHER_PRECISION');

但是查询中断并说缺少 id 以及其他多个错误。我意识到我可能没有正确地进行查询,但是任何能够指出编写查询的正确方法的人都会有很大帮助。那这种方法可行吗?或者我需要一个高级帐户来完成我想要做的事情吗?

最佳答案

采样

当您在给定时间段内有大量 session 或事件时,往往会进行采样。
处理采样的选项:

  • 缩短日期范围。
  • 减少维数。
  • 增加samplingLevel .

  • 猜一猜并验证您的结果是否containSampledData通过检查字段 containsSampledData 的响应.同样在您的查询中,您正在请求 今日数据 ,默认情况下,在 UI 中,它们会向您显示昨天的数据。今天的数据仍在传入,因此根据您查询 API 的时间,您将获得不同的 session 数答案。

    API 错误:

    您的代码存在一些问题。我建议看一些 examples in the docs并查看 reference docs了解 API 的结构。例如,您需要将可选参数作为数组传入:
    foreach ($profilesArray as $p) {
      $optParams = array(
          'dimensions' => 'ga:source,ga:keyword',
          'sort' => '-ga:sessions,ga:source',
          'filters' => 'ga:medium==organic',
          'max-results' => '25',
          'samplingLevel' => 'HIGHER_PRECISION');
    
      $results = $analytics->data_ga->get(
          'ga:' + $p,
          '7daysAgo',
          'today',
          'ga:sessions',
          $optParams);
    
      ...
      // Do something with the $results.
    }
    

    警告的话,API以Limits and Quotas为准,因此如果您有 10 个以上的 View (配置文件),您的 API 将返回一个速率限制错误以供查询太快。实现速率限制和指数退避是一种很好的做法。

    迁移到 Analytics Reporting API V4

    我们都喜欢拥有 Shiny 的新玩具。继续考虑迁移到 Analytics Reporting API V4 .你已经完成了弄清楚OAuth的艰苦工作, 他们提供了很棒的 Migration Guide

    StackOverflow 建议

    StackOverflow 是获得实现帮助的好地方,您在包含代码方面做得很好(您会惊讶于有多少人没有这样做)。我还建议包括您的错误响应、堆栈跟踪以及您在网上看到的资源。

    关于php - 谷歌分析 API : Why is the API data different than what's being seen on the Analytics Dashboard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38791878/

    相关文章:

    google-analytics - 谷歌分析 : How to get events over last 90 minutes

    php - 在 PHPDoc 中声明局部变量的属性

    php - 从 PHP 数组中删除特定的键值对

    php - 带命名空间的 DOMDocument

    php - 对联接表中的行内容求和

    python - 谷歌云自然语言 API 的参数

    javascript - 使用 Google Analytics 的 Vuejs 事件跟踪

    Android Webview 从 header 数据传递到 Google Analytics

    javascript - 如何生成带边界的谷歌地图?

    python - gRPC/proto Google 云客户端库与 GAPIC Google 云客户端库