Skip to content

Instantly share code, notes, and snippets.

@pdtyreus
Last active February 14, 2016 01:04
Show Gist options
  • Save pdtyreus/3660dfff71711032442d to your computer and use it in GitHub Desktop.
Save pdtyreus/3660dfff71711032442d to your computer and use it in GitHub Desktop.
Java Amplitude Service for sending Amplitude events server side. Uses Jackson for JSON serialization.
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.ws.rs.core.UriBuilder;
public class AmplitudeService {
private final String apiKey;
private final ObjectMapper objectMapper;
public AmplitudeService(String apiKey) {
objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
objectMapper.setSerializationInclusion(Include.NON_NULL);
this.apiKey = apiKey;
}
public void sendEvent(AmplitudeEvent... events) throws IOException {
List<AmplitudeEvent> request = Arrays.asList(events);
URI apiUri = UriBuilder.fromUri("https://api.amplitude.com/httpapi")
.queryParam("api_key", apiKey)
.queryParam("event", URLEncoder.encode(objectMapper.writeValueAsString(request),"UTF-8"))
.build();
HttpURLConnection connection = (HttpURLConnection)apiUri.toURL().openConnection();
StringBuilder response = new StringBuilder();
String inputLine;
try (BufferedReader in = new BufferedReader(
(connection.getResponseCode() == 200)
? new InputStreamReader(connection.getInputStream())
: new InputStreamReader(connection.getErrorStream()) )) {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}
if (!"success".equals(response.toString())) {
throw new IOException("Amplitude Error: " + response.toString());
}
}
public static class AmplitudeEvent {
private String userId;
private String eventType;
private Long time;
private HashMap<String, Object> eventProperties;
private HashMap<String, Object> userProperties;
private Float revenue;
private Float locationLat;
private Float locationLng;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public HashMap<String, Object> getEventProperties() {
return eventProperties;
}
public void setEventProperties(HashMap<String, Object> eventProperties) {
this.eventProperties = eventProperties;
}
public HashMap<String, Object> getUserProperties() {
return userProperties;
}
public void setUserProperties(HashMap<String, Object> userProperties) {
this.userProperties = userProperties;
}
public Float getRevenue() {
return revenue;
}
public void setRevenue(Float revenue) {
this.revenue = revenue;
}
public Float getLocationLat() {
return locationLat;
}
public void setLocationLat(Float locationLat) {
this.locationLat = locationLat;
}
public Float getLocationLng() {
return locationLng;
}
public void setLocationLng(Float locationLng) {
this.locationLng = locationLng;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment