Google 分析 v4 - 无法获取维度

Google Analytic v4 - Cant get dimensions

我一直在摆弄 Google Analytics v 4,我 运行 陷入困境,似乎无法找到关于如何通过它的适当文档。

我希望从 Google Analytics 中获取一些简单的指标/维度,并且我已经毫无问题地获取了指标。我使用了来自 google 的 Google analytics v4 "Hello Analytic's Example"。

但是我无法使维度部分正常工作。我找不到任何关于它的信息,所以我想有人以前来过这里,并且在这方面的知识比我多一点。

我的工作指标代码:

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;
}

您尝试过以下方法吗?

// 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