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.
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.
<dependency> <groupId>com.segment.analytics.java</groupId> <artifactId>analytics</artifactId> <version>LATEST</version> </dependency>
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") .endpoint("https://e.metarouter.io") .build();
Note that there exists an internal
AnalyticsClient class, not to be confused with the public
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
anonymousID for each message, as failing to do so will raise an exception at runtime.
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.
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.
analytics.enqueue(IdentifyMessage.builder() .userId("qwerty1234") .traits(ImmutableMap.builder() .put("name", "Buzz Aldrin") .put("email", "email@example.com") .put("gender", "male") .put("title", "Second person to visit the moon") .build() ) );
The above call identifies Buzz by his unique
userID and labels him with
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") .userId("qwerty1234") .properties(ImmutableMap.builder() .put("revenue", 50.00) .put("shipping", "Next-Day") .build() ) );
The above call tells us that someone has purchased an item for 50 dollars and has selected a "next day" shipping option.
screen call pulls the same data as a
page call, but is used for mobile rather than web sources.
analytics.enqueue(ScreenMessage.builder("MoonLanding") .userId("qwerty1234") .properties(ImmutableMap.builder() .put("category", "Space") .put("path", "/space/moonlanding") .build() ) );
The above call tells us that someone has viewed a
MoonLanding page that is categorized in a
Space section of the mobile app.
group method associates an identified user with a company, organization, project, etc.
analytics.enqueue(GroupMessage.builder("some-group-id") .userId("qwerty1234") .traits(ImmutableMap.builder() .put("name", "MetaRouter") .put("size", 55) .put('website", "www.metarouter.io") .build() ) );
The above call assigns the user with the "MetaRouter" group and gives that group the "size" and "website" traits.
alias method combines two unassociated User IDs.
analytics.enqueue(AliasMessage.builder("previousId") .userId("qwerty1234") );
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", "firstname.lastname@example.org"); // the signed up user is identified identify("email@example.com", new Traits("plan", "Pro")); // the identified user clicks a button track("firstname.lastname@example.org", "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.