Google Analytics Data V1beta Client - Class BetaAnalyticsDataClient (0.10.1)

Reference documentation and code samples for the Google Analytics Data V1beta Client class BetaAnalyticsDataClient.

Service Description: Google Analytics reporting data service.

This class is currently experimental and may be subject to changes.

Namespace

Google \ Analytics \ Data \ V1beta \ Client

Methods

__construct

Constructor.

Parameters
NameDescription
options array

Optional. Options for configuring the service API wrapper.

↳ apiEndpoint string

The address of the API remote host. May optionally include the port, formatted as "

↳ credentials string|array|FetchAuthTokenInterface|CredentialsWrapper

The credentials to be used by the client to authorize API calls. This option accepts either a path to a credentials file, or a decoded credentials file as a PHP array. Advanced usage: In addition, this option can also accept a pre-constructed Google\Auth\FetchAuthTokenInterface object or Google\ApiCore\CredentialsWrapper object. Note that when one of these objects are provided, any settings in $credentialsConfig will be ignored.

↳ credentialsConfig array

Options used to configure credentials, including auth token caching, for the client. For a full list of supporting configuration options, see Google\ApiCore\CredentialsWrapper::build() .

↳ disableRetries bool

Determines whether or not retries defined by the client configuration should be disabled. Defaults to false.

↳ clientConfig string|array

Client method configuration, including retry settings. This option can be either a path to a JSON file, or a PHP array containing the decoded JSON data. By default this settings points to the default client config file, which is provided in the resources folder.

↳ transport string|TransportInterface

The transport used for executing network requests. May be either the string rest or grpc. Defaults to grpc if gRPC support is detected on the system. Advanced usage: Additionally, it is possible to pass in an already instantiated Google\ApiCore\Transport\TransportInterface object. Note that when this object is provided, any settings in $transportConfig, and any $apiEndpoint setting, will be ignored.

↳ transportConfig array

Configuration options that will be used to construct the transport. Options for each supported transport type should be passed in a key for that transport. For example: $transportConfig = [ 'grpc' => [...], 'rest' => [...], ]; See the Google\ApiCore\Transport\GrpcTransport::build() and Google\ApiCore\Transport\RestTransport::build() methods for the supported options.

↳ clientCertSource callable

A callable which returns the client cert as a string. This can be used to provide a certificate and private key to the transport layer for mTLS.

batchRunPivotReports

Returns multiple pivot reports in a batch. All reports must be for the same GA4 Property.

The async variant is Google\Analytics\Data\V1beta\Client\BaseClient\self::batchRunPivotReportsAsync() .

Parameters
NameDescription
request Google\Analytics\Data\V1beta\BatchRunPivotReportsRequest

A request to house fields associated with the call.

callOptions array

Optional.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Analytics\Data\V1beta\BatchRunPivotReportsResponse
Example
use Google\Analytics\Data\V1beta\BatchRunPivotReportsRequest;
use Google\Analytics\Data\V1beta\BatchRunPivotReportsResponse;
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\ApiCore\ApiException;

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function batch_run_pivot_reports_sample(): void
{
    // Create a client.
    $betaAnalyticsDataClient = new BetaAnalyticsDataClient();

    // Prepare the request message.
    $request = new BatchRunPivotReportsRequest();

    // Call the API and handle any network failures.
    try {
        /** @var BatchRunPivotReportsResponse $response */
        $response = $betaAnalyticsDataClient->batchRunPivotReports($request);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

batchRunReports

Returns multiple reports in a batch. All reports must be for the same GA4 Property.

The async variant is Google\Analytics\Data\V1beta\Client\BaseClient\self::batchRunReportsAsync() .

Parameters
NameDescription
request Google\Analytics\Data\V1beta\BatchRunReportsRequest

A request to house fields associated with the call.

callOptions array

Optional.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Analytics\Data\V1beta\BatchRunReportsResponse
Example
use Google\Analytics\Data\V1beta\BatchRunReportsRequest;
use Google\Analytics\Data\V1beta\BatchRunReportsResponse;
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\ApiCore\ApiException;

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function batch_run_reports_sample(): void
{
    // Create a client.
    $betaAnalyticsDataClient = new BetaAnalyticsDataClient();

    // Prepare the request message.
    $request = new BatchRunReportsRequest();

    // Call the API and handle any network failures.
    try {
        /** @var BatchRunReportsResponse $response */
        $response = $betaAnalyticsDataClient->batchRunReports($request);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

checkCompatibility

This compatibility method lists dimensions and metrics that can be added to a report request and maintain compatibility. This method fails if the request's dimensions and metrics are incompatible.

In Google Analytics, reports fail if they request incompatible dimensions and/or metrics; in that case, you will need to remove dimensions and/or metrics from the incompatible report until the report is compatible.

The Realtime and Core reports have different compatibility rules. This method checks compatibility for Core reports.

The async variant is Google\Analytics\Data\V1beta\Client\BaseClient\self::checkCompatibilityAsync() .

Parameters
NameDescription
request Google\Analytics\Data\V1beta\CheckCompatibilityRequest

A request to house fields associated with the call.

callOptions array

Optional.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Analytics\Data\V1beta\CheckCompatibilityResponse
Example
use Google\Analytics\Data\V1beta\CheckCompatibilityRequest;
use Google\Analytics\Data\V1beta\CheckCompatibilityResponse;
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\ApiCore\ApiException;

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function check_compatibility_sample(): void
{
    // Create a client.
    $betaAnalyticsDataClient = new BetaAnalyticsDataClient();

    // Prepare the request message.
    $request = new CheckCompatibilityRequest();

    // Call the API and handle any network failures.
    try {
        /** @var CheckCompatibilityResponse $response */
        $response = $betaAnalyticsDataClient->checkCompatibility($request);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

getMetadata

Returns metadata for dimensions and metrics available in reporting methods.

Used to explore the dimensions and metrics. In this method, a Google Analytics GA4 Property Identifier is specified in the request, and the metadata response includes Custom dimensions and metrics as well as Universal metadata.

For example if a custom metric with parameter name levels_unlocked is registered to a property, the Metadata response will contain customEvent:levels_unlocked. Universal metadata are dimensions and metrics applicable to any property such as country and totalUsers.

The async variant is Google\Analytics\Data\V1beta\Client\BaseClient\self::getMetadataAsync() .

Parameters
NameDescription
request Google\Analytics\Data\V1beta\GetMetadataRequest

A request to house fields associated with the call.

callOptions array

Optional.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Analytics\Data\V1beta\Metadata
Example
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\GetMetadataRequest;
use Google\Analytics\Data\V1beta\Metadata;
use Google\ApiCore\ApiException;

/**
 * @param string $formattedName The resource name of the metadata to retrieve. This name field is
 *                              specified in the URL path and not URL parameters. Property is a numeric
 *                              Google Analytics GA4 Property identifier. To learn more, see [where to find
 *                              your Property
 *                              ID](https://developers.google.com/analytics/devguides/reporting/data/v1/property-id).
 *
 *                              Example: properties/1234/metadata
 *
 *                              Set the Property ID to 0 for dimensions and metrics common to all
 *                              properties. In this special mode, this method will not return custom
 *                              dimensions and metrics. Please see
 *                              {@see BetaAnalyticsDataClient::metadataName()} for help formatting this field.
 */
function get_metadata_sample(string $formattedName): void
{
    // Create a client.
    $betaAnalyticsDataClient = new BetaAnalyticsDataClient();

    // Prepare the request message.
    $request = (new GetMetadataRequest())
        ->setName($formattedName);

    // Call the API and handle any network failures.
    try {
        /** @var Metadata $response */
        $response = $betaAnalyticsDataClient->getMetadata($request);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * Helper to execute the sample.
 *
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $formattedName = BetaAnalyticsDataClient::metadataName('[PROPERTY]');

    get_metadata_sample($formattedName);
}

runPivotReport

Returns a customized pivot report of your Google Analytics event data.

Pivot reports are more advanced and expressive formats than regular reports. In a pivot report, dimensions are only visible if they are included in a pivot. Multiple pivots can be specified to further dissect your data.

The async variant is Google\Analytics\Data\V1beta\Client\BaseClient\self::runPivotReportAsync() .

Parameters
NameDescription
request Google\Analytics\Data\V1beta\RunPivotReportRequest

A request to house fields associated with the call.

callOptions array

Optional.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Analytics\Data\V1beta\RunPivotReportResponse
Example
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\RunPivotReportRequest;
use Google\Analytics\Data\V1beta\RunPivotReportResponse;
use Google\ApiCore\ApiException;

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function run_pivot_report_sample(): void
{
    // Create a client.
    $betaAnalyticsDataClient = new BetaAnalyticsDataClient();

    // Prepare the request message.
    $request = new RunPivotReportRequest();

    // Call the API and handle any network failures.
    try {
        /** @var RunPivotReportResponse $response */
        $response = $betaAnalyticsDataClient->runPivotReport($request);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

runRealtimeReport

Returns a customized report of realtime event data for your property.

Events appear in realtime reports seconds after they have been sent to the Google Analytics. Realtime reports show events and usage data for the periods of time ranging from the present moment to 30 minutes ago (up to 60 minutes for Google Analytics 360 properties).

For a guide to constructing realtime requests & understanding responses, see Creating a Realtime Report.

The async variant is Google\Analytics\Data\V1beta\Client\BaseClient\self::runRealtimeReportAsync() .

Parameters
NameDescription
request Google\Analytics\Data\V1beta\RunRealtimeReportRequest

A request to house fields associated with the call.

callOptions array

Optional.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Analytics\Data\V1beta\RunRealtimeReportResponse
Example
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\RunRealtimeReportRequest;
use Google\Analytics\Data\V1beta\RunRealtimeReportResponse;
use Google\ApiCore\ApiException;

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function run_realtime_report_sample(): void
{
    // Create a client.
    $betaAnalyticsDataClient = new BetaAnalyticsDataClient();

    // Prepare the request message.
    $request = new RunRealtimeReportRequest();

    // Call the API and handle any network failures.
    try {
        /** @var RunRealtimeReportResponse $response */
        $response = $betaAnalyticsDataClient->runRealtimeReport($request);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

runReport

Returns a customized report of your Google Analytics event data. Reports contain statistics derived from data collected by the Google Analytics tracking code. The data returned from the API is as a table with columns for the requested dimensions and metrics. Metrics are individual measurements of user activity on your property, such as active users or event count. Dimensions break down metrics across some common criteria, such as country or event name.

For a guide to constructing requests & understanding responses, see Creating a Report.

The async variant is Google\Analytics\Data\V1beta\Client\BaseClient\self::runReportAsync() .

Parameters
NameDescription
request Google\Analytics\Data\V1beta\RunReportRequest

A request to house fields associated with the call.

callOptions array

Optional.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Analytics\Data\V1beta\RunReportResponse
Example
use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\RunReportRequest;
use Google\Analytics\Data\V1beta\RunReportResponse;
use Google\ApiCore\ApiException;

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function run_report_sample(): void
{
    // Create a client.
    $betaAnalyticsDataClient = new BetaAnalyticsDataClient();

    // Prepare the request message.
    $request = new RunReportRequest();

    // Call the API and handle any network failures.
    try {
        /** @var RunReportResponse $response */
        $response = $betaAnalyticsDataClient->runReport($request);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

batchRunPivotReportsAsync

Parameters
NameDescription
request Google\Analytics\Data\V1beta\BatchRunPivotReportsRequest
optionalArgs = [] array
Returns
TypeDescription
GuzzleHttp\Promise\PromiseInterface

batchRunReportsAsync

Parameters
NameDescription
request Google\Analytics\Data\V1beta\BatchRunReportsRequest
optionalArgs = [] array
Returns
TypeDescription
GuzzleHttp\Promise\PromiseInterface

checkCompatibilityAsync

Parameters
NameDescription
request Google\Analytics\Data\V1beta\CheckCompatibilityRequest
optionalArgs = [] array
Returns
TypeDescription
GuzzleHttp\Promise\PromiseInterface

getMetadataAsync

Parameters
NameDescription
request Google\Analytics\Data\V1beta\GetMetadataRequest
optionalArgs = [] array
Returns
TypeDescription
GuzzleHttp\Promise\PromiseInterface

runPivotReportAsync

Parameters
NameDescription
request Google\Analytics\Data\V1beta\RunPivotReportRequest
optionalArgs = [] array
Returns
TypeDescription
GuzzleHttp\Promise\PromiseInterface

runRealtimeReportAsync

Parameters
NameDescription
request Google\Analytics\Data\V1beta\RunRealtimeReportRequest
optionalArgs = [] array
Returns
TypeDescription
GuzzleHttp\Promise\PromiseInterface

runReportAsync

Parameters
NameDescription
request Google\Analytics\Data\V1beta\RunReportRequest
optionalArgs = [] array
Returns
TypeDescription
GuzzleHttp\Promise\PromiseInterface

static::metadataName

Formats a string containing the fully-qualified path to represent a metadata resource.

Parameter
NameDescription
property string
Returns
TypeDescription
stringThe formatted metadata resource.

static::parseName

Parses a formatted name string and returns an associative array of the components in the name.

The following name formats are supported: Template: Pattern

  • metadata: properties/{property}/metadata

The optional $template argument can be supplied to specify a particular pattern, and must match one of the templates listed above. If no $template argument is provided, or if the $template argument does not match one of the templates listed, then parseName will check each of the supported templates, and return the first match.

Parameters
NameDescription
formattedName string

The formatted name string

template string

Optional name of template to match

Returns
TypeDescription
arrayAn associative array from name component IDs to component values.