I use the google analytics example and I'd like to get list of my keywords. Sorry I am very new to analytics API and couldn't find it the reference. Here is the code copied from there:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$analytics = initializeAnalytics();
$response = getReport($analytics);
printResults($response);
// Initializes an Analytics Reporting API service object.
function initializeAnalytics()
{
}
// Queries the Analytics Reporting API V4.
function getReport($analytics) {
$VIEW_ID = "xxxx";
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );
}
// Parses and prints the Analytics Reporting API V4 response.
function printResults($reports) {
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");
}
for ($j = 0; $j < count($metrics); $j++) {
$values = $metrics[$j]->getValues();
for ($k = 0; $k < count($values); $k++) {
$entry = $metricHeaders[$k];
print($entry->getName() . ": " . $values[$k] . "\n");
}
}
}
}
}
This outputs the number of sessions for the last seven days for the given view but I would like to get the keywords instead.
You need to add dimensions
parameter to your API request. Something like
//Create the Dimensions object.
$keyword = new Google_Service_AnalyticsReporting_Dimension();
$keyword->setName("ga:keyword");
///...
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
// Here is an addition
$request->setDimensions(array($keyword));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
You can find more samples at the Analytics dev site: https://developers.google.com/analytics/devguides/reporting/core/v4/samples
please keep in mind that in this way you'll only get the data for paid keywords since organic keywords are not tracked in Analytics in most cases.