user9295119
user9295119

Reputation: 13

Google Analytic v4 - Cant get dimensions

I have been messing around with Google Analytics v 4, and I have run into a wall, and seem unable to find proper documentation - on how to get past it.

I'm looking to get some simple metric / dimensions out of Google Analytics and I have gotten the metrics out with no problem. I used the Google analytics v4 "Hello Analytic's Example" from google.

However I am unable to get the dimension part working. I am unable to find any information about it, so figured somebody had been here before, and had a little more knowledge in this subject than me.

My working Metric Code:

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

Upvotes: 1

Views: 778

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117036

Have you tried the following?

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

Code ripped directly from the dimension and metric sample

Upvotes: 1

Related Questions