Java SDK

This SDK is a helper library for the mParticle Events HTTP API, it exposes mParticle’s schema as simple models and provides an HTTP client interface. This SDK is stateless and will only send the data that you populate, whereas our mobile SDKs will automatically collect app and device information, session events, install events, and maintain persistence. Read this wiki for a general overview and examples, and contact our customer support team to make sure you’re feeding the platform with the right data to power your integrations. You can find the Java SDK hosted on Github.

Model Overview


All data sent via the SDK must be encapsulated in a Batch struct. Each Batch is associated with a single user. Batch objects must be associated with an environment (development or production) to properly silo your testing and production data.

Batch batch = new Batch();

User Identities

Most use-cases require that data be associated with a user identity, for example:

  • If you’re also sending data to mParticle via our mobile SDKs, set a customer ID both via the mobile SDKs and this SDK so that mParticle can correctly associate data with each user.
  • Several marketing automation and audience integrations are powered by email.
        new UserIdentities()

Device Information

The DeviceInformation object describes the device that should be associated with this batch. Crucially, it exposes properties for device identities (Apple IDFA and Google Advertising ID) which are required for nearly all mParticle Audience integrations.

        new DeviceInformation()

User Attributes

The mParticle audience platform can be powered by only sending a combination of user attributes, used to describe segments of users, and device identities/user identities used to then target those users.

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"));


Events are central to many of mParticle’s integrations; analytics integrations typically require events, and you can create mParticle Audiences based on the recency and frequency of different events. All events should be associated with a timestamp reflecting when they actually occurred, otherwise they will be assigned a timestamp when mParticle receives them.

Custom Event

Custom Events represent specific actions that a user has taken in your app. At minimum they require a name and a type, but can also be associate with a free-form dictionary of key/value pairs.

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

Commerce Event

The Commerce event is central to mParticle’s Commerce measurement. Commerce events can contain many data points but it’s important to understand that there are 3 core variations:

  • Product-based: Used to measure measured datapoints associated with one or more products
  • Promotion-base: Used to measure datapoints associated with internal promotions or campaigns
  • Impression-based: Used to measure interactions with impressions of products and product-listings
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 Example

The SDK provides an interface to the mParticle HTTP API by way of the EventsApi class.

At a minimum, the EventsApi must be initialized using the mParticle key and secret issued for your input.

To find your key and secret if you are using a platform input such as iOS or Roku:

  1. Log into your mParticle account, and navigate to Setup > Inputs in the left nav bar.
  2. From the Platforms tab, select the correct platform from the list to view its configuration.
  3. Copy the key and secret, and use these when initializing the Events API.

To find your key and secret if you are using a custom feed as your data source:

  1. Log into your mParticle account, and navigate to Setup > Inputs in the left nav bar.
  2. Click the Feeds tab.
  3. Click on the feed in the list displayed, and select the configuration you are using.
  4. Copy the key and secret from the model, and use these when initializing the Events API.

You must associate your data with the correct key and secret. If your app is multi-platform, for example, be sure to send your Android data to your Android key/secret, and your iOS data to your iOS key/secret.

By default, the Java SDK will upload to the US1 Data Center URL. If the API key you’re sending to exists in an mParticle Data Center that’s not US1, you must find the right URL for your hosting location on the mParticle docs site. Using this URL, you can override the base url.

// create an API client
ApiClient apiClient = new ApiClient(
// include this if your API keys are from a non-US1 mParticle data center, by default the Java SDK sends to US1
// this example changes to US2, reference here for other data centers:

// create the service instance
EventsApi api = client.createService(EventsApi.class);

// assemble an event batch
Batch batch = new Batch();
batch.userIdentities(new UserIdentities()

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

// create an event
CustomEvent customEvent = new CustomEvent().data(
        new CustomEventData()

// create attributes
Map customAttributes = new HashMap<>();
customAttributes.put("price", 33);

// add them to an event

// upload
Call<Void> singleResult = api.uploadEvents(batch);
Response<Void> singleResponse = singleResult.execute();
System.out.println("Returned code: " + singleResponse.code());

Was this page helpful?