This is the documentation for the Amplitude Analytics Java SDK.

Install the SDK

If you use Gradle in your project, add the following dependency to build.gradle, and sync your project with the updated file.

1dependencies {
2 implementation 'org.json:json:20201115'
3 implementation 'com.amplitude:java-sdk:1.+'

Download the latest JAR file then add it to the project's build path. See instructions for your IDE.

Import the SDK

Import Amplitude into any file that uses it. Amplitude uses the open source JSONObject library to conveniently create JSON key-value objects.

1import com.amplitude.Amplitude;
2import org.json.JSONObject;

Initialize the SDK

You must initialize the SDK before any events are instrumented. The API key for your Amplitude project is required.

1Amplitude client = Amplitude.getInstance();

Amplitude.getInstance(String name) may optionally take a name which uniquely holds settings.

1Amplitude client = Amplitude.getInstance("YOUR_INSTANCE_NAME");

Configure the SDK

Name Description Default Value
setServerUrl() String. The server url events are uploaded to. For example, Amplitude.getInstance().setServerUrl("https://www.your-server-url.com"). https://api2.amplitude.com/2/httpapi
useBatchMode() Boolean. Whether to use batch API. By default, the SDK will use the default serverUrl. For example, Amplitude.getInstance().useBatchMode(true). false
setLogMode() AmplitudeLog.LogMode. The level at which to filter out debug messages. For example, Amplitude.getInstance().setLogMode(AmplitudeLog.LogMode.DEBUG);. AmplitudeLog.LogMode.ERROR
setEventUploadThreshold() int. SDK will attempt to upload once unsent event count exceeds the event upload threshold or reach eventUploadPeriodSeconds interval. For example, Amplitude.getInstance().setEventUploadThreshold(50);. 10
setEventUploadPeriodMillis() int. The amount of time SDK will attempt to upload the unsent events to the server or reach eventUploadThreshold threshold. The input parameter is in milliseconds. For example, Amplitude.getInstance().setEventUploadPeriodMillis(200000);. 10 seconds
setCallbacks() AmplitudeCallbacks. Event callback which are triggered after event sent. null
setProxy() Proxy. Custom proxy for https requests. For example, Amplitude.getInstance().setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.domain.com", port)));. Proxy.NO_PROXY
setFlushTimeout() long. Events flushing thread timeout in milliseconds. For example, Amplitude.getInstance().setFlushTimeout(2000L);. 0
setOptions() Options. A dictionary of key-value pairs that represent additional instructions for server save operation. For example, Amplitude.getInstance().setOptions(new Options().setMinIdLength(8));. See the available options.


Name Description Default Value
Options.setMinIdLength() Integer. Set the minimum length for user id or device id. For example, Amplitude.getInstance().setOptions(new Options().setMinIdLength(8));. 5
Options.setHeaders() Map<String, String>. Set the custom headers. For example, Amplitude.getInstance().setOptions(new Options().setHeaders(new HashMap<>(Map.of("Custom Header", "value"))));. {"Content-Type", "application/json", "Accept", "application/json"}
Options.addHeader() String, String. Add more custom headers. For example, Amplitude.getInstance().setOptions(new Options().addHeader("Custom Header", "value"));. {"Content-Type", "application/json", "Accept", "application/json"}

Configure batching behavior

To support high-performance environments, the SDK sends events in batches. Every event logged by logEvent method is queued in memory. Events are flushed in batches in background. You can customize batch behavior with setEventUploadThreshold and setEventUploadPeriodMillis. By default, the SDK is in regular mode with serverUrl to https://api2.amplitude.com/2/httpapi. For customers who want to send large batches of data at a time, switch to batch mode by setting useBatchMode to true to set setServerUrl to batch event upload API https://api2.amplitude.com/batch. Both the regular mode and the batch mode use the same flush queue size and flush intervals.

1Amplitude client = Amplitude.getInstance();
2// Events queued in memory will flush when number of events exceed upload threshold
3// Default value is 10
6// Events queue will flush every certain milliseconds based on setting
7// Default value is 10,000 milliseconds
10// Using batch mode with batch API endpoint, `https://api2.amplitude.com/batch`

You can also flush events on demand.


For customers who want to send large batches of data at a time, for example through scheduled jobs, rather than in a continuous real-time stream, Amplitude provides the batch mode.

Both the regular mode and the batch mode use the same events upload threshold and flush time intervals. The batch mode allows larger payload size (20MB) and has a higher throttling limit.

Due to the higher rate of data that's permitted by this mode, data sent by batch mode may be delayed based on load. You can see a usage example in this project on GitHub.

1// Enable batch mode
4// Disable batch mode

Configure a custom HTTP proxy

New in version 1.9.0. Set and unset custom proxy for HTTP requests.

1// Set proxy for http requests
2client.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.domain.com", port)));
4// Unset proxy

Configure a custom logger

New in version 1.10.0. Set a customized logger for the Amplitude client.

1// Set logger
2client.setLogger(new AmplitudeLog() {
3 @Override
4 public void log(String tag, String message, LogMode messageMode) {
5 if (messageMode.level >= logMode.level) {
6 // implement using custom logging framework and format
7 }
8 }

Configure minIdLength and header

Amplitude Java SDK supports customizing the min length of ID and header on the version later than 1.7.0.

1// Set logger
2client.setOptions(new Options()
3 .addHeader("Custom Header", "value")
4 .setMinIdLength(5));

Configure event flushing and thread timeout

New in version 1.10.0. Set events flushing thread timeout in milliseconds. If set to a positive long integer, events flushing tasks time out and trigger callbacks for those events.

1client.setFlushTimeout(2000L); // 2 seconds

Shutdown client and release resource

New in version 1.10.0. Stops the Amplitude client from accepting new events and shuts down the threads pool. Events in the buffer trigger callbacks. A new instance is created and returned if you call Amplitude.getInstance(INSTANCE_NAME) with the same instance name.


Send events


This SDK uses the HTTP V2 API and follows the same constraints for events. Make sure that all events logged in the SDK have the event_type field and at least one of device_id or user_id, and follow the HTTP API's constraints on each of those fields.

To prevent instrumentation issues, device IDs and user IDs must be strings with a length of 5 characters or more. If an event contains a device ID or user ID that's too short, the ID value is removed from the event. If the event doesn't have a user_id or device_id value, the upload may be rejected with a 400 status. Override the default minimum length of 5 characters by passing the min_id_length option with the request.

Events represent how users interact with your application. For example, "Button Clicked" may be an action you want to track. In Java, logEvent only accepts an event object. See the HTTP V2 API for available event object keys.


For testing the Java SDK, make sure your main thread continues until the background daemon thread that has the Amplitude HTTP request is finished. Otherwise, the main thread terminated earlier than the daemon thread will lead logEvent to fail silently.

1Amplitude client = Amplitude.getInstance();
2client.logEvent(new Event("Button Clicked", "test_user_id"));

Events with properties

Events can also contain properties. They provide context about the event taken. For example, "hover time" may be a relevant event property to "button click."

1JSONObject eventProps = new JSONObject()
2 .put("Hover Time", 10)
3 .put("prop_2", "value_2");
5Event event = new Event("Button Clicked", userId);
6event.eventProperties = eventProps;

Events witih groups

Amplitude supports assigning users to groups and performing queries, such as Count by Distinct, on those groups. If at least one member of the group has performed the specific event, then the count includes the group.

For example, you want to group your users based on what organization they're in by using an 'orgId'. Joe is in 'orgId' '10', and Sue is in 'orgId' '15'. Sue and Joe both perform a certain event. You can query their organizations in the Event Segmentation Chart.

When setting groups, define a groupType and groupName. In the previous example, 'orgId' is the groupType and '10' and '15' are the values for groupName. Another example of a groupType could be 'sport' with groupName values like 'tennis' and 'baseball'.

Setting a group also sets the groupType:groupName as a user property, and overwrites any existing groupName value set for that user's groupType, and the corresponding user property value. groupType is a string, and groupName can be either a string or an array of strings to indicate that a user is in multiple groups.


See examples of group functionality in the demo application.


If Joe is in 'orgId' '10', then the groupName would be '10':

1JSONObject groups = new JSONObject();
2groups.put("orgId", 10);
4Event setGroupEvent = new Event("$identify", userId);
5setGroupEvent.groups = groups;
6setGroupEvent.userProperties = groups;

If Joe is in 'sport' 'tennis' and 'soccer', then the groupName would be '["tennis", "soccer"]'.

1JSONObject groups = new JSONObject();
2groups.put("sport", new String[] {"tennis", "soccer"});
4Event setGroupsEvent = new Event("$identify", userId);
5setGroupsEvent.groups = groupProps;
6setGroupsEvent.userProperties = groups;

You can also use logEvent to set event-level groups. With event-level groups, the group designation applies only to the specific event being logged, and doesn't persist on the user.

1JSONObject groups = new JSONObject();
2groups.put("orgId", 10);
4Event event = new Event('event type', userId);
5event.groups = groups;

After setting groups, you can then set or update the properties of particular groups. However, these updates will only affect events going forward.

1JSONObject groups = new JSONObject()
2 .put("org", "engineering")
3 .put("department", "sdk");
4JSONObject groupProps = new JSONObject()
5 .put("technology", "java")
6 .put("location", "sf");
8Event event = new Event("$groupidentify", userId);
9event.groups = groups;
10event.groupProperties = groupProps;

Set user properties


Don't track any user data that may be against your privacy terms.

Use event.userProperties as a shorthand to set multiple user properties at one time.

1Event event = new Event("Button Clicked", "test_user_id");
3JSONObject userProps = new JSONObject();
4double[] arr = {1,2,4,8};
5try {
6 userProps.put("team", "red").put("running_times", arr);
7} catch (JSONException e) {
8 e.printStackTrace();
9 System.err.println("Invalid JSON");
12event.userProperties = userProps;

Set device information

Unlike the Android SDK or iOS SDK, device information in Java SDK isn't collected via SDK. Device information like device id, device brand, device manufacturer, and device model can be set as properties in each event.

1Event event = new Event("Button Clicked", "test_user_id");
2event.deviceId = "device_id";
3event.deviceBrand = "device_brand";
4event.deviceManufacturer = "device_manufacturer";
5event.deviceModel = "device_model";

Set session information

You can set sessionId in an event. This pattern also applies to other properties like city and price. You can see a full list of events properties in Event.java.

1Event event = new Event("Button Clicked", "test_user_id");
2event.sessionId = 1;

Amplitude callbacks

Support for AmplitudeCallBacks is available beginning with 1.4.0. You can trigger a callback when event is sent to server or failed after retries.

1Amplitude client = Amplitude.getInstance();
2AmplitudeCallbacks callbacks =
3 new AmplitudeCallbacks() {
4 @Override
5 public void onLogEventServerResponse(Event event, int status, String message) {
6 // Event: Event processed.
7 // status: response code, like 200, 400, etc.
8 // message: success or error message.
9 }

From 1.5.0, callbacks can be added to event level and triggered when the event is sent to server or failed after retries. One event can trigger both client level callbacks and event level callbacks.

1Amplitude client = Amplitude.getInstance();
2AmplitudeCallbacks eventCallbacks =
3 new AmplitudeCallbacks() {
4 @Override
5 public void onLogEventServerResponse(Event event, int status, String message) {
6 // Event: Event processed.
7 // status: response code, like 200, 400, etc.
8 // message: success or error message.
9 }
11client.logEvent(event, eventCallbacks)


Middleware allows you to extend Amplitude by running a sequence of custom code on every event. This pattern is flexible and can be used to support event enrichment, transformation, filtering, routing to third-party destinations, and more.

Each middleware is a simple interface with a run method:

1void run(MiddlewarePayload payload, MiddlewareNext next);

The payload contains the event being sent as well as an optional extra that allows you to pass custom data to your own middleware implementations.

To invoke the next middleware in the queue, use the next function. You must call next.run(payload) to continue the middleware chain. If a middleware doesn't call next, then the event processing stop executing after the current middleware completes.

Add middleware to Amplitude via client.addEventMiddleware. You can add as many middleware as you like. Each middleware runs in the order in which it was added.

You can find examples for Java and Kotlin.


When debugging, check the logs. The SDK prints error messages.

If you have problems, open an issue on the GitHub issues page.

Was this page helpful?

Thanks for your feedback!

July 1st, 2024

Need help? Contact Support

Visit Amplitude.com

Have a look at the Amplitude Blog

Learn more at Amplitude Academy

© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.