Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('google-analytics/vendor/autoload.php');
- function initializeAnalytics($json_path) {
- // Use the developers console and download your service account
- // credentials in JSON format. Place them in this directory or
- // change the key file location if necessary.
- $KEY_FILE_LOCATION = $json_path;
- // Create and configure a new client object.
- $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) {
- // Replace with your view ID, for example XXXX.
- $VIEW_ID = "XXXXXXXX";
- // Create the DateRange object.
- // Create the DateRange object.
- $dateRange = new Google_Service_AnalyticsReporting_DateRange();
- $dateRange->setStartDate("7daysAgo");
- $dateRange->setEndDate("today");
- //Create the Dimensions object.
- $year = new Google_Service_AnalyticsReporting_Dimension();
- $year->setName("ga:year");
- $month = new Google_Service_AnalyticsReporting_Dimension();
- $month->setName("ga:month");
- $browser = new Google_Service_AnalyticsReporting_Dimension();
- $browser->setName("ga:browser");
- $country = new Google_Service_AnalyticsReporting_Dimension();
- $country->setName("ga:country");
- $search = new Google_Service_AnalyticsReporting_Dimension();
- $search->setName("ga:searchUsed");
- $page = new Google_Service_AnalyticsReporting_Dimension();
- $page->setName("ga:pagePath");
- // Create the Metrics object.
- $sessions = new Google_Service_AnalyticsReporting_Metric();
- $sessions->setExpression("ga:sessions");
- $sessions->setAlias("sessions");
- // Create the Metrics object.
- $pageviews = new Google_Service_AnalyticsReporting_Metric();
- $pageviews->setExpression("ga:pageviews");
- $pageviews->setAlias("pageviews");
- // Create the Metrics object.
- $timeOnPage = new Google_Service_AnalyticsReporting_Metric();
- $timeOnPage->setExpression("ga:timeOnPage");
- $timeOnPage->setAlias("timeOnPage");
- // Create the ReportRequest object.
- $requestSession = new Google_Service_AnalyticsReporting_ReportRequest();
- $requestSession->setViewId($VIEW_ID);
- $requestSession->setDateRanges($dateRange);
- $requestSession->setDimensions(array($year, $month, $browser, $search, $country));
- $requestSession->setMetrics(array($sessions));
- // Create the ReportRequest object.
- $requestPageviews = new Google_Service_AnalyticsReporting_ReportRequest();
- $requestPageviews->setViewId($VIEW_ID);
- $requestPageviews->setDateRanges($dateRange);
- $requestPageviews->setDimensions(array($year, $month, $page));
- $requestPageviews->setMetrics(array($sessions, $timeOnPage));
- $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
- $body->setReportRequests( array( $requestSession, $requestPageviews) );
- 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);
- echo '</pre>';
- 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++) {
- echo $dimensionHeaders[$i] . ": " . $dimensions[$i] . "<br>";
- }
- for ($j = 0; $j < count($metrics); $j++) {
- $values = $metrics[$j]->getValues();
- for ($k = 0; $k < count($values); $k++) {
- $entry = $metricHeaders[$k];
- echo $entry->getName() . ": " . $values[$k] . "<br>";
- }
- }
- }
- }
- }
- $analytics = initializeAnalytics($json_path);
- $response = getReport($analytics);
- printResults($response);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement