Java

Last updated:

|Edit this page
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

Terminal
go get github.com/posthog/posthog-go
Go
package main
import (
"os"
"github.com/posthog/posthog-go"
)
func main() {
client, _ := posthog.NewWithConfig(
os.Getenv("POSTHOG_API_KEY"),
posthog.Config{
Endpoint: "<ph_instance_address>",
PersonalApiKey: "your personal API key", // needed for feature flags
},
)
defer client.Close()
// run commands
}

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

Setting user properties

To set user properties, include the properties you'd like to set when capturing an event:

Java
posthog.capture("distinct_id", "event_name", new HashMap<String, Object>() {
{
put("$set", new HashMap<String, Object>() {
{
put("name", "Max Hedgehog");
}
});
put("$set_once", new HashMap<String, Object>() {
{
put("initial_url", "/blog");
}
});
}
});

For more details on the difference between $set and $set_once, see our user properties docs.

Alias

Sometimes, you may want to assign multiple distinct IDs to a single user. This is helpful in scenarios where your primary distinct ID may be inaccessible. For example, if a distinct ID which is typically used on the frontend is not available in certain parts of your backend code. In this case, you can use alias to assign another distinct ID to the same user.

We strongly recommend reading our docs on alias to best understand how to correctly use this method.

Questions?

Was this page useful?

Next article

Node.js

If you're working with Node.js, the official posthog-node library is the simplest 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 Options Variable Description Default value host…

Read next article