Reputation: 7515
I am using Google SDK V1beta
found HERE
I have successfully created an account, and created a property under that account after much trouble with the documentation not aligning with the API/SDK.
I am now trying to create a DataStream, and I am almost there. The Example the the SDK uses can be found HERE -- However this example, like most provided in the docs is broken. If you use the example provided you'll find that the ->createDataStream(
requires more aruments than just one .. Even when using V1beta library. So I came up with this:
$dataStream = (new DataStream())
->setDisplayName('Zaks Datastream')
->setType('1');
try {
/** @var DataStream $response */
$response = $client->createDataStream($property_name, $dataStream);
printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
It passes the SDK checks but the call returns:
Call failed with message: {
"message": "The value for the 'default_uri' field was empty, but must be provided.",
"code": 3,
"status": "INVALID_ARGUMENT",
"details": []
}
When I look for setting the default URI there is no option in the SDK to do so .. -->
Google's documentation is frustrating to say the least .. Is there an external function, or another call to set the default URI?
Upvotes: 0
Views: 161
Reputation: 7515
OK so after fiddling around, I found that DataStream was looking for a WebStream()
Object that CONTAINS the default_uri
. The documentation is extremely vague and it seems was written before the dynamic classes were completed to be honest. So for those that run across this in the future .. Create your WebStream()
Object and pass it into your DataStream()
The only thing you have to do to make the following work is define $propery_name
.
Also for the DataStream
type, I could not find the docs in PHP -- But the Java Docs provide the integer for the various types -- Which mine needed to be Web
so ->setType('1')
(It's a string/var in PHP not an int) Types can be found HERE
$property_name = 'properties/xxxxxxxx'
use Google\Analytics\Admin\V1beta\AnalyticsAdminServiceClient;
use Google\Analytics\Admin\V1beta\Property;
use Google\ApiCore\ApiException;
use Google\ApiCore\PagedListResponse;
use Google\Analytics\Admin\V1beta\DataStream;
use Google\Analytics\Admin\V1beta\DataStream\WebStreamData;
$webDataStream = (new WebStreamData())
->setDefaultUri('test.com');
$dataStream = (new DataStream())
->setDisplayName('Zaks Datastream')
->setWebStreamData($webDataStream)
->setType('1');
try {
/** @var DataStream $response */
$response = $client->createDataStream($property_name, $dataStream);
printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
The response from Goolge looks like this ..
{
"webStreamData":{
"measurementId":"G-XXXXXXX",
"defaultUri":"test.com"
},
"name":"properties\/402978158\/dataStreams\/6007998051",
"type":"WEB_DATA_STREAM",
"displayName":"Zaks Datastream",
"createTime":"2023-08-16T18:23:31.000000801Z",
"updateTime":"2023-08-16T18:23:31.000000801Z"
}
Upvotes: 0