Learn business growth with Google Analytics 4 Forums Google Analytics 4 Implementing Advanced Dimension Filters in Google Analytics Data API (GA4) with PHP Client Library

  • Implementing Advanced Dimension Filters in Google Analytics Data API (GA4) with PHP Client Library

    Posted by Benjamin on 13 March 2023 at 3:35 am

    Hey, I’m working on adding regex filters to a request. I’m trying to use the filter FilterStringFilterMatchType::FULL_REGEXP, but I can’t seem to find any examples or helpful documentation on this. I’ve run my code, and even though there aren’t any errors, I’m not getting any results either.

    Right now, I’m trying to retrieve results where the hostname is a site and the pageReferrer starts with https://. Below is the piece of code I’m working with:


    $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://*',
    ])
    ]),
    ]),
    ]
    ]),
    ]),
    ]);

    Any ideas where I’m going wrong here?

    Xavier replied 11 months, 2 weeks ago 3 Members · 2 Replies
  • 2 Replies
  • Harry

    Member
    4 July 2023 at 6:59 am

    Just gauging from your post, it seems like there’s been a bit of head scratching going on due to the somewhat skimpy examples in the GA4 documentation. It’s understandable, as we all know APIs aren’t always the most straightforward thing to tackle, let alone when the supporting resources are a little on the lean side.

    But don’t worry, I think I see what could be causing those blank results. When defining a filter using FULL_REGEXP as the match type, you’d actually want to pass a valid regular expression string to ‘value’. Your current setup is more of a regex evaluation, hence the confusion.

    Referring to your hostname filter, it would be something more like this:

    `code
    new FilterExpression([
    ‘filter’ => new Filter([
    ‘field_name’ => ‘hostName’,
    ‘string_filter’ => new FilterStringFilter([
    ‘match_type’ => FilterStringFilterMatchType::FULL_REGEXP,
    ‘value’ => ‘www.site.com’, // FULL_REGEXP expects a regex, not a regex evaluation
    ])
    ]),
    ]),
    `

    Hopefully, that straightens things out a bit and gets you closer to seeing those desired results. Keep up the great work and happy coding!

  • Xavier

    Member
    7 July 2023 at 12:37 pm

    Firstly, from your code, it appears your regex filter for ‘value’ is incorrect. When using FULL_REGEXP, the ‘value’ should just be the regex pattern you are trying to match against, without the field name. For example, for your ‘hostName’ filter, it should be ‘value’ => ‘www.site.com’ (note the escaping for periods), not ‘hostName==www.site.com’. Similarly, for ‘pageReferrer’, ‘value’ => ‘^https://.*’ should work.

    Secondly, the use of !~ operator in the value for ‘pageReferrer’ filter seems to suggest you are trying to get results where ‘pageReferrer’ does NOT start with https://. If that is the case, it would be the reason you aren’t getting any results because your condition is that hostname should be http://www.site.com and pageReferrer should NOT start with https://. So, if you want results where pageReferrer starts with https://, you might want to drop the !~ for the ‘pageReferrer’ filter.

    Finally, be sure your regex patterns are correct and reviewing your regex syntax rules may also be helpful. Test your regular expressions thoroughly to ensure that they are working as intended.

Log in to reply.