PHP code example of aahmed / google-api-client-php-bundle
1. Go to this page and download the library: Download aahmed/google-api-client-php-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
aahmed / google-api-client-php-bundle example snippets
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Samiax\GoogleApiBundle\SamiaxGoogleApiBundle(),
);
// ...
}
// ...
}
/**
* @Route("/google/analytics", name="google_analytics")
*/
public function googleAnalyticsAction(Request $request)
{
$service = $this->get('samiax_google_api.google_client');
$googleClient = $service->getGoogleClient();
$googleClient->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = $service->analytics();
// Create the DateRange object.
$dateRange = new \Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("1daysAgo");
$dateRange->setEndDate("1daysAgo");
// Create the Metrics object.
$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([$sessions]);
$body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$request]);
echo $analytics->reports->batchGet($body)->getReports()[0]->getData()->getTotals()[0]->getValues()[0];
return new Response();
}