PostHog / posthog

🦔 PostHog provides open-source product analytics, session recording, feature flagging and A/B testing that you can self-host.
https://posthog.com
Other
21.18k stars 1.25k forks source link

Call a webhook on a Cohort update #18083

Open jurajmajerik opened 11 months ago

jurajmajerik commented 11 months ago

Is your feature request related to a problem?

From a user:

I am looking for a way to be notified when new users are added to a dynamic cohort. As I understand it these are updated every 24 hours so it could just happen on that same cadence. Ideally I would call a webhook - however I see webhooks only support Actions. Our end goal is to automate getting cohorts of users into our CRM for personalized outreach. A slack notification or something like that would be nice too.

Describe the solution you'd like

Add the ability to call a webhook on a cohort update.

Additional context

Community question: https://posthog.com/questions/is-it-possible-to-get-a-notification-when-members-of-a-cohort-change

Thank you for your feature request – we love each and every one!

MarconLP commented 6 months ago

+1 https://posthoghelp.zendesk.com/agent/tickets/11125

Curldog21 commented 6 months ago

I vote for this feature.

MarconLP commented 6 months ago

+1 https://posthoghelp.zendesk.com/agent/tickets/11736

dylanswartz commented 6 months ago

+1 to this one. I used Zapier to effectively create a cron that runs nightly, it hits the PostHog API to get a list of users in a specific cohort, then loops through each user and checks if they are in a list in our CRM. I they are not in the list, it adds them to the list. If they are already in the list it updates a timestamp (this allows us to both mirror the cohort into our CRM but also maintain a list of folks who dropped off the cohort based on the timestamp).

The current setup is pretty inefficient, brittle, and expensive in terms of Zapier "compute". Would love to just call a web-hook whenever a change a cohort happens!

mariusandra commented 6 months ago

This is a great feature request, but at present hard to pull off.

This is how we update cohorts: https://github.com/PostHog/posthog/blob/0257b2bcb90278349b67022923c9e9c62bb4d229/posthog/models/cohort/sql.py#L37-L48

It's one SQL query that happens fully within ClickHouse. There's no way to hook into "when new people got added to the cohort".

The way to get this information is to make a HogQL query to the raw_cohort_people table and select values with a higher version than what you saw the last time:

image

This also is the only way we could trigger updates if someone got added to a cohort. However such a system is brittle by design if scaled out to a lot of users, and taking on the risk of maintaining this is not something we've had the resources to do.