mirror of
https://github.com/BillyOutlast/posthog.com.git
synced 2026-02-13 15:50:57 +01:00
* style(profiles): add custom media query to fix mobile layout * updated parameter for .onFeatureFlags to include (flags) as the first parameter * Delete src/pages/community/profiles/[id].tsx * Revert "Delete src/pages/community/profiles/[id].tsx" This reverts commit 4e4a042763af540e54c4ee44bfd8c26964171851. * Revert "style(profiles): add custom media query to fix mobile layout" This reverts commit faf8dbb0a6bfa0345d46391671ff86c489749ea6. --------- Co-authored-by: Eli Kinsey <eli@ekinsey.dev>
160 lines
6.0 KiB
Plaintext
160 lines
6.0 KiB
Plaintext
### Boolean feature flags
|
|
|
|
```js-web
|
|
if (posthog.isFeatureEnabled('flag-key') ) {
|
|
// Do something differently for this user
|
|
|
|
// Optional: fetch the payload
|
|
const matchedFlagPayload = posthog.getFeatureFlagPayload('flag-key')
|
|
}
|
|
```
|
|
|
|
### Multivariate feature flags
|
|
|
|
```js-web
|
|
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')
|
|
}
|
|
```
|
|
|
|
### Ensuring flags are loaded before usage
|
|
|
|
Every time a user loads a page, we send a request in the background to fetch the feature flags that apply to that user. We store those flags in your chosen persistence option (local storage by default).
|
|
|
|
This means that for most pages, the feature flags are available immediately — **except for the first time a user visits**.
|
|
|
|
To handle this, you can use the `onFeatureFlags` callback to wait for the feature flag request to finish:
|
|
|
|
```js-web
|
|
posthog.onFeatureFlags(function (flags, flagVariants, { errorsLoading }) {
|
|
// feature flags are guaranteed to be available at this point
|
|
if (posthog.isFeatureEnabled('flag-key')) {
|
|
// do something
|
|
}
|
|
})
|
|
```
|
|
|
|
#### Callback parameters
|
|
|
|
The `onFeatureFlags` callback receives the following parameters:
|
|
|
|
- `flags: string[]`: An object containing the feature flags that apply to the user.
|
|
|
|
- `flagVariants: Record<string, string | boolean>`: An object containing the variants that apply to the user.
|
|
|
|
- `{ errorsLoading }: { errorsLoading?: boolean }`: An object containing a boolean indicating if an error occurred during the request to load the feature flags. This is `true` if the request timed out or if there was an error. It will be `false` or `undefined` if the request was successful.
|
|
|
|
You won't usually need to use these, but they are useful if you want to be extra careful about feature flags not being loaded yet because of a network error and/or a network timeout (see `feature_flag_request_timeout_ms`).
|
|
|
|
### Reloading feature flags
|
|
|
|
Feature flag values are cached. If something has changed with your user and you'd like to refetch their flag values, call:
|
|
|
|
```js-web
|
|
posthog.reloadFeatureFlags()
|
|
```
|
|
|
|
### Overriding server properties
|
|
|
|
Sometimes, you might want to evaluate feature flags using properties that haven't been ingested yet, or were set incorrectly earlier. You can do so by setting properties the flag depends on with these calls:
|
|
|
|
```js-web
|
|
posthog.setPersonPropertiesForFlags({'property1': 'value', property2: 'value2'})
|
|
```
|
|
|
|
> **Note:** These are set for the entire session. Successive calls are additive: all properties you set are combined together and sent for flag evaluation.
|
|
|
|
Whenever you set these properties, we also trigger a reload of feature flags to ensure we have the latest values. You can disable this by passing in the optional parameter for reloading:
|
|
|
|
```js-web
|
|
posthog.setPersonPropertiesForFlags({'property1': 'value', property2: 'value2'}, false)
|
|
```
|
|
|
|
At any point, you can reset these properties by calling `resetPersonPropertiesForFlags`:
|
|
|
|
```js-web
|
|
posthog.resetPersonPropertiesForFlags()
|
|
```
|
|
|
|
The same holds for [group](/manual/group-analytics) properties:
|
|
|
|
```js-web
|
|
// set properties for a group
|
|
posthog.setGroupPropertiesForFlags({'company': {'property1': 'value', property2: 'value2'}})
|
|
|
|
// reset properties for a given group:
|
|
posthog.resetGroupPropertiesForFlags('company')
|
|
|
|
// reset properties for all groups:
|
|
posthog.resetGroupPropertiesForFlags()
|
|
```
|
|
|
|
> **Note:** You don't need to add the group names here, since these properties are automatically attached to the current group (set via `posthog.group()`). When you change the group, these properties are reset.
|
|
|
|
#### Automatic overrides
|
|
|
|
Whenever you call `posthog.identify` with person properties, we automatically add these properties to flag evaluation calls to help determine the correct flag values. The same is true for when you call `posthog.group()`.
|
|
|
|
#### Default overridden properties
|
|
|
|
By default, we always override some properties based on the user IP address.
|
|
|
|
The list of properties that this overrides:
|
|
|
|
1. `$geoip_city_name`
|
|
2. `$geoip_country_name`
|
|
3. `$geoip_country_code`
|
|
4. `$geoip_continent_name`
|
|
5. `$geoip_continent_code`
|
|
6. `$geoip_postal_code`
|
|
7. `$geoip_time_zone`
|
|
|
|
This enables any geolocation-based flags to work without manually setting these properties.
|
|
|
|
### Request timeout
|
|
|
|
You can configure the `feature_flag_request_timeout_ms` parameter when initializing your PostHog client to set a flag request timeout. This helps prevent your code from being blocked in the case when PostHog's servers are too slow to respond. By default, this is set at 3 seconds.
|
|
|
|
```js
|
|
posthog.init('<ph_project_api_key>', {
|
|
api_host: '<ph_client_api_host>',
|
|
defaults: '<ph_posthog_js_defaults>'
|
|
feature_flag_request_timeout_ms: 3000 // Time in milliseconds. Default is 3000 (3 seconds).
|
|
})
|
|
```
|
|
|
|
### Feature flag error handling
|
|
|
|
When using the PostHog SDK, it's important to handle potential errors that may occur during feature flag operations. Here's an example of how to wrap PostHog SDK methods in an error handler:
|
|
|
|
```js
|
|
function handleFeatureFlag(client, flagKey, distinctId) {
|
|
try {
|
|
const isEnabled = client.isFeatureEnabled(flagKey, distinctId);
|
|
console.log(`Feature flag '${flagKey}' for user '${distinctId}' is ${isEnabled ? 'enabled' : 'disabled'}`);
|
|
return isEnabled;
|
|
} catch (error) {
|
|
console.error(`Error fetching feature flag '${flagKey}': ${error.message}`);
|
|
// Optionally, you can return a default value or throw the error
|
|
// return false; // Default to disabled
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Usage example
|
|
try {
|
|
const flagEnabled = handleFeatureFlag(client, 'new-feature', 'user-123');
|
|
if (flagEnabled) {
|
|
// Implement new feature logic
|
|
} else {
|
|
// Implement old feature logic
|
|
}
|
|
} catch (error) {
|
|
// Handle the error at a higher level
|
|
console.error('Feature flag check failed, using default behavior');
|
|
// Implement fallback logic
|
|
}
|
|
``` |