0

I use laravel 5.3 for this project. I have a google analytics api on an account and have another gmail account. I want to give access permission to api to second gmail account and I added it to users.

i have a credential json file and all codes are below :

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

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 \GuzzleHttp\Client([
            '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 );
    }
}

but I got this error :

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"
}
}

where is the problem ?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Hanik
  • 317
  • 2
  • 6
  • 25

1 Answers1

0

"User does not have any Google Analytics account.

Means that the user you have authenticated with does has not created a Google analytics account yet or does not have access to the view you are trying to access.

You can grant this user access by going to the google analytics website under the admin section and granting them access to the view in question.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449