Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Java Server Events SDK

Maven Central Status

This is the mParticle Java SDK for the server-based Events API - use it to send your data to the mParticle platform and off to 250+ integrations! The SDK is designed to be use in a variety of environments. It provides a Retrofit interface as well as serializable models if you prefer to handle HTTP transport on your own.

This SDK is designed only to be used in a server-side environment. If you're looking for our Android SDK - please see here.


Java 1.7 and later.

Getting Started

Add the Dependency

The SDK is available via Maven Central and jCenter.


implementation 'com.mparticle:server-events-sdk:2.3.3'



Local Maven

You can also install it locally and use it from a local Maven repository:

./gradlew uploadArchives

Create a Batch

All data that passes through mParticle does so in the form of a "batch." A batch describes identities, attributes, events, and other information related to a single user. This SDK lets you upload either single batches or multiple batches at a time.

The full schema of a batch is documented in the mParticle Events API overview. The models in this SDK directly match the JSON referenced in the overview.

Batch batch = new Batch();

// Set a Data Plan
Context context = new Context();
DataPlanContext dpContext = new DataPlanContext();

// Set user identities
        new UserIdentities()

// Set device identities
        new DeviceInformation()

// Set user attributes
Map<String, Object> userAttributes = new HashMap<>();
userAttributes.put("foo", "bar");
userAttributes.put("foo-array", new String[]{"bar1", "bar2"});
userAttributes.put("foo-array-2", Arrays.asList("bar3","bar4"));

It's critical to include either user or device identities with your server-side data

Create Events

All mParticle events have a similar structure:

  • event_type: this is the type of event, such as custom_event and commerce_event
  • data: this contains common properties of all events, as well as properties specific to each event_type

The following are common properties that all events share, as represented by the CommonEventData class:

	"data" :
		"event_id" : 6004677780660780000,
		"source_message_id" : "e8335d31-2031-4bff-afec-17ffc1784697",
		"session_id" : 4957918240501247982,
		"session_uuid" : "91b86d0c-86cb-4124-a8b2-edee107de454",
		"timestamp_unixtime_ms" : 1402521613976,
		"location" : {},
		"device_current_state" : {},
		"custom_attributes": {},
		"custom_flags": {}
	"event_type" : "custom_event"

The Java Server Events SDK represents this structure via an event and an event-data class for each unique event type. For example, CustomEvent which can be populated by a CustomEventData instance.

Custom Events

Map customAttributes = new HashMap<>();
customAttributes.put("foo", "bar");
CustomEvent event = new CustomEvent().data(
        new CustomEventData()
                .eventName("My Custom Event Name")

Screen Events

ScreenViewEvent event = new ScreenViewEvent().data(
        new ScreenViewEventData()
                .screenName("foo screen")

Commerce Events

Product product = new Product()
    .totalProductAmount(new BigDecimal("123.12"))
ProductAction action = new ProductAction()
    .totalAmount(new BigDecimal("123.12"))
CommerceEvent event = new CommerceEvent().data(
        new CommerceEventData().productAction(action)

Full Upload Example

Create an EventsApi instance

EventsApi is a Retrofit-compatible interface, allowing you to use the rich feature-set of the Retrofit and OkHttp libraries, such as queueing and asynchronous requests.

Create an API instance with your mParticle workspace credentials. These credentials may be either "platform" (iOS, Android, etc) or "custom feed" keys:

EventsApi api = new ApiClient("API KEY", "API-SECRET")

The mParticle Events API leverages HTTP basic authentication over TLS.

Perform a Synchronous Upload

The SDK supports both multi-batch ("bulk") or single-batch uploads:

//assemble your Batch
Batch batch = new Batch();

batch.userIdentities(new UserIdentities()

// Add your events
Map customAttributes = new HashMap<>();
customAttributes.put("foo", "bar");
CustomEvent customEvent = new CustomEvent().data(
        new CustomEventData()
                .eventName("foo event")

ScreenViewEvent screenEvent = new ScreenViewEvent().data(
        new ScreenViewEventData()
                .screenName("foo screen")

EventsApi api = new ApiClient("API KEY", "API-SECRET").createService(EventsApi.class);

// Perform a bulk upload
List<Batch> bulk = new ArrayList<>();
Call<Void> bulkResult = api.bulkUploadEvents(bulk);
Response<Void> bulkResponse = bulkResult.execute();

// Perform a single upload
Call<Void> singleResult = api.uploadEvents(batch);
Response<Void> singleResponse = singleResult.execute();


Apache License 2.0