I have following code and its working fine.
<?php
require_once __DIR__ . '/vendor/autoload.php';
$analytics = initializeAnalytics();
$response = getReport($analytics);
printResults($response);
function initializeAnalytics()
{
$KEY_FILE_LOCATION = __DIR__ . '/Student-a2aeba87a13b.json';
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_AnalyticsReporting($client);
return $analytics;
}
function getReport($analytics) {
$VIEW_ID = "107034574";
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("30daysAgo");
$dateRange->setEndDate("today");
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:pageviews");
$sessions->setAlias("hits");
echo "<pre>";
$browser = new Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:pagePathLevel1");
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
//Create the Dimensions object.
/* $sessions->setExpression("ga:users"); --get total users
/$browser->setName("ga:userType");
$sessions->setExpression("ga:percentNewSessions"); ---new user session percentage
* $sessions->setExpression("ga:newUsers");
$sessions->setExpression("ga:sessions");
$sessions->setExpression("ga:bounceRate");--bounce tare
$sessions->setExpression("ga:pageviews"); //total page view
$sessions->setExpression("ga:visits");
*
*/
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
echo "<pre>";
// print_r($body);
return $analytics->reports->batchGet( $body );
}
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();
echo "<pre>";
// print_r($rows);
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] . "
");
}
for ($j = 0; $j < count( $metricHeaders ) && $j < count( $metrics ); $j++) {
$entry = $metricHeaders[$j];
$values = $metrics[$j];
print("Metric type: " . $entry->getType() . "
" );
for ( $valueIndex = 0; $valueIndex < count( $values->getValues() ); $valueIndex++ ) {
$value = $values->getValues()[ $valueIndex ];
print($entry->getName() . ": " . $value . "
");
}
}
}
}
}
Now i am getting all page count and unique page count.Now my problem is i need to get particular page view count for example login page signup page etc.Can any one help me how to get particular page count.Thanks