Search code examples
phpgoogle-analyticsgoogle-analytics-apigoogle-api-php-clientgoogle-analytics-4

How to filter using FULL_REGEXP by dimension using Google Analytics Data API (GA4) php client library?


I try to add regex filters to this request but I didn't find documentation with samples using filter : Filter\StringFilter\MatchType::FULL_REGEXP.

I don't have errors but i also doesn't have result. Where i do wrong ?

Here my code in error :

$request = $client->runReport([

      'property' => 'properties/' . $property_id,
      'dateRanges' => [
            new DateRange([
                 'start_date' => "$dateStart",
                 'end_date' => "$dateEnd",
            ]),
       ],
       'dimensions' => [
             new Dimension(['name' => 'hostName']),
             new Dimension(['name' => 'pageReferrer']),
       ],
       'metrics' => [
             new Metric(['name' => 'sessions']),
       ],

       'dimensionFilter' => new FilterExpression([
             'and_group' => new FilterExpressionList([
                  'expressions' => [
                         new FilterExpression([
                              'filter' => new Filter([
                                    'field_name' => 'hostName',
                                    'string_filter' => new Filter\StringFilter([
                                           'match_type' => Filter\StringFilter\MatchType::FULL_REGEXP,
                                           'value' => 'hostName==www.site.com',
                                  ])
                              ]),
                         ]),
                         new FilterExpression([
                              'filter' => new Filter([
                                  'field_name' => 'pageReferrer',
                                  'string_filter' => new Filter\StringFilter([
                                           'match_type' => Filter\StringFilter\MatchType::FULL_REGEXP,
                                           'value' => 'pageReferrer!~^https:\/\/*',
                                  ])
                              ]),
                         ]),
                  ]
             ]),
       ]),
]);

Still i link doc and samples i found about this API :


Solution

  • This is my first post, so please forgive formatting mistakes.

    I concur with the other poster's comment "There's obviously lack of examples in the GA4 documentation." The GA4 Data API v1 is laughably unready for commercial use, utterly at odds with Google's promise to sunset Universal Analytics on July 1. The lack of examples in their docs is maybe the most egregious gap.

    Soapbox put away.... When you specify FULL_REGEXP, then the 'value' key is intended to hold a regular expression, not a regex evaluation. So, your filter definition would be:

    new FilterExpression([
                                  'filter' => new Filter([
                                      'field_name' => 'hostName',
                                      'string_filter' => new Filter\StringFilter([
                                          'match_type' => Filter\StringFilter\MatchType::FULL_REGEXP,
                                          'value' => 'www\.site\.com',
                                      ])
                                  ]),
                              ]),