skip to Main Content

I try to add regex filters this request. Also I didn’t find documentation with samples using filter : FilterStringFilterMatchType::FULL_REGEXP.

I don’t have errors but i also doesn’t have result. Where i do wrong ?
Here i try to get result where hostname is a site and where pageReferrer start with https:// .

    $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 FilterStringFilter([
                                      'match_type' => FilterStringFilterMatchType::FULL_REGEXP,
                                      **'value' => 'hostName==www.site.com',**
                                  ])
                              ]),
                          ]),
                          new FilterExpression([
                              'filter' => new Filter([
                                  'field_name' => 'pageReferrer',
                                  'string_filter' => new FilterStringFilter([
                                      'match_type' => FilterStringFilterMatchType::FULL_REGEXP,
                                      **'value' => 'pageReferrer!~^https://*',**
                                  ])
                              ]),
                          ]),
                      ]
                  ]),
                ]),
              ]);

2

Answers


  1. There’s obviously lack of examples in the GA4 documentation.

    Try to escape the symbols in the first filter.
    Also maybe PARTIAL_REGEXP could help with testing.

    Login or Signup to reply.
  2. 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 FilterStringFilter([
                                          'match_type' => FilterStringFilterMatchType::FULL_REGEXP,
                                          'value' => 'www.site.com',
                                      ])
                                  ]),
                              ]),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search