通过API提取Google Analytics多渠道路径数据

时间:2015-07-23 20:09:33

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

我正在尝试从Google AnalyticsAPI中提取多渠道漏斗报告,并收到以下错误:

无效的价值' mcf:source'。值必须与以下正则表达式匹配:'(ga:。+)?'

这是我正在使用的代码,它在尺寸和尺寸方面工作正常。指标来自ga:...系列,但出于某种原因,它不会让我拉mcf:reports。

$analytics = new Google_Service_Analytics($client);
$analytics_id   = 'ga:XXXXXXXX';
$lastWeek       = date('Y-m-d', strtotime('-26 day', time()));
$today          = date('Y-m-d', strtotime('-26 day', time()));

try {
    $optParams = array();
    $optParams['dimensions'] = "mcf:source";
    #$optParams['sort'] = "";
    $optParams['max-results'] = "10000";
    $metrics = 'mcf:totalConversions';
    $results = $analytics->data_ga->get($analytics_id,
                       $lastWeek,
                       $today,$metrics,$optParams);

    $rows = $results->getRows();
    foreach ($results->columnHeaders as $header) {
        $headerName = ucwords(preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', str_replace('ga:', '', $header->name)));
        printf('%s', $headerName);
        print ',';
    }

    print "\n";

    foreach ($results->rows as $row) {
        foreach ($row as $cell) {
            printf('%s', $cell);
            print ',';
        }
        print "\n";
    }
} 

1 个答案:

答案 0 :(得分:3)

从我的头脑中我会说这是因为您使用$analytics->data_ga->get()代替$analytics->data_mcf->get()(多渠道数据拥有自己的API)。