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

# PostHog

> Access analytics events, feature flags, insights, and cohorts through Airstore

Connect your PostHog account and access analytics events, feature flags, insights, and cohorts as local files.

## Prerequisites

* An [Airstore account](https://app.airstore.ai) with the CLI installed ([installation guide](/installation))
* A [PostHog](https://posthog.com) account with data you want to access
* A PostHog personal API key

## Get your PostHog API key

<Steps>
  <Step title="Open PostHog settings">
    Log in to [PostHog](https://us.posthog.com) and click your profile icon in the top right corner, then select **Settings**.
  </Step>

  <Step title="Create a personal API key">
    Navigate to **Personal API Keys** and click **Create personal API key**. Give it a descriptive name (e.g., "Airstore integration").
  </Step>

  <Step title="Copy the key">
    Copy the generated key. It starts with `phx_`.
  </Step>
</Steps>

## Connect PostHog

<Tabs>
  <Tab title="Dashboard">
    1. Go to [app.airstore.ai](https://app.airstore.ai)
    2. Navigate to **Settings → Integrations**
    3. Click **PostHog → Connect**
    4. Enter your API key and optionally a custom host URL
  </Tab>

  <Tab title="CLI">
    ```bash theme={null}
    airstore connection add <workspace_id> posthog --api-key phx_xxxxxxxxxxxx
    ```

    For self-hosted PostHog instances, include the `--host` flag:

    ```bash theme={null}
    airstore connection add <workspace_id> posthog --api-key phx_xxxxxxxxxxxx --host https://posthog.yourcompany.com
    ```
  </Tab>
</Tabs>

## Available data

Once connected, you can access the following PostHog resources:

| Resource          | Description                                                               |
| ----------------- | ------------------------------------------------------------------------- |
| **Events**        | Analytics events tracked in your project (pageviews, custom events, etc.) |
| **Feature flags** | Feature toggles and their current status                                  |
| **Insights**      | Saved analyses like trends, funnels, and retention charts                 |
| **Cohorts**       | User segment definitions based on behavior or properties                  |

## Filesystem structure

After mounting, PostHog data appears under `~/airstore/posthog/`:

```
~/airstore/posthog/
├── <view-name>/
│   ├── event_pageview_2024-01-15.md
│   ├── flag_new-onboarding-flow.md
│   ├── insight_signup-funnel.md
│   └── cohort_power-users.md
```

## Source view queries

Create source views with natural language to query your PostHog data:

| Query                            | What you get                              |
| -------------------------------- | ----------------------------------------- |
| "active feature flags"           | Currently enabled feature toggles         |
| "pageview events from last week" | Recent page view analytics data           |
| "onboarding funnel insights"     | Saved funnel analyses for user onboarding |
| "power user cohort"              | User segment definitions                  |

Airstore uses keyword-based routing to determine which PostHog resource to query. For example:

* Words like "flag" or "toggle" route to **feature flags**
* Words like "funnel" or "retention" route to **insights**
* Words like "cohort" or "segment" route to **cohorts**
* Event-related queries route to **events** by default

<Accordion title="Using a self-hosted PostHog instance?">
  If you're running PostHog on your own infrastructure, provide your custom host URL when connecting. Airstore will use this URL instead of the default PostHog Cloud endpoint.

  Via the dashboard, enter your host URL in the connection form. Via the CLI, use the `--host` flag:

  ```bash theme={null}
  airstore connection add ws_abc123 posthog \
    --api-key phx_xxxxxxxxxxxx \
    --host https://posthog.yourcompany.com
  ```
</Accordion>

## Next steps

<CardGroup cols={2}>
  <Card title="Source Views" icon="folder" href="/concepts/source-views">
    Learn more about creating and managing source views.
  </Card>

  <Card title="CLI Reference" icon="terminal" href="/reference/cli">
    Full command reference including connection management.
  </Card>
</CardGroup>
