# PostHog

## Installing

### Install the package;

```
npm install posthog-js
```

### Initalize site-wide in your site.ts

At the top under imports, add;

```typescript
import posthog from 'posthog-js'
```

In the setup() method, use the posthog.init code given to you;

e.g.;

{% code overflow="wrap" %}

```typescript
posthog.init('phc_YOUR_KEY', { api_host: 'https://us.i.posthog.com', person_profiles: 'identified_only' })
```

{% endcode %}

Note; you can choose to initialize only on production sites, e.g.;&#x20;

{% code overflow="wrap" %}

```typescript
// Init Posthog
// Only on non-staging pages 
if (!window.location.host.endsWith('.webflow.io')) {
  posthog.init('phc_YOUR_KEY', { api_host: 'https://us.i.posthog.com', person_profiles: 'identified_only' })
}

```

{% endcode %}

## Usage Notes

In the exec() method, you'll perform your actual tests.

### Feature Flags

Good for;

* Switching on or off specific features
* Doing a limited deployment of a feature to a smaller audience

```typescript
if (posthog.isFeatureEnabled('flag-key') ) {
    // Do something differently for this user

    // Optional: fetch the payload
    const matchedFlagPayload = posthog.getFeatureFlagPayload('flag-key')
}
```

### Experiments (A/B tests)

* Good for handling variants&#x20;

{% code overflow="wrap" %}

```typescript
if (posthog.getFeatureFlag('flag-key')  == 'variant-key') { // replace 'variant-key' with the key of your variant
    // Do something differently for this user
    
    // Optional: fetch the payload
    const matchedFlagPayload = posthog.getFeatureFlagPayload('flag-key')
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://engine.sygnal.com/special/posthog.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
