Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Java:

### 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
}
});
```
30 changes: 28 additions & 2 deletions contents/docs/libraries/java/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ features:
userIdentification: true
autoCapture: false
sessionRecording: false
featureFlags: false
featureFlags: true
groupAnalytics: true
---

Expand Down 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).