Java Quickstart

Welcome to mParticle! Let’s send your first event.

Step 1: Generate your API key

Create a Custom Feed on the Setup page to generate server-to-server API credentials.

Step 2: Add the mParticle SDK dependency

Our Java SDK is available via Gradle or Maven.

Gradle

Add the SDK dependency to your build.gradle file.

dependencies {
  implementation 'com:mparticle:server-events-sdk:2+'
}

Maven

Add the SDK dependency to your pom.xml file.

<dependency>
  <groupId>com.mparticle</groupId>
  <artifactId>server-events-sdk</artifactId>
  <version>2.0.0</version>
</dependency>

Step 3: Call the Events API

Now you can integrate mParticle in your Java application.

// configure API
EventsApi api = new ApiClient(
        "YOUR_API_KEY",
        "YOUR_API_SECRET")
        .createService(EventsApi.class);

// assemble an event batch
Batch batch = new Batch();
batch.environment(Batch.Environment.DEVELOPMENT);
batch.userIdentities(new UserIdentities()
        .customerId("1234")
        .email("example@foo.com")
);

// create an event
CustomEvent customEvent = new CustomEvent().data(
        new CustomEventData()
                .eventName("bid")
);

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

// add them to an event
customEvent.getData().customAttributes(customAttributes);
batch.addEventsItem(customEvent);

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

Step 4: Verify installation

Go to the Live Stream and watch new events come in as you run your application.

Next Steps

πŸŽ‰πŸŽ‰πŸŽ‰ Congrats on sending your first event to mParticle!

Some ideas on what to do next:

Was this page helpful?