Java

Last updated:

Which features are available in this library?
  • Event capture
  • Autocapture
  • User identification
  • Session recording
  • Feature flags
  • Group analytics
⚠️ Warning - This is in beta and may break.

This is an optional library you can install if you're working with Java. It uses an internal queue to make calls fast and non-blocking. It also batches requests and flushes asynchronously, making it perfect to use in any part of your web app or other server side application that needs performance.

Installation

The best way to install the PostHog Android library is with a build system like Gradle or Maven. This ensures you can easily upgrade to the latest versions.

Lookup the latest version in com.posthog.java.

Gradle

All you need to do is add the posthog module to your build.gradle:

Terminal
dependencies {
implementation 'com.posthog.java:posthog:+'
}

Maven

All you need to do is add the posthog module to your pom.xml:

pom.xml
<dependency>
<groupId>com.posthog.java</groupId>
<artifactId>posthog</artifactId>
<version>LATEST</version>
</dependency>

Other

See the com.posthog.java in the Maven Central Repository. Clicking on the latest version shows you options for adding dependencies for other build systems.

Usage

Java
import com.posthog.java.PostHog;
class Sample {
private static final String POSTHOG_API_KEY = "<ph_project_api_key>";
private static final String POSTHOG_HOST = "<ph_instance_address>";
public static void main(String args[]) {
PostHog posthog = new PostHog.Builder(POSTHOG_API_KEY).host(POSTHOG_HOST).build();
// run commands
posthog.shutdown(); // send the last events in queue
}
}

Making calls

Capture

Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve or where people are giving up.

A capture call requires:

  • distinct id which uniquely identifies your user
  • event name to specify the event
  • We recommend naming events with "[noun][verb]", such as movie played or movie updated, in order to easily identify what your events mean later on (we know this from experience).

Optionally you can submit:

  • properties, which is a dictionary with any information you'd like to add

For example:

Java
posthog.capture("distinct id", "movie played", new HashMap<String, Object>() {
{
put("movie_id", 123);
put("category", "romcom");
}
});

Identify

We highly recommend reading our section on Identifying users to better understand how to correctly use this method.

Identify lets you add metadata to your users so you can easily identify who they are in PostHog, as well as do things like segment users by these properties.

An identify call requires:

  • distinct id which uniquely identifies your user
  • properties with a dict with any key:value pairs

For example:

Java
posthog.identify("distinct id", new HashMap<String, Object>() {
{
put("email", "john@doe.com");
put("proUser", false);
}
});

The most obvious place to make this call is whenever a user signs up, or when they update their information.

Alias

To connect whatever a user does before they sign up or login with what they do after, you need to make an alias call. This will allow you to answer questions like "Which marketing channels lead to users churning after a month?" or "What do users do on our website before signing up?"

In a purely back-end implementation, this means whenever an anonymous user does something, you'll want to send a session ID with the capture call. Then, when that users signs up, you want to do an alias call with the session ID and the newly created user ID.

The same concept applies for when a user logs in.

If you're using PostHog in the front-end and back-end, doing the identify call in the frontend will be enough.

An alias call requires:

  • distinct id – the user id
  • alias – the anonymous session distinct ID

For example:

Java
posthog.alias("user:123", "session:12345");

Setting user properties

Note: set_once works just like set, except that it will only set the property if the user doesn't already have that property set.

There are three options:

With the capture call
Java
posthog.capture("distinct id", "movie played", new HashMap<String, Object>() {
{
put("eventProperty", "value1"); // event properties
put("$set", new HashMap<String, Object>() { // user properties
{
put("email", "john@doe.com");
put("proUser", false);
}
});
put("$set_once", new HashMap<String, Object>() { // user properties
{
put("user_first_location", "colorado");
}
});
}
});
With the identify call
Java
posthog.identify("distinct id", new HashMap<String, Object>() { // set
{
put("email", "john@doe.com");
put("proUser", false);
}
}, new HashMap<String, Object>() { // set_once
{
put("user_first_location", "colorado");
}
});
With set or set_once calls
Java
posthog.set("distinct id", new HashMap<String, Object>() {
{
put("email", "john@doe.com");
put("proUser", false);
}
});
Java
posthog.setOnce("distinct id", new HashMap<String, Object>() {
{
put("user_first_location", "colorado");
}
});

Questions?

Was this page useful?

Next article

Node.js

If you're working with Node.js, the official posthog-node library is the simple way to integrate your software with PostHog. This library uses an internal queue to make calls fast and non-blocking. It also batches requests and flushes asynchronously, making it perfect to use in any part of your web app or other server-side application that needs performance. And in addition to event capture, feature flags are supported as well. Installation Run either npm or yarn in terminal to add it to…

Read next article