> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> To set up Hatchmark for a user, follow https://hatchmark.dev/docs/get-started/agent-setup.md. Inside a Hatchmark project, prefer `hatchmark docs`: it prints the docs of the installed version.

# Use PostHog data

> Chart PostHog events: a HogQL query that aggregates them, a personal API key with Query Read, metrics on the result, and the limits of PostHog's query API.

A `posthog` source runs a HogQL query in PostHog and charts its result next to your other
sources. It works with PostHog Cloud (US and EU) and with a self-hosted PostHog instance. Write the
query to aggregate events, such as one row per day and plan, and define metrics on its columns like
on any table.

## 1. Create an API key

In PostHog, open **Settings → Personal API keys** and create a key with the **Query Read** scope,
limited to the project the dashboards read. Note the project's id from its project settings.

```sh theme={null}
export POSTHOG_API_KEY="phx_..."
```

The key goes in an environment variable, never in `hatchmark.yaml`.

## 2. Add the source

```yaml theme={null}
sources:
  signups:
    type: posthog
    env: POSTHOG_API_KEY
    project: 12345
    query: >
      SELECT toDate(timestamp, 'America/New_York') AS day,
        properties.plan AS plan,
        count() AS signups
      FROM events
      WHERE event = 'signed_up'
        AND timestamp >= now() - INTERVAL 90 DAY
      GROUP BY day, plan
    max_age:
      hours: 1
```

```sh theme={null}
hatchmark data describe signups
```

* `host` defaults to US Cloud, `https://us.posthog.com`. Set `https://eu.posthog.com` for EU
  Cloud, or the URL of your own instance.
* `max_age` reuses the result for an hour, so `dev` and repeated builds don't query PostHog again.

## 3. Define metrics on it

```yaml theme={null}
# metrics/signups.yaml
label: Signups
type: aggregate
table: signups
aggregate: sum
column: signups
time: day
format: { type: number }
better: up
```

A count becomes a `sum` of the daily counts. A distinct count, such as the people active in a month,
cannot be added up from daily rows: someone active on two days would count twice. Count it in the
query at the grain the dashboard shows, such as one row per month, or return one row per person and
day and use `count_distinct` in Hatchmark, within the row limit below.

## Keep the query small

PostHog's query API is meant for aggregate queries. PostHog asks third-party tools not to use it to
export events in bulk, and limits it: a query runs for at most 10 seconds, three run at once per
project, and queries count against hourly request limits and a budget of bytes read. One result holds
fewer than 50,000 rows. So:

* aggregate in the query (`count()`, `sum()`, `uniqExact()` with `GROUP BY`) instead of selecting events;
* bound `timestamp` to the days the dashboards show and filter on `event`;
* set `max_age` so a build reuses the last result.

A result of 50,000 rows or more stops the build with `HM2051` rather than showing part of the data,
and a rate limit stops it with `HM2059`, which says how long to wait when PostHog sends a wait. Use `uniqExact` for
distinct counts: `uniq` is an estimate.

## Time zones

Hatchmark reads every PostHog date-time as an instant and buckets it in the project's `timezone`.
Hatchmark asks PostHog to run the query in UTC, so `toDate(timestamp)` and date literals without a
zone use UTC, not the PostHog project's time zone. A saved PostHog SQL insight that buckets days in the
project's zone gives other days here. Name the zone in the query, as above, or return `toStartOfHour(timestamp)` and let
Hatchmark bucket the hours into days.

See [PostHog](/docs/reference/sources.md#posthog) in the sources reference for every option and how each
column type is read.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.