Skip to content

Commit

Permalink
add java feature flag docs
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelhrocha committed Jun 2, 2024
1 parent a30ec27 commit ec3db8f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
9 changes: 7 additions & 2 deletions contents/docs/integrate/_snippets/install-java.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ See the [com.posthog.java](https://search.maven.org/artifact/com.posthog.java/po
import com.posthog.java.PostHog;

class Sample {
private static final String POSTHOG_API_KEY = "<ph_project_api_key>";
private static final String POSTHOG_PROJECT_API_KEY = "<ph_project_api_key>";
private static final String POSTHOG_HOST = "<ph_client_api_host>";
// Optional, but required for feature flags
private static final String POSTHOG_PERSONAL_API_KEY = "<ph_personal_api_key>";

public static void main(String args[]) {
PostHog posthog = new PostHog.Builder(POSTHOG_API_KEY).host(POSTHOG_HOST).build();
PostHog posthog = new PostHog.Builder(POSTHOG_PROJECT_API_KEY)
.host(POSTHOG_HOST)
.personalApiKey(POSTHOG_PERSONAL_API_KEY) // Required for feature flags
.build();

// run commands

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
There are 2 steps to implement feature flags in Go:

### Step 1: Evaluate the feature flag value

#### Boolean feature flags

```java
boolean isMyFlagEnabled = posthog.isFeatureFlagEnabled("flag-key", "distinct_id_of_your_user");

if isMyFlagEnabled == true {
// Do something differently for this user
}
```

#### Multivariate feature flags

```java
Optional<String> variant = posthog.getFeatureFlagVariant("flag-key", "distinct_id_of_your_user");

if (variant.isPresent() && variant.get().equals("variant-key")) { // replace 'variant-key' with the key of your variant
// Do something differently for this user
}
```

import IncludePropertyInEvents from "./include-feature-flag-property-in-backend-events.mdx"

<IncludePropertyInEvents />

There are two methods you can use to include feature flag information in your events:

#### Include the `$feature/feature_flag_name` property

In the event properties, include `$feature/feature_flag_name: variant_key`:

```java
posthog.capture("distinct_id_of_your_user", "event_name", new HashMap<String, Object>() {
{
put("$feature/feature-flag-key", "variant-key"); // replace feature-flag-key with your flag key. Replace 'variant-key' with the key of your variant
}
});
```
28 changes: 27 additions & 1 deletion contents/docs/libraries/java/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,30 @@ import FeatureFlagsLibsIntro from "../_snippets/feature-flags-libs-intro.mdx"

<FeatureFlagsLibsIntro />

Feature flags are not supported yet in our Java SDK. However, you can integrate them into your project by using the [PostHog API](/docs/feature-flags/adding-feature-flag-code?tab=api).
import JavaFeatureFlagsCode from '../../integrate/feature-flags-code/_snippets/feature-flags-code-java.mdx'

<JavaFeatureFlagsCode />

import LocalEvaluationIntro from "../../feature-flags/snippets/local-evaluation-intro.mdx"

### Local Evaluation

<LocalEvaluationIntro />

For details on how to implement local evaluation, see our [local evaluation guide](/docs/feature-flags/local-evaluation).

## Experiments (A/B tests)

Since [experiments](/docs/experiments/manual) use feature flags, the code for running an experiment is very similar to the feature flags code:


```java
Optional<String> variant = posthog.getFeatureFlagVariant("experimentfeature-flag-name", "user_distinct_id");

if (variant.isPresent() && variant.get().equals("variant-key")) {
// Do something differently for this user
}
```

It's also possible to [run experiments without using feature flags](/docs/experiments/running-experiments-without-feature-flags).

0 comments on commit ec3db8f

Please sign in to comment.