设置Google Analytics

时间:2013-11-07 19:25:18

标签: php google-analytics

我正在尝试在我的网站上获取自定义Google分析结果。我的授权完全正常。但是,当我测试我的脚本时,我收到以下错误:

  

致命错误:未捕获的异常'Google_ServiceException',消息'错误调用GET 5ddIg-G7w3W92o1Pcc“> https://www.googleapis.com/analytics/v3/data/ga?ids = ga%3A12428978a12428978w47700129p48054548& start-date = 2013年11月5日&安培;结束日期= 2013年11月7日和安培;度量= GA%3Avisits%2Cga%3Apageviews%2Cga%3Abounces%2Cga%3AentranceBounceRate%2Cga%3AvisitBounceRate%2Cga%3AavgTimeOnSite&安培;尺寸= GA%3Adate%2Cga %3Ayear%2Cga%3Amonth%2Cga%3Aday& key = AIzaSyDAo0pJuOoC0Ec_5ddIg-G7w3W92o1Pcc:(400)无效值'ga:12428978a12428978w47700129p48054548'。值必须与以下正则表达式匹配:'ga:[0-9] +''在public_html /中GoogleClientApi / src / io / Google_REST.php:66堆栈跟踪:#0 public_html / GoogleClientApi / src / io / Google_REST.php(36):Google_REST :: decodeHttpResponse(Object(Google_HttpRequest))#1 / public_html / GoogleClientApi / src / service / Google_ServiceResource.php(186):Google_REST :: execute(Object(Google_HttpRequest))#2 / home / apu in public_html / GoogleClientApi / src / io / Google_REST.p第66行hp

我使用的代码如下:

$projectId = 'XXX';

// metrics
$_params[] = 'date';
$_params[] = 'date_year';
$_params[] = 'date_month';
$_params[] = 'date_day';
// dimensions
$_params[] = 'visits';
$_params[] = 'pageviews';
$_params[] = 'bounces';
$_params[] = 'entrance_bounce_rate';
$_params[] = 'visit_bounce_rate';
$_params[] = 'avg_time_on_site';

$from = date('Y-m-d', time()-2*24*60*60); // 2 days
$to = date('Y-m-d'); // today
$metrics = 'ga:visits,ga:pageviews,ga:bounces,ga:entranceBounceRate,ga:visitBounceRate,ga:avgTimeOnSite';
$dimensions = 'ga:date,ga:year,ga:month,ga:day';
$data = $service->data_ga->get('ga:XXXXXXXX'.$projectId, $from, $to, $metrics, array('dimensions' => $dimensions));

foreach($data['rows'] as $row) {
   $dataRow = array();
   foreach($_params as $colNr => $column) echo $column . ': '.$row[$colNr].', ';
}

我错过了什么?

2 个答案:

答案 0 :(得分:0)

显示您的项目ID无效......

<br /><br />

说它应该匹配ga:[0-9]+<br />

  

当前值ga:12428978a12428978w47700129p48054548
项目ID:   a12428978w47700129p48054548

答案 1 :(得分:0)

您目前正在通过此

'ga:12428978a12428978w47700129p48054548'这是不正确的,您只需要p上的

通过此'ga:48054548'