Skip to main content

Installation

Add to your pom.xml:
<dependency>
  <groupId>com.mixpanel</groupId>
  <artifactId>mixpanel-java</artifactId>
  <version>1.5.2</version>
</dependency>
You can also download the JAR directly from Maven Central.

Initialize

import com.mixpanel.mixpanelapi.MessageBuilder;
import com.mixpanel.mixpanelapi.ClientDelivery;
import com.mixpanel.mixpanelapi.MixpanelAPI;
import org.json.JSONObject;

MessageBuilder messageBuilder = new MessageBuilder("YOUR_PROJECT_TOKEN");

Track Events

You must provide a distinct_id for all events.
MessageBuilder messageBuilder = new MessageBuilder("YOUR_PROJECT_TOKEN");
MixpanelAPI mixpanel = new MixpanelAPI();

// Create event with properties
JSONObject props = new JSONObject();
props.put("Product", "Premium Subscription");
props.put("Amount", 49.99);

JSONObject event = messageBuilder.event("12345", "Purchase", props);

// Bundle and send
ClientDelivery delivery = new ClientDelivery();
delivery.addMessage(event);
mixpanel.deliver(delivery);

Multiple Events

ClientDelivery delivery = new ClientDelivery();

JSONObject event1 = messageBuilder.event("12345", "Page View", null);
JSONObject event2 = messageBuilder.event("12345", "Button Click", 
  new JSONObject().put("button", "Sign Up"));

delivery.addMessage(event1);
delivery.addMessage(event2);

mixpanel.deliver(delivery);

User Profiles

MessageBuilder messageBuilder = new MessageBuilder("YOUR_PROJECT_TOKEN");
MixpanelAPI mixpanel = new MixpanelAPI();

JSONObject props = new JSONObject();
props.put("Plan", "Premium");
props.put("$email", "[email protected]");
props.put("$ip", "0"); // Disable geolocation

JSONObject message = messageBuilder.set("12345", props);
mixpanel.sendMessage(message);
JSONObject props = new JSONObject();
props.put("First Purchase", "2024-01-01");
props.put("$ip", "0");

JSONObject message = messageBuilder.setOnce("12345", props);
mixpanel.sendMessage(message);

Group Analytics

Send Events

JSONObject props = new JSONObject();
props.put("company", "Acme Inc");
props.put("feature", "Reports");

JSONObject event = messageBuilder.event("12345", "Feature Used", props);
mixpanel.sendMessage(event);

Set Group Properties

JSONObject groupProps = new JSONObject();
groupProps.put("name", "Acme Inc");
groupProps.put("industry", "Technology");
groupProps.put("employees", 500);

JSONObject message = messageBuilder.groupSet("company", "Acme Inc", groupProps);
mixpanel.sendMessage(message);
JSONObject props = new JSONObject();
props.put("founded", "2010-01-01");

JSONObject message = messageBuilder.groupSetOnce(
  "company", "Acme Inc", props
);
mixpanel.sendMessage(message);

Privacy Controls

EU Data Residency

MixpanelAPI mixpanel = new MixpanelAPI(
  "https://api-eu.mixpanel.com/track",
  "https://api-eu.mixpanel.com/engage",
  "https://api-eu.mixpanel.com/groups"
);

India Data Residency

MixpanelAPI mixpanel = new MixpanelAPI(
  "https://api-in.mixpanel.com/track",
  "https://api-in.mixpanel.com/engage",
  "https://api-in.mixpanel.com/groups"
);

Disable Geolocation

// For events
JSONObject props = new JSONObject();
props.put("ip", "0");
JSONObject event = messageBuilder.event("12345", "event", props);

// For profiles
JSONObject userProps = new JSONObject();
userProps.put("$ip", "0");
JSONObject message = messageBuilder.set("12345", userProps);

Platform Considerations

All server-side calls originate from your server’s IP. Set ip to 0 to disable geolocation.
  • You manage distinct_id yourself
  • No automatic identity management
  • Message-based architecture (build, bundle, send)
  • Use /import endpoint for historical data
  • Designed for high-throughput server environments

Resources

Build docs developers (and LLMs) love