0

I am working with the Google Cloud PHP API. I am hooked up and everything works great when pulling admin info like accounts and properties.

I am trying to create a basic property within an account using the API. I am running across either a bug, or I am missing something glaring. The example I am using is HERE and this is the code I am attempting to use:

function createNewProperty($accountName){
    echo "Account Name: $accountName \n\n"; // accounts/123456789

    $client = new AnalyticsAdminServiceClient();

    echo "Create Property \n\n";

    $property = ( new Property() )
        ->setDisplayName("Zaks Test GA4 Property")
        ->setTimeZone('America/Chicago');

    try {
        $createdProperty = $client->createProperty($accountName, $property);
        echo 'Property created: ' . "\n"; // . $createdProperty->getName();
        print_r( $createdProperty );
    } catch (\Exception $e) {
        echo 'An error occurred: ' . $e->getMessage() . "\n";
    }
}

And I call it simply by:

 $createAccountName = 'accounts/123456789';
 createNewProperty($createAccountName);

I feel I am following the EXAMPLE verbatim. But I get:

Account Name: accounts/123456789

Create Property

PHP Fatal error:  Uncaught TypeError: Google\Analytics\Admin\V1alpha\Gapic\AnalyticsAdminServiceGapicClient::createProperty(): Argument #2 ($optionalArgs) must be of type array, Google\Analytics\Admin\V1alpha\Property given, called in /var/www/GA/GA4/test.php on line 75 and defined in /var/www/GA/GA4/vendor/google/analytics-admin/src/V1alpha/Gapic/AnalyticsAdminServiceGapicClient.php:2618
Stack trace:
#0 /var/www/GA/GA4/test.php(75): Google\Analytics\Admin\V1alpha\Gapic\AnalyticsAdminServiceGapicClient->createProperty()
#1 /var/www/GA/GA4/test.php(32): createNewProperty()
#2 {main}
  thrown in /var/www/GA/GA4/vendor/google/analytics-admin/src/V1alpha/Gapic/AnalyticsAdminServiceGapicClient.php on line 2618

Obviously this part:

Argument #2 ($optionalArgs) must be of type array, Google\Analytics\Admin\V1alpha\Property given

is the $property argument that I am passing through to the function $client->createProperty($accountName, $property);

I have also seen it done this way:

$property = new Property([
    'display_name' => 'Zaks Test GA4 Property',
    'time_zone' => 'America/Chicago'
]);

So me being me .. I am like hmm .. Arg #2 needs to be an array. OK I'll make it an array. I do the following:

$property = [
    'display_name' => 'Zaks Test GA4 Property',
    'time_zone' => 'America/Chicago'
];

But I get a different error.

An error occurred: Expect Google\Analytics\Admin\V1alpha\Property.

Which is it? Is argument #2 supposed to be an array? Or is it supposed to be a Google/.../Property?

Is this a bug in the API or am I doing something wrong?

Zak
  • 6,976
  • 2
  • 26
  • 48

1 Answers1

1

I think the issue you are having is that there are two versions of this api, the documentation is mainly focused on the beta version.

Try using the beta version instead of the alpha

<?php

require 'vendor/autoload.php';

use Google\Analytics\Admin\V1beta\AnalyticsAdminServiceClient;

$service_account_key_file_path = "C:\Development\FreeLance\GoogleSamples\Credentials/ServiceAccountCred.json";

$client = new AnalyticsAdminServiceClient([
        'credentials' => $service_account_key_file_path
    ]);

$accounts = $client->listAccounts();

foreach ($accounts as $account) {
    print 'Found account: ' . $account->getName() . ' '. $account->getDisplayName() . PHP_EOL;
}

$property = ( new \Google\Analytics\Admin\V1beta\Property() )
    ->setDisplayName("Test property")
    ->setTimeZone('Europe/Copenhagen')
    ->setParent('accounts/[REDACTED]');

try {
    $createdProperty = $client->createProperty($property);
    echo 'Property created: ' . "\n"; // . $createdProperty->getName();
    print_r( $createdProperty );
} catch (\Exception $e) {
    echo 'An error occurred: ' . $e->getMessage() . "\n";
}

creating Google Analytics properties using PHP

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • OK, now that makes sense .. I could not find `setParent()` anywhere in the docs .. Can you share where you found that? Also, since I am working with the Google SDK, I don't know how to retrieve that specific property we just created -- So I can move to the next step of creating a view. We have `$createdProperty->getName()` -- But is there any way to just grab the whole property Object? – Zak Aug 16 '23 at 15:28
  • Not to say I couldn't just as easily turn around and grab the property using the API .. Just wondering if there's a "return" from Google with the property info already in tow .. – Zak Aug 16 '23 at 15:40
  • check the video i made for you – Linda Lawton - DaImTo Aug 16 '23 at 16:05
  • I found your answer useful enough, that I recommend making another video on DataStreams. You may base it on my [Self Answered Question](https://stackoverflow.com/questions/76915919/google-analytics-api-createdatastream-missing-args-that-must-be-provided/) which is about DataStreams and the lacking in documentation -- It follows suit with your method suggested in this answer, and I feel would be a great follow-up video considering a property with no dataStream is pretty much useless. Thank you again for your time and getting me on my way! I have over 1,000 properties to create ... – Zak Aug 16 '23 at 22:53
  • Not a bad idea for another video. Thanks! – Linda Lawton - DaImTo Aug 17 '23 at 06:45