Identifier set in cases where no unique user identifier is available.
traits
Object
Dictionary of the user’s traits like name, email, etc.
context
Object
Optional dictionary of information that provides context about the event.
integrations
Object
Optional dictionary containing the destinations to be enabled or disabled.
timestamp
Timestamp in ISO 8601 format
The event’s timestamp. If not provided, it defaults to the current time when the message was built.
Track
The track method records user actions and their associated properties.
Example:
// Create a map to store event properties
Map<String,Object>properties=newLinkedHashMap<>();properties.put("product_id","P123");properties.put("product_name","Running Shoes");properties.put("price",89.99);properties.put("currency","USD");properties.put("category","Sports");// Send the track event
analytics.enqueue(TrackMessage.builder("Product Added").properties(properties).userId("1hKOmRA4GRlm"));
Parameters:
Field
Type
Description
userId Required, if anonymousId is absent.
String
Unique identifier for a user in your database.
anonymousId Required, if userId is absent.
String
Identifier set in cases where no unique user identifier is available.
event Required
String
Name of the event.
properties
Object
Optional dictionary of the properties associated with the event.
context
Object
Optional dictionary of information that provides context about the event.
integrations
Object
Optional dictionary containing the destinations to be enabled or disabled.
timestamp
Date
The event’s timestamp. If not provided, it defaults to the current time when the message is created. The SDK automatically converts it in the ISO 8601 format before sending to the server.
Page
The page call records page views in your application along with the relevant page information.
Any custom information passed in the context object is merged with the existing context, except for the library information.
Destination filtering
The SDK provides methods to enable or disable sending events to specific destinations by passing the integrations object in your API calls:
analytics.enqueue(TrackMessage.builder("Order Completed").userId("1hKOmRA4GRlm").enableIntegration("All",false)// Disable all destinations
.enableIntegration("Amplitude",true)// Enable only Amplitude
.properties(ImmutableMap.builder().put("revenue",99.99).put("currency","USD").build()));
Destination flags are case-sensitive and must match the destination names in the RudderStack dashboard.
Event batching
The SDK queues events in memory and flushes them in batches for optimal performance. Each SDK API call adds the event to the queue instead of making an immediate HTTP request.
Batch size limits
Maximum batch request size: 500KB
Maximum single event size: 32KB
The RudderStack HTTP Tracking API accepts batch requests up to 500KB. Keep single event payloads below 32KB to avoid errors.
Event flushing
The Java SDK provides a flush method that notifies the RudderStack client to upload the events and make sure no events are left in the queue at any given point.
Manual flush
The flush method usage is shown below:
analytics.flush()
Flush blocking
By default, the Java SDK does not support blocking event flush implicitly. However, you can implement it using the following two classes:
BlockingFlush: Handles up to 65535 parallel flush calls
TierBlockingFlush: No limit on parallel flush calls
Both the BlockingFlush and TierBlockingFlush classes are not included in the core SDK.
BlockingFlush implementation
finalBlockingFlushblockingFlush=BlockingFlush.create();RudderAnalyticsanalytics=RudderAnalytics.builder("<WRITE_KEY>").plugin(blockingFlush.plugin()).setDataPlaneUrl("<DATA_PLANE_URL>").build();// ...YOUR CODE...
analytics.flush();// Triggers a flush.
blockingFlush.block();analytics.shutdown();// Shuts down after the flush is complete.
packagesample;importcom.rudderstack.sdk.java.analytics.RudderAnalytics;importcom.rudderstack.sdk.java.analytics.Callback;importcom.rudderstack.sdk.java.analytics.MessageTransformer;importcom.rudderstack.sdk.java.analytics.Plugin;importcom.rudderstack.sdk.java.analytics.messages.Message;importcom.rudderstack.sdk.java.analytics.messages.MessageBuilder;importjava.util.concurrent.Phaser;/*
* The {@link RudderAnalytics} class doesn't come with a blocking {@link RudderAnalytics#flush()} implementation
* out of the box. It's trivial to build one using a {@link Phaser} that monitors requests and is
* able to block until they're uploaded.
*/publicclassBlockingFlush{publicstaticBlockingFlushcreate(){returnnewBlockingFlush();}BlockingFlush(){this.phaser=newPhaser(1);}finalPhaserphaser;publicPluginplugin(){returnbuilder->{builder.messageTransformer(builder1->{phaser.register();returntrue;});builder.callback(newCallback(){@Overridepublicvoidsuccess(Messagemessage){phaser.arrive();}@Overridepublicvoidfailure(Messagemessage,Throwablethrowable){phaser.arrive();}});};}publicvoidblock(){phaser.arriveAndAwaitAdvance();}}
TierBlockingFlush implementation
finalTierBlockingFlushblockingFlush=TierBlockingFlush.create();RudderAnalyticsanalytics=RudderAnalytics.builder("<WRITE_KEY>").plugin(blockingFlush.plugin()).setDataPlaneUrl("<DATA_PLANE_URL>").build();// ...YOUR CODE...
analytics.flush();// Trigger a flush.
blockingFlush.block();analytics.shutdown();// Shut down after the flush is complete.
packagesample;importcom.rudderstack.sdk.java.analytics.Callback;importcom.rudderstack.sdk.java.analytics.Plugin;importcom.rudderstack.sdk.java.analytics.messages.Message;importjava.util.concurrent.Phaser;/**
* Blocking flush implementor for cases where parties exceed 65535
*/publicclassTierBlockingFlush{privatestaticfinalintMAX_PARTIES_PER_PHASER=(1<<16)-2;// max a phaser can accommodate
publicstaticTierBlockingFlushcreate(){returnnewTierBlockingFlush(MAX_PARTIES_PER_PHASER);}privateTierBlockingFlush(intmaxPartiesPerPhaser){this.currentPhaser=newPhaser(1);this.maxPartiesPerPhaser=maxPartiesPerPhaser;}privatePhasercurrentPhaser;privatefinalintmaxPartiesPerPhaser;publicPluginplugin(){returnbuilder->{builder.messageTransformer(messageTransformationBuilder->{currentPhaser=currentPhaser.getRegisteredParties()==maxPartiesPerPhaser?newPhaser(currentPhaser):currentPhaser;currentPhaser.register();returntrue;});builder.callback(newCallback(){@Overridepublicvoidsuccess(Messagemessage){onResult();}@Overridepublicvoidfailure(Messagemessage,Throwablethrowable){onResult();}privatevoidonResult(){if(currentPhaser.getUnarrivedParties()==0){currentPhaser=currentPhaser.getParent();}currentPhaser.arrive();}});};}publicvoidblock(){currentPhaser.arriveAndAwaitAdvance();}}
Event request compression
Note the following:
The event request compression feature via Gzip is enabled by default in the Java SDK version 3.0.0.
Self-hosted data planes require rudder-server version 1.4+ to support event request compression.
The Java SDK automatically gzips event requests by leveraging interceptors in OkHttp.
When using a custom OkHttp client with the client API, its interceptor configuration takes precedence over the setGZIP setting.
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.