【发布时间】: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