Java (Sources)

  • Sources & SDKs

Getting Started

This library lets you record analytics data from your Java code. Once installed, the requests will hit our servers and then be routed to the destinations of your choice.

You can install the library here.

You can use this library in your web server controller code- it is built for high performance and uses and internal queue to make all calls non-blocking and fast. It will batch messages and flush asynchronously to our servers.

Install the Library

We reccommend installing the library with a build system like Maven. If you do it this way, you will have much less trouble upgrading and swapping out destinations.

The library is distributed as a jar dependency via Maven Central. Here's what it would look like with Maven.

Add to pom.xml:


Initialize the SDK

Before you can start sending us events, you'll need to initialize an instance of the Analytics class. Do this using the Analytics.builder class, inputting the Source ID found in the source settings of your MetaRouter UI.

Analytics analytics = Analytics.builder("Your Source ID")

Note that there exists an internal AnalyticsClient class, not to be confused with the public Analytics class.

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. Be sure to provide either a userId or anonymousID for each message, as failing to do so will raise an exception at runtime.

Calls in Java

Check out the below calls and their use cases to determine the calls that you need to make. We have also included examples of how you'd call specific objects in Java.

Note: Thee following examples use the Guava immutable map style, but feel free to use standard Java maps instead.


The identify method helps you associate your users and their actions to a unique and recognizable userID and any optional traits that you know about them. We recommend calling an identify a single time - when the user's account is first created and only again when their traits change.

        .put("name", "Buzz Aldrin")
        .put("email", "")
        .put("gender", "male")
        .put("title", "Second person to visit the moon")

The above call identifies Buzz by his unique userID and labels him with name, email, gender, and title traits. For a complete library of the traits that you're able to assign to a user, check out our API Calls doc.


To get to a more complete event tracking analytics setup, you can add a track call to your website. This will tell MetaRouter which actions you are performing on your site. With track, each user action triggers an “event,” which can also have associated properties.

analytics.enqueue(TrackMessage.builder("Item Purchased")
        .put("revenue", 50.00)
        .put("shipping", "Next-Day")

The above call tells us that someone has purchased an item for 50 dollars and has selected a "next day" shipping option.


Note: The screen call pulls the same data as a page call, but is used for mobile rather than web sources.

        .put("category", "Space")
        .put("path", "/space/moonlanding")

The above call tells us that someone has viewed a MoonLanding page that is categorized in a Space section of the mobile app.


The group method associates an identified user with a company, organization, project, etc.

        .put("name", "MetaRouter")
        .put("size", 55)
        .put('website", "")

The above call assigns the user with the "MetaRouter" group and gives that group the "size" and "website" traits.


The alias method combines two unassociated User IDs.


We might use the above call in the following way:

// the anonymous user clicks a button
track("anonymous_user", "Click Button");
// the anonymous user signs up and is aliased
alias("anonymous_user", "");
// the signed up user is identified
identify("", new Traits("plan", "Pro"));
// the identified user clicks a button
track("", "Click Button");

Note that making an alias call means that we are able to combine the user's anonymous actions with their identified actions, so that we can get a clear picture of their user journey.

© 2020 MetaRouter

Disclaimer: MetaRouter Cloud leverages code from several of Segment's fabulous library of open-source tools, including their source SDKs, destinations, and core Analytics.js library. As some of the core configurations outlined in our docs are largely identical to Segment's, we often re-purpose, and in some cases copy, directly from Segment's docs. In the cases where we have forked Segment's tools, we acknowledge and attribute all credit to Segment for their creation and subsequent documentation.