php-docs-samples icon indicating copy to clipboard operation
php-docs-samples copied to clipboard

Where are the examples (or docs for that matter) about filters???

Open php4fan opened this issue 3 years ago • 5 comments

Where is the detailed documentation with examples, of how to build reports with complex (or even trivial) filters with the PHP library??

Here: https://developers.google.com/analytics/devguides/reporting/data/v1/basics#dimension_filters Some (too basic) example of filters in raw HTTP but no mention of PHP.

Here: http://googleapis.github.io/google-cloud-php/#/docs/analytics-data/v0.8.0/analyticsdata/v1beta/readme The only one example in PHP is one request, not only with no filters but with no parameters whatsoever.

Here: https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/FilterExpression#StringFilter Some more details about filters but no clue as to how to translate it into PHP code.

This is pathetic.

php4fan avatar Feb 01 '22 15:02 php4fan

Hi @php4fan Sorry to hear your experience hasn't been great. We haven't created samples for the documentation you linked to yet. However, looking at the Python sample, the PHP sample would look something like this:

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\RunReportRequest;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Filter;
use Google\Analytics\Data\V1beta\FilterExpression;

$client = new BetaAnalyticsDataClient();
$propertyId = 'YOUR-GA4-PROPERTY-ID';

$dimension = (new Dimension())
    ->setName('date');
$metric = (new Metric())
    ->setName('eventCount');
$dateRange = (new DateRange())
    ->setStartDate('7daysAgo')
    ->setEndDate('yesterday');
$stringFilter = (new Filter\StringFilter())
    ->setValue('first_open');
$filter = (new Filter())
    ->setFieldName('eventName')
    ->setStringFilter($stringFilter);
$filterExprssion = (new FilterExpression())
    ->setFilter($filter);
$response = $client->runReport([
    'property' => 'properties/' . $propertyId,
    'dimensions' => [$dimension],
    'metrics' => [$metric],
    'date_ranges' => [$dateRange],
    'dimension_filter' => $filter,
]);

var_dump($response->serializeToJsonString());

I hope this helps.

bshaffer avatar Jun 15 '22 18:06 bshaffer

Have you added that to the docs? If so where? Otherwise why are you closing this as complete?

php4fan avatar Jun 16 '22 10:06 php4fan

@php4fan May be this could be helpful for you or anyone else

$dimensionFilter = new \Google\Analytics\Data\V1beta\FilterExpression([
                'and_group' => new \Google\Analytics\Data\V1beta\FilterExpressionList(
                    [
                        'expressions' => [
                            0 => new \Google\Analytics\Data\V1beta\FilterExpression(
                                [
                                    'filter' => new \Google\Analytics\Data\V1beta\Filter(
                                        array(
                                            'field_name' => 'streamId',
                                            'in_list_filter' => new \Google\Analytics\Data\V1beta\Filter\InListFilter([
                                                'values' => $streamIds,
                                                'case_sensitive' => false
                                            ]),
                                        )
                                    )
                                ]
                            )
                        ]
                    ]
                )
            ]);

AltamashShaikh avatar Sep 11 '23 07:09 AltamashShaikh

We can link the documentation to our samples that we already have generated in this repository.

bshaffer avatar Sep 11 '23 17:09 bshaffer

@bshaffer That would be great :+1:

AltamashShaikh avatar Sep 12 '23 01:09 AltamashShaikh