Google Analytic v4 - 无法获得维度

时间:2018-01-31 14:56:18

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

我一直在搞乱谷歌分析第4版,而且我遇到了障碍,似乎无法找到适当的文档 - 关于如何通过它。

我希望从Google Analytics中获取一些简单的指标/维度,并且我已经毫不费力地获得指标。我使用谷歌的谷歌分析v4“Hello Analytic的例子”。

但是我无法使尺寸部件正常工作。我无法找到任何有关它的信息,所以想到有人曾经在这里,并且比我更了解这个主题。

我的工作指标代码:

function initializeAnalytics()
{
    $sKeyFileLocation = get_stylesheet_directory() . '/service-account-credentials.json';
    // echo get_stylesheet_directory() . '/service-account-credentials.json';
    // Create and configure a new client object.
    $client = new Google_Client();
    $client->setApplicationName("stats.laybackcph.dk");
    $client->setAuthConfig($sKeyFileLocation);
    $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
    $analytics = new Google_Service_AnalyticsReporting($client);

    return $analytics;
}
function getMetricReport($analytics, $iViewID, $aMetrics, $sStartDate, 
$sEndDate) 
{
      // Create the DateRange object.
    $dateRange = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange->setStartDate($sStartDate);
    $dateRange->setEndDate($sEndDate);

    $aMetricResults = array();
    foreach($aMetrics as $sMetric => $sAlias)
    {
        // Create the Metrics object.
        $sessions = new Google_Service_AnalyticsReporting_Metric();
        $sessions->setExpression($sMetric);
        $sessions->setAlias($sAlias);

        // Create the ReportRequest object.
        $request = new Google_Service_AnalyticsReporting_ReportRequest();
        $request->setViewId($iViewID);
        $request->setDateRanges($dateRange);
        $request->setMetrics(array($sessions));

        $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
        $body->setReportRequests( array( $request) );
        $aMetricResults[$sAlias] = printResults($analytics->reports->batchGet($body)); 
        // return $analytics->reports->batchGet( $body );
    }
    return $aMetricResults;
}


/**
 * Parses and prints the Analytics Reporting API V4 response.
 *
 * @param An Analytics Reporting API V4 response.
 */
function printResults($reports) 
{   
    $aReport = array();
    for($reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++) 
    {
        $report = $reports[ $reportIndex ];
        $header = $report->getColumnHeader();
        $dimensionHeaders = $header->getDimensions();
        $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
        $rows = $report->getData()->getRows();

        for($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) 
        {
            $row = $rows[ $rowIndex ];
            $dimensions = $row->getDimensions();
            $metrics = $row->getMetrics();
            for($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) 
            {
            // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
                $aReport[$dimensionHeaders[$i]] = $dimensions[$i];
            }

            for($j = 0; $j < count($metrics); $j++) 
            {
                $values = $metrics[$j]->getValues();
                for($k = 0; $k < count($values); $k++) 
                {
                    $entry = $metricHeaders[$k];
                    $aReport[$entry->getName()] = $values[$k];
                }
            }
        }
    }
    return $aReport;
}

1 个答案:

答案 0 :(得分:1)

您是否尝试过以下操作?

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2015-06-15");
$dateRange->setEndDate("2015-06-30");

// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");

//Create the Dimensions object.
$browser = new Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:browser");

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("XXXX");
$request->setDateRanges($dateRange);
$request->setDimensions(array($browser));
$request->setMetrics(array($sessions));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analyticsreporting->reports->batchGet( $body );

代码直接从dimension and metric sample

中删除