Google Analytics(分析)API:为什么API数据与Google Analytics(分析)仪表板上显示的数据不同? [英] Google Analytics API: Why is the API data different than what's being seen on the Analytics Dashboard?

查看:69
本文介绍了Google Analytics(分析)API:为什么API数据与Google Analytics(分析)仪表板上显示的数据不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了一段时间,从我收集的数据来看,它与 samplingLevel 有关.

I've been researching this for a little while now, and from what I gather, it has something to do with samplingLevel.

我从其他大多数stackoverflow问题中收集到的问题是,除非拥有高级帐户,否则数据将始终按抽样方式返回.

The issue I'm gathering from most other stackoverflow questions is that unless I have a Premium Account, the data will always come back as sampled.

值得一提的是,是否有任何方法可以更改我的Google API查询,以便使数据更加准确?

It is worth asking, is there anyway to can alter my Google API query so the data is a little more accurate?

我的查询代码:

$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缺失以及其他多个错误.我意识到我可能没有正确地执行查询,但是任何可以指出编写我的查询的正确方法的人都会有所帮助.那并且这种方法可行吗?还是我需要高级帐户才能完成我想做的事情?

But the query breaks and says the id's are missing along with multiple other errors. I realize that I'm probably not making the query right, but anyone who can point out what the proper way to write my query would be helping out greatly. That and is this approach possible? Or will I need a Premium Account to accomplish what I'm trying to do?

推荐答案

采样

在给定时间段内会话或事件的数量很多时,往往会发生采样. 处理采样的选项:

Sampling

Sampling tends to occur when you have high number of sessions or events for a given time period. Options to handle sampling:

  • 缩短日期范围.
  • 减少尺寸数.
  • 增加samplingLevel.

通过检查字段

Take the guess work out things and verify if your results containSampledData by checking the response for the field containsSampledData. Also in your query you are requesting today's data, in the UI by default they show you up to Yesterday's data. Today's data is still coming in, so depending on when you query the API you will get a different answer for the number of sessions.

您的代码存在一些问题.我建议您看一些文档中的示例和查看参考文档以了解API的结构.例如,您需要将可选参数作为数组传递:

There are some issues with your code. I would suggest looking at some of the examples in the docs and look at the reference docs to understand how the API is structured. For example you need to pass in the optional parameters as an array:

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受限制和配额,因此,如果您有10个以上的视图(配置文件),您的API将返回一个限速错误,以免查询速度过快.实施速率限制和指数补偿是一种很好的做法.

Words of warning, the API is subject to Limits and Quotas, so if you have more than 10 Views (profiles) your API will return a ratelimiting error for querying too quickly. It is good practice to implement rate limiting and exponential backoff.

我们都喜欢拥有闪亮的新玩具.继续考虑迁移到 Analytics Reporting API V4 .您已经完成了 OAuth 的辛苦工作,它们为您提供了一个很好的解决方案迁移指南

We all like to have the shiny new toy. Go ahead think about migrating to Analytics Reporting API V4. You've already done the hard work of figuring out OAuth, And they made available a great Migration Guide

StackOverflow是获得实现帮助的好地方,并且在包括代码方面做得很好(您会惊讶地发现有很多人没有这样做).我还建议您包括错误响应,堆栈跟踪以及在线查看的资源.

StackOverflow is a great place to get help with your implementations, and you did great job of including your code (you'd be surprised how many people don't). I would also recommend including your error responses, stacktraces as well as the resources you've seen online.

这篇关于Google Analytics(分析)API:为什么API数据与Google Analytics(分析)仪表板上显示的数据不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆