Search code examples
jsonregexposthog

PostHog API Regex Filtering Issue


I'm trying to use PostHog's API to filter events based on a regex pattern, as documented here: PostHog API Documentation

Here is an example I'm using based on the documentation for filtering by a current_url property with a regex pattern:

properties: [
    {
        "key": "current_url",
        "value": "^test\.aws\.worldee\.com\/[^?]+\?tripId=\d{1,5}$",
        "operator": "regex"
    }
]

However, when I include this in the query parameters, I receive the following error response:

{
    "type": "validation_error",
    "code": "invalid_input",
    "detail": "Properties are unparsable!",
    "attr": null
}

I'm not sure what might be causing this error. Can someone help me understand why PostHog is unable to parse this regex pattern when filtering events? Are there any specific requirements or limitations when using regex filtering with PostHog's API? Any insights would be greatly appreciated.

Help with solution.....


Solution

  • (disclaimer I work on PostHog)

    Trying the equivalent of that query in the PostHog UI gives me

    {"type":"AND","values":[{"type":"AND","values":[{"key":"$current_url","value":"^pauldambra\\.dev\\/[^?]+\\?tripId=\\d{1,5}$","operator":"regex","type":"event"}]}]}
    

    Which succeeds...

    Well - it returns 0 results. The $current_url property in PostHog includes the URL scheme so you need to include (http|https)://

    For example this regex (http|https)://pauldambra\.dev/\?tripId=\d{1,5}$ matches traffic I faked for the purpose of testing

    The PostHog UI showing a result

    Testing in the UI and using the developer tools to see what API calls are made is a good way to figure out how to use the PostHog API (and many others 😊)

    It would be easier to debug why this failed for you if you included the entire API call here or if you request support from within your PostHog account using https://app.posthog.com/insights/new#supportModal=support%3Aanalytics (StackOverflow is great but this might be a little too specific)