Analytics for Java

Community x
Maintenance x
Flagship ✓
?

Flagship libraries offer the most up-to-date functionality on Segment’s most popular platforms. Segment actively maintains flagship libraries, which benefit from new feature releases and ongoing development and support.


Maven Central

Segment’s Java library lets you record analytics data from your Java code. The requests hit Segment’s servers, and then routes your data to any analytics service you enable on your destinations page.

This library is open-source, so you can check it out on GitHub.

All of Segment’s server-side libraries are built for high-performance, so you can use them in your web server controller code. This library uses an internal queue to make all calls non-blocking and fast. It also batches messages and flushes asynchronously to Segment’s servers.

Want to stay updated on releases? Subscribe to the release feed.

Getting Started

Maven Central

The recommended way to install the library for Java is with a build system like Gradle or Maven. This makes it simple to upgrade and swap out destinations. The library is distributed using Maven Central as a jar dependency.

Here’s what it would look like with Maven:

Add to pom.xml:

<dependency>
  <groupId>com.segment.analytics.java</groupId>
  <artifactId>analytics</artifactId>
  <version>LATEST</version>
</dependency>

or if you’re using Gradle:

implementation 'com.segment.analytics.java:analytics:+'

Initialize the SDK

Before you can send events to Segment, you need to initialize an instance of the Analytics class. To do so, you must use the Analytics.Builder class.

Analytics analytics = Analytics.builder(writeKey).build();

Of course, you’ll want to replace writeKey with your actual Write Key which you can find in Segment under your source settings.

The Builder can also be used to customize behavior of the Analytics instance.

Note: There is an internal AnalyticsClient class. Do not confuse this class with the public Analytics class and do not use this class directly.

The Analytics class has a method called enqueue that takes a MessageBuilder. Each message class has a corresponding builder that is used to construct instances of a message.

Although not enforced at compile time, make sure you provide either of userId or anonymousId for each message. Failing to do so will raise an exception at runtime.

The following examples use Guava’s immutable maps, but feel free to use plain old Java maps instead.

Regional configuration

For Business plans with access to Regional Segment, you can use the host configuration parameter to send data to the desired region:

  1. Oregon (Default) — api.segment.io/
  2. Dublin — events.eu1.segmentapis.com/

Basic tracking methods

The basic tracking methods below serve as the building blocks of your Segment tracking. They include Identify, Track, Page, Group, and Alias. These methods correspond with those used in the Segment Spec. The documentation on this page explains how to use these methods in Analytics for Java.

For any of the different methods described on this page, you can replace the properties and traits in the code samples with variables that represent the data collected.

Note that sending a property or trait with a null value won’t be possible as Guava’s immutable maps will reject the null value and the GSON library used to serialize the Java object will ignore it. As a workaround, you can send an empty string instead of a null value in on your properties or traits.

Identify

Identify lets you tie a user to their actions and record traits about them. It includes a unique User ID and any optional traits you know about them.

Segment recommends calling Identify a single time when the user’s account is first created, and only identifying again later when their traits change.

Example Identify call:

Map<String, String> map = new HashMap();
map.put("name", "Michael Bolton");
map.put("email", "mbolton@example.com");

analytics.enqueue(IdentifyMessage.builder()
        .userId("f4ca124298")
        .traits(map));

This call identifies Michael by his unique User ID (the one you know him by in your database) and labeling him with name and email traits.

The Identify call has the following fields:

userId String The ID for this user in your database.
traits Traits, optional A dictionary of traits you know about the user. Things like: email, name or friends.

Note: The enqueue method takes a MessageBuilder instance and not a Message instance directly. This is to allow you to use a MessageTransformer that applies to all incoming messages and transform or add data.

Find details on the identify method payload in the Segment Spec.

Track

Track lets you record the actions your users perform. Every action triggers what Segment calls an “event”, which can also have associated properties.

You’ll want to track events that you’re interested in, such as Signed Up, Item Purchased or Article Bookmarked.

To get started, Segment recommends tracking just a few important events. You can always add more later.

Example Track call:

analytics.enqueue(TrackMessage.builder("Item Purchased")
    .userId("f4ca124298")
    .properties(ImmutableMap.builder()
        .put("revenue", 39.95)
        .put("shipping", "2-day")
        .build()
    )
);

This example Track call tells you that your user just triggered the Item Purchased event with a revenue of $39.95 and chose your hypothetical ‘2-day’ shipping.

Track event properties can be anything you want to record. In this case, revenue and shipping.

The Track call has the following fields:

userId String The ID for this user in your database.
event String The name of the event you’re tracking. Segment recommends human-readable names like Song Played or Status Updated.
properties Properties, optional A dictionary of properties for the event. If the event was Product Added, it might have properties like price or product.

Find details on best practices in event naming as well as the Track method payload in the Segment Spec.

Screen

The Screen method lets you record whenever a user sees a screen of your mobile app, along with optional extra information about the screen being viewed.

You’ll want to record a screen event an event whenever the user opens a screen in your app. This could be a view, fragment, dialog or activity depending on your app.

Not all services support screen, so when it’s not supported explicitly, the screen method tracks as an event with the same parameters.

Example Screen call:

analytics.enqueue(ScreenMessage.builder("Schedule")
    .userId("f4ca124298")
    .properties(ImmutableMap.builder()
        .put("category", "Sports")
        .put("path", "/sports/schedule")
        .build()
    )
);

The Screen call has the following fields:

userId String The ID for this user in your database.
name String The webpage name you’re tracking. Segment recommends human-readable names like Login or Register.
properties Properties, optional A dictionary of properties for the screen visit. If the screen was Login, it might have properties like path or title.

Find details on the Screen payload in the Segment Spec.

Page

The Page method lets you record whenever a user sees a page of your website, along with optional extra information about the page being viewed.

Not all services support page, so when it’s not supported explicitly, the Page method typically tracks as an event with the same parameters.

Example Page call:

analytics.enqueue(PageMessage.builder("Schedule")
    .userId("f4ca124298")
    .properties(ImmutableMap.builder()
        .put("category", "Sports")
        .put("path", "/sports/schedule")
        .build()
    )
);

The Page call has the following fields:

userId String The ID for this user in your database.
name String The webpage name you’re tracking. Segment recommends human-readable names like Login or Register.
properties Properties, optional A dictionary of properties for the page visit. If the page was Login, it might have properties like path or title.

Find details on the Page payload in the Segment Spec.

Group

Group lets you associate an identified user user with a group. A group could be a company, organization, account, project or team. It also lets you record custom traits about the group, like industry or number of employees.

This is useful for tools like Intercom, Preact and Totango, as it ties the user to a group of other users.

Example Group call:

analytics.enqueue(GroupMessage.builder("some-group-id")
    .userId("f4ca124298")
    .traits(ImmutableMap.builder()
        .put("name", "Segment")
        .put("size", 50)
        .build()
    )
);

The Group call has the following fields:

userId String The ID for this user in your database.
groupId String The ID for this group in your database.
traits Traits, optional A dictionary of traits you know about the group. Things like: name or website.

Find more details about Group, including the Group payload, in the Segment Spec.

Alias

Alias is how you associate one identity with another. This is an advanced method, but it is required to manage user identities successfully in some of our destinations.

In Mixpanel it’s used to associate an anonymous user with an identified user once they sign up. For Kissmetrics, if your user switches IDs, you can use ‘alias’ to rename the ‘userId’.

Example Alias call:

analytics.enqueue(AliasMessage.builder("previousId")
    .userId("f4ca124298")
);

Here’s a full example of how you might use the Alias call:

// the anonymous user does actions ...
track("anonymous_user", "Anonymous Event");
// the anonymous user signs up and is aliased
alias("anonymous_user", "identified@example.com");
// the signed up user is identified
identify("identified@example.com", new Traits("plan", "Free"));
// the identified user does actions ...
track("identified@example.com", "Identified Action");

For more details about Alias, including the Alias call payload, check out the Segment Spec.


Historical Import

You can import historical data by adding the timestamp argument to any of your method calls. This can be helpful if you’ve just switched to Segment.

Historical imports can only be done into destinations that can accept historical timestamped data. Most analytics tools like Mixpanel, Amplitude, Kissmetrics, etc. can handle that type of data just fine. One common destination that does not accept historical data is Google Analytics since their API cannot accept historical data.

Note: If you’re tracking things that are happening right now, leave out the timestamp and Segment’s servers will timestamp the requests for you.

Date historicalDate = ...;
analytics.enqueue(TrackMessage.builder("Button Clicked")
    .userId("f4ca124298")
    .timestamp(historicalDate)
);

Selecting Destinations

The Alias, Group, Identify, Page, and Track calls can all be passed an object of integrations that lets you turn certain destinations on or off. By default all destinations are enabled.

Similar to timestamp, the builders take a map of destinations that control which analytics destinations you want each message to go to.

analytics.enqueue(TrackMessage.builder("Button Clicked")
    .userId("f4ca124298")
    .enableIntegration("All", false)
    .enableIntegration("Amplitude", true)
);

In this case, you’re specifying that you want this identify to only go to Amplitude. "All", false says that no destination should be enabled unless otherwise specified, and { "Amplitude", true } turns on Amplitude.

Destination flags are case sensitive and match the destination’s name in the docs (for example, “AdLearn Open Platform”, “awe.sm”, or “MailChimp”).

Note:

  • Business Tier users can filter Track calls right from the Segment UI on your source schema page. Segment recommends using the UI if possible since it’s a much simpler way of managing your filters and can be updated with no code changes on your side.

  • If you are on a grandfathered plan, events sent server-side that are filtered through the Segment dashboard still count towards your API usage.

Context

If you’re running a web server, you might want to send context variables such as userAgent or ip with your page or screen calls. You can do so by setting the Context.

analytics.enqueue(TrackMessage.builder("Button Clicked")
    .userId("f4ca124298")
    .context(ImmutableMap.builder()
        .put("ip", "12.212.12.49")
        .put("language", "en-us")
        .build()
    )
);

Batching

Segment’s libraries are built to support high performance environments. That means it is safe to use analytics-java on a web server that’s serving hundreds of requests per second. For more information, check out the java benchmark.

Every method you call does not result in an HTTP request, but is queued in memory instead. Messages are flushed in batch in the background, which allows for much faster operation.

There is a maximum of 500KB per batch request and 32KB per call.

HTTP Tracking API limits

Segment's HTTP Tracking API accepts batch requests up to 500KB. To avoid errors in event creation, ensure that individual event payload sizes remain below 32KB.

How do I flush right now?!

You can also flush on demand. For example, at the end of your program, you’ll want to flush to make sure there’s nothing left in the queue. Just call the flush method:

analytics.flush()

Calling this method will notify the client to upload any events in the queue.

How do I gzip requests?

The Java library does not automatically gzip requests, but allows you to do so if you desire using interceptors in OkHttp. See the sample app in Segment’s analytics-java repository for a working example.

Multiple Clients

Different parts of your app may require different types of batching. In that case, you can initialize different Analytics instances. Simply use the builder method (you can reuse it with different parameters) to create different instances.

Analytics.Builder builder = Analytics.builder(writeKey);
Analytics first = builder.build();
Analytics second = builder.flushInterval(2, TimeUnit.SECONDS).build();

Logging

You can enable verbose logging to see what data is being sent over HTTP when debugging issues. You can enable logging by initializing the library like this:

Log STDOUT = new Log() {
    @Override
    public void print(Level level, String format, Object... args) {
        System.out.println(level + ":\t" + String.format(format, args));
    }

    @Override
    public void print(Level level, Throwable error, String format, Object... args) {
        System.out.println(level + ":\t" + String.format(format, args));
        System.out.println(error);
    }
};

Analytics analytics = Analytics.builder("<writeKey>")
        .log(STDOUT)
        .build();

For more advance logging, you can check out the sample code in Segment’s open-source library.

Java Support

Segment supports Java 8, 9, 10, and 11. The library may work on other versions of Java as well, however Segment doesn’t test for compatibility on unsupported versions.

Snapshots

To add a snapshot dependency to your builds, make sure you add the snapshot repository so your build system can look up the dependency.

Maven users can add the following to their pom.xml:

<repository>
    <id>ossrh</id>
    <name>Sonatype Snapshot Repository</name>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    <releases>
        <enabled>false</enabled>
    </releases>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>

Gradle users should declare this in their repositories block:

repositories {
  mavenCentral()
  maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

Troubleshooting

The following tips often help resolve common issues.

No events in my debugger

  1. Double check that you’ve followed all the steps in the Quickstart.

  2. Make sure that you’re calling a Segment API method once the library is successfully installed—identify, track, etc.

  3. Make sure your application isn’t shutting down before the Analytics.Client local queue events are pushed to Segment. You can manually call Analytics.Client.Flush() to ensure the queue is fully processed before shutdown.

Other common errors

If you are experiencing data loss from your Java source, you may be experiencing one or more of the following common errors:

  • Payload is too large: If you attempt to send events larger than 32KB per normal API request or batches of events larger than 500KB per request, Segment’s tracking API responds with 400 Bad Request. Try sending smaller events (or smaller batches) to correct this error.

  • Identifier is not present: Segment’s tracking API requires that each payload has a userId and/or anonymousId. If you send events without either the userId or anonymousId, Segment’s tracking API responds with an no_user_anon_id error. Check the event payload and client instrumentation for more details.

  • Track event is missing name: All Track events to Segment must have a name in string format.

  • Event dropped during deduplication: Segment automatically adds a messageId field to all payloads and uses this value to deduplicate events. If you’re manually setting a messageId value, ensure that each event has a unique value.

  • Incorrect credentials: Double check your credentials for your downstream destination(s).

  • Destination incompatibility: Make sure that the destination you are troubleshooting can accept server-side API calls. You can see compatibility information on the Destination comparison by category page and in the documentation for your specific destination.

  • Destination-specific requirements: Check out the destination’s documentation to see if there are other requirements for using the method and destination that you’re trying to get working.

This page was last modified: 30 May 2024



Get started with Segment

Segment is the easiest way to integrate your websites & mobile apps data to over 300 analytics and growth tools.
or
Create free account