Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppHttpControllers;
- use AppHttpControllersController;
- use IlluminateHttpRequest;
- class HomeController extends Controller {
- public function index(Request $request)
- {
- $analytics = $this->initializeAnalytics();
- $response = $this->getReport($analytics);
- echo '<pre>', print_r($response) ,'</pre>';exit;
- return view('index');
- }
- private function initializeAnalytics()
- {
- $client = new Google_Client();
- $client->setHttpClient(new GuzzleHttpClient([
- 'verify' => false
- ]));
- $client->setApplicationName("Rezzta Analytics Reporting");
- $client->setAuthConfig(realpath(base_path(env('GA_JSON'))));
- $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
- $analytics = new Google_Service_AnalyticsReporting($client);
- return $analytics;
- }
- private 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");
- $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 );
- }
- }
- Google_Service_Exception in REST.php line 118:
- {
- "error": {
- "code": 403,
- "message": "User does not have any Google Analytics account.",
- "errors": [
- {
- "message": "User does not have any Google Analytics account.",
- "domain": "global",
- "reason": "forbidden"
- }
- ],
- "status": "PERMISSION_DENIED"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement