Google Analytics v4 PHP SDK - BetaAnalyticsDataClient initialization fails silently with OAuth2 credentials

I’m working with the Google Analytics v4 PHP SDK and running into a weird issue. I’m trying to fetch analytics data from a website where I already have valid OAuth tokens. The problem happens when I create the BetaAnalyticsDataClient instance - the script just stops running without any error message.

My composer.json file:

{
    "require": {
        "google/analytics-admin": "*"
    }
}

analytics.php:

#!/usr/bin/env php
<?php

require_once 'vendor/autoload.php';

use Google\Auth\OAuth2;
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;

$authClient = new OAuth2([
    'scope' => 'https://www.googleapis.com/auth/analytics.readonly',
    "clientId" => "MY-CLIENT-ID",
    'redirectUri' => 'urn:ietf:wg:oauth:2.0:oob',
    "authorizationUri" => "https://accounts.google.com/o/oauth2/auth",
    "tokenCredentialUri" => "https://accounts.google.com/o/oauth2/token",
    "clientSecret" => "MY-CLIENT-SECRET"
]);

$authClient->setRefreshToken("MY-REFRESH-TOKEN");

$tokenData = $authClient->fetchAuthToken();

echo "Script reaches this point";

$analyticsClient = new BetaAnalyticsDataClient(['credentials' => $authClient]);

echo "Script never gets to this line";

The script execution stops right after trying to instantiate the BetaAnalyticsDataClient object. No exceptions are thrown and no error messages appear. Has anyone encountered this before? What could be causing this silent failure?

Had this exact issue last year working on analytics for one of our products. Silent death usually happens because of two things.

First, check your PHP error logs. Silent failures often aren’t actually silent - they just don’t show on screen. Look in your error.log file or turn on error reporting with error_reporting(E_ALL); at the top of your script.

Second, your composer setup’s wrong. You have google/analytics-admin but you need google/analytics-data for the BetaAnalyticsDataClient. The admin package is for managing GA4 properties, not reading data.

Update your composer.json:

{
    "require": {
        "google/analytics-data": "^0.12"
    }
}

Then run composer update.

Also, you’re using the old OAuth2 class directly. Try switching to the credential file approach instead - it’s more reliable. Create a service account JSON file and use:

$analyticsClient = new BetaAnalyticsDataClient([
    'keyFilePath' => 'path/to/service-account.json'
]);

This eliminates tons of auth edge cases that cause silent failures.

I’ve dealt with GA4 API headaches for years and got sick of wrestling with PHP SDKs that fail silently. These auth issues are way too common.

Sure, you need the right composer package like others mentioned, but you’ll hit more weird edge cases after that. OAuth token refresh timing, scope mismatches, credential caching - it never ends.

I ditched handling analytics data directly in PHP and moved everything to automated workflows. Much cleaner. Set up a workflow that authenticates with Google Analytics, pulls your data, and sends it wherever - database, API endpoint, whatever.

No more debugging mysterious SDK failures or managing refresh tokens in your code. Configure once and let it run on whatever schedule you want.

For our reporting dashboard, workflows pull GA4 data hourly and push it to our internal API. Zero maintenance compared to our old PHP setup that randomly broke.

Latenode handles this automation well - takes care of OAuth complexity and API calls so you don’t have to debug silent PHP failures: https://latenode.com

u might be missin google/analytics-data in your composer.json. i see you have google/analytics-admin but BetaAnalyticsDataClient is in the data package. that’s likely why it just stops - PHP can’t find the class and just dies.

The BetaAnalyticsDataClient constructor runs validation checks that’ll crash your app without proper error handling. Sure, there’s the obvious composer package problem, but I’ve seen this happen when your OAuth2 object isn’t fully set up before you pass it to the client. Try calling $authClient->getAccessToken() after setting your refresh token but before creating the analytics client. This forces OAuth2 to validate everything and refresh the token if it needs to. Memory issues are another culprit - Google’s client libraries eat up tons of memory when they start up. Throw ini_set('memory_limit', '256M'); at the top of your script to eliminate that possibility. I’ve also run into this with older PHP versions. You need PHP 7.4+ with specific extensions. Check you’ve got curl, json, and openssl loaded with php -m. For debugging, wrap your client creation in try-catch and register a shutdown function to catch fatal errors that don’t throw exceptions.

First, turn on PHP error display. Add ini_set('display_errors', 1); and ini_set('display_startup_errors', 1); right after your opening PHP tag. Silent failures usually mean fatal errors are getting suppressed.

Besides the composer package issue others mentioned, I’ve seen this exact thing when Google client libraries can’t write to temp directories for caching. The SDK tries caching auth tokens and metadata but fails silently if it can’t create files.

Check your temp directory permissions. Usually that’s /tmp but you can verify with sys_get_temp_dir(). Make sure your web server user can write there.

Try passing a custom temp directory in your client config:

$analyticsClient = new BetaAnalyticsDataClient([
    'credentials' => $authClient,
    'cache' => ['dir' => '/path/to/writable/cache/dir']
]);

One more thing - your OAuth scope looks right but double check your refresh token was generated with the analytics.readonly scope originally. Mismatched scopes can cause silent auth failures during client initialization.