The Amp Devcenter Test Developer Hub

Welcome to the Amp Devcenter Test developer hub. You'll find comprehensive guides and documentation to help you start working with Amp Devcenter Test as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Java

The backend Java SDK (separate from Android)

SDK Installation

1a. Maven

Use Gradle or another build system to resolve the Java SDK dependency. The following example is for Gradle:

dependencies {
    implementation 'org.json:json:20201115'
    implementation 'com.amplitude:java-sdk:1.2.1'
}

1b. Download (alternative)

Download the latest JAR file and add it to the project's buildpath. See instructions for your IDE.

EU Data Residency

Sending data to Amplitude's EU servers, you need to configure the server url during the initialization.

Amplitude amplitude = Amplitude.getInstance();
amplitude.init("API KEY");
amplitude.setServerUrl("https://api.eu.amplitude.com/2/httpapi");

Usage & Examples

Importing

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

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

Initialization

Initialization is necessary before any instrumentation is done. The API key for your Amplitude project is required.

Amplitude client = Amplitude.getInstance();
client.init("YOUR_API_KEY");

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

Amplitude client = Amplitude.getInstance("YOUR_INSTANCE_NAME")
  .init("YOUR_API_KEY");

Sending Events

Events represent how users interact with your application. For example, “Button Clicked” may be an action you want to track.

🚧

IMPORTANT NOTE

For testing Java SDK out, please 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.

amplitude.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."

Event event = new Event("Button Clicked", "test_user_id");

JSONObject eventProps = new JSONObject();
try {
  eventProps.put("Hover Time", 10).put("prop_2", "value_2");
} catch (JSONException e) {
  System.err.println("Invalid JSON");
  e.printStackTrace();
}

event.eventProperties = eventProps;

client.logEvent(event);

Set User Properties

🚧

Privacy and Tracking

Please be sure to not track any user data that may be against your privacy terms. If you need any assistance with privacy concerns, then please reach out to our Platform team.

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

Event event = new Event("Button Clicked", "test_user_id");

JSONObject userProps = new JSONObject();
double[] arr = {1,2,4,8};
try {
  userProps.put("team", "red").put("running_times", arr);
} catch (JSONException e) {
  e.printStackTrace();
  System.err.println("Invalid JSON");
}

event.userProperties = userProps;
client.logEvent(event);

Set Device Information

Unlike other SDKs like Android SDK or iOS SDK, device information in Java SDK is not collected via SDK. Device information like device id, device brand, device manufacturer, and device model can be set as properties in event if needed. However, for now that information may be needed to set in every event.

Event event = new Event("Button Clicked", "test_user_id");
event.deviceId = "device_id";
event.deviceBrand = "device_brand";
event.deviceManufacturer = "device_manufacturer";
event.deviceModel = "device_model";
client.logEvent(event);

Set Session Information

Session id can set in event as well, like other properties. This pattern also applies to other properies like city, price, etc.

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

Troubleshooting

When debugging, please also check the logs. Amplitude-Java SDK will print out error messages on where we implement it.

Need Help?

Please create new issues directly at the GitHub issues page.

Updated about a month ago


Java


The backend Java SDK (separate from Android)

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.