Learn business growth with Google Analytics 4 Forums Google Analytics 4 How to connect to the Google Analytics 4 API using PHP

  • How to connect to the Google Analytics 4 API using PHP

    Posted by Hannah on 26 December 2022 at 5:06 pm

    Every time I try to make a request, I keep bumping into this error:

    `php
    PHP Fatal error: Uncaught GoogleServiceException: {
    “error”: {
    “code”: 403,
    “message”: “User does not have sufficient permissions for this profile.”,
    “errors”: [
    {
    “message”: “User does not have sufficient permissions for this profile.”,
    “domain”: “global”,
    “reason”: “forbidden”
    }
    ],
    “status”: “PERMISSION_DENIED”
    }
    }
    `
    To understand the error better, here’s a glimpse into what I did:

    1. I whipped up a service account on Google Cloud Platform and cooked up an API key.
    2. I fed the client_email into the Google Analytics count from which I wanted to barnstorm some data (sure, I used admin permission).
    3. I got the Google API client library for PHP (the google/apiclient) all set up.

    I even went along with the guide Google offers [here](https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php?hl=pt-br).
    Below, you’ll find a taste of the PHP code I used:

    `php
    public function __construct() {
    $analytics = $this->initializeAnalytics();
    $this->getReport($analytics);
    }

    function initializeAnalytics() {
    $KEY_FILE_LOCATION = __DIR__ . ‘/service-account-credentials.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 = “here I put my id”;

    $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 );
    }
    `

    Logan replied 1 year ago 3 Members · 2 Replies
  • 2 Replies
  • Anthony

    Member
    24 March 2023 at 1:05 pm

    The error message you’re seeing indicates that the service account with which you’re trying to access Google Analytics does not have the necessary permissions. Even though you used the client_email and granted the admin permission, this could still be caused by two key issues. Firstly, the service account email which you’ve set up in Google Cloud Platform might not have been added to Google Analytics. Make sure you’ve added it with ‘Read & Analyze’ permissions in the Google Analytics account. Secondly, the “VIEW_ID” you’re using in your PHP code may not be valid or may not belong to the account you’ve given permission to. Make sure that the view account’s ID is correct and is the one you’ve granted access to for the service account.

  • Logan

    Member
    20 May 2023 at 11:46 am

    It seems like your service account doesn’t have the right permissions, despite you using admin permission. Make sure you’ve added the client_email from your service account to your Google Analytics with correct permissions. Also, check if your view’s ID is correct. If all else fails, try starting the process from scratch to catch any unseen errors.

Log in to reply.