Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,19 @@ EventCollection events = Event.list(params);
while (events.hasNext()) {
System.out.println(events.next().getEventName());
}

// List event summaries of a user
Map<String, String> params = Maps.newHashMap();
params.put("type", "user");
params.put("user_id", "1");
// Alternatively list by Intercom ID
// params.put("intercom_user_id", "541a144b201ebf2ec5000001");
// Or by email
// params.put("email", "river@serenity.io");
EventSummaryCollection eventSummaryCollection = Event.listSummary(params);
for(EventSummary eventSummary : eventSummaryCollection.getEventSummaries()){
System.out.println(eventSummary);
}
```

### Tags
Expand Down
11 changes: 8 additions & 3 deletions intercom-java/src/main/java/io/intercom/api/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -60,6 +57,14 @@ public static EventCollection list(Map<String, String> params) throws InvalidExc
return DataResource.list(params, "events", EventCollection.class);
}

public static EventSummaryCollection listSummary(Map<String, String> params) throws InvalidException, AuthorizationException {
if ((!params.containsKey("email")) && (!params.containsKey("user_id")) && (!params.containsKey("intercom_user_id"))) {
throw new InvalidException("an event query must include an email, user_id or intercom_user_id parameter");
}
params.put("summary", "true");
return DataResource.list(params, "events", EventSummaryCollection.class);
}

@VisibleForTesting
static List<JobItem<Event>> validateJobItems(List<JobItem<Event>> items) {
final JobSupport jobSupport = new JobSupport();
Expand Down
101 changes: 101 additions & 0 deletions intercom-java/src/main/java/io/intercom/api/EventSummary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package io.intercom.api;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@SuppressWarnings("UnusedDeclaration")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class EventSummary {

@JsonProperty("name")
private String name;

@JsonProperty("first")
private String first;

@JsonProperty("last")
private String last;

@JsonProperty("count")
private int count;

@JsonProperty("description")
private String description;

public EventSummary() {
}

public String getName() {
return name;
}

public String getFirstOccurredAtString() {
return first;
}

public Date getFirstOccurredAt() throws ParseException {
return stringToDate(first);
}

public String getLastOccurredAtString() {
return last;
}

public Date getLastOccurredAt() throws ParseException {
return stringToDate(last);
}

public int getCount() {
return count;
}

public String getDescription() {
return description;
}

private Date stringToDate(String dateString) throws ParseException {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").parse(dateString);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

EventSummary eventSummary = (EventSummary) o;

if (name != null ? !name.equals(eventSummary.name) : eventSummary.name != null) return false;
if (first != null ? !first.equals(eventSummary.first) : eventSummary.first != null) return false;
if (last != null ? !last.equals(eventSummary.last) : eventSummary.last != null) return false;
if (count != eventSummary.count) return false;
if (description != null ? !description.equals(eventSummary.description) : eventSummary.description != null) return false;

return true;
}

@Override
public int hashCode() {
int result = (name!= null ? name.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (first != null ? first.hashCode() : 0);
result = 31 * result + (last != null ? last.hashCode() : 0);
result = 31 * result + count;
return result;
}
@Override
public String toString() {
return "EventSummaryCollection{" +
"name='" + name+ '\'' +
", count=" + count +
", description='" + description + '\'' +
", first='" + first + '\'' +
", last='" + last + '\'' +
"} " + super.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.intercom.api;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

@SuppressWarnings("UnusedDeclaration")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class EventSummaryCollection extends TypedData {

@JsonProperty("type")
private final String type = "event.summary";

@JsonProperty("email")
private String email;

@JsonProperty("user_id")
private String userID;

@JsonProperty("intercom_user_id")
private String intercomUserID;

@JsonProperty("events")
private List<EventSummary> eventSummaries;

public EventSummaryCollection() {
}

public String getType() {
return type;
}

public String getEmail() {
return email;
}

public String getUserID() {
return userID;
}

public String getIntercomUserID() {
return intercomUserID ;
}

public List<EventSummary> getEventSummaries() {
return eventSummaries;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

EventSummaryCollection eventSummaryCollection = (EventSummaryCollection) o;

if (email != null ? !email.equals(eventSummaryCollection.email) : eventSummaryCollection.email != null) return false;
if (userID != null ? !userID.equals(eventSummaryCollection.userID) : eventSummaryCollection.userID != null) return false;
if (intercomUserID!= null ? !intercomUserID.equals(eventSummaryCollection.intercomUserID) : eventSummaryCollection.intercomUserID != null) return false;
if (eventSummaries!= null ? !eventSummaries.equals(eventSummaryCollection.eventSummaries) : eventSummaryCollection.eventSummaries != null) return false;
if (!type.equals(eventSummaryCollection.type)) return false;

return true;
}

@Override
public int hashCode() {
int result = type.hashCode();
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (userID != null ? userID.hashCode() : 0);
result = 31 * result + (intercomUserID != null ? intercomUserID.hashCode() : 0);
result = 31 * result + (eventSummaries != null ? eventSummaries.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "Event{" +
"type='" + type + '\'' +
", email='" + email + '\'' +
", userID='" + userID + '\'' +
", intercomUserID='" + intercomUserID + '\'' +
", eventSummaries=" + eventSummaries +
"} " + super.toString();
}
}
26 changes: 26 additions & 0 deletions intercom-java/src/test/java/io/intercom/api/EventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.List;

import static io.intercom.api.TestSupport.load;
import static org.junit.Assert.*;

Expand Down Expand Up @@ -214,5 +216,29 @@ public void testListing() throws Exception {
assertEquals("ADDAFRIEND", eventWithMetadata.getMetadata().get("invite_code"));
assertEquals(null, eventWithMetadata.getMetadata().get("non_existing_key"));
}

@Test
public void testEventSummaryParsing() throws Exception {
String json = load("event_summary.json");
final EventSummaryCollection eventSummaryCollection = mapper.readValue(json, EventSummaryCollection.class);
assertEquals("event.summary", eventSummaryCollection.getType());
assertEquals("wash@serenity.io", eventSummaryCollection.getEmail());
assertEquals("530370b477ad7120001d", eventSummaryCollection.getIntercomUserID());
assertEquals("25",eventSummaryCollection.getUserID());
List<EventSummary> eventSummaries = eventSummaryCollection.getEventSummaries();
assertEquals(3, eventSummaries.size());

assertEquals("test-event", eventSummaries.get(0).getName());
assertEquals("2016-12-22T03:54:57.000Z", eventSummaries.get(0).getFirstOccurredAtString());
assertEquals("2018-10-10T06:51:02.000Z", eventSummaries.get(0).getLastOccurredAtString());
assertEquals(8, eventSummaries.get(0).getCount());
assertEquals(null, eventSummaries.get(0).getDescription());

assertEquals("clicked-button", eventSummaries.get(1).getName());
assertEquals("2018-02-20T06:40:16.000Z", eventSummaries.get(1).getFirstOccurredAtString());
assertEquals("2018-02-20T06:40:16.000Z", eventSummaries.get(1).getLastOccurredAtString());
assertEquals(1, eventSummaries.get(1).getCount());
assertEquals("Clicking home page button", eventSummaries.get(1).getDescription());
}
}

29 changes: 29 additions & 0 deletions intercom-java/src/test/resources/event_summary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "event.summary",
"email": "wash@serenity.io",
"intercom_user_id": "530370b477ad7120001d",
"user_id": "25",
"events": [
{
"name": "test-event",
"first": "2016-12-22T03:54:57.000Z",
"last": "2018-10-10T06:51:02.000Z",
"count": 8,
"description": null
},
{
"name": "clicked-button",
"first": "2018-02-20T06:40:16.000Z",
"last": "2018-02-20T06:40:16.000Z",
"count": 1,
"description": "Clicking home page button"
},
{
"name": "completed purchase",
"first": "2018-03-13T02:52:53.000Z",
"last": "2018-03-13T02:58:31.000Z",
"count": 4,
"description": null
}
]
}