Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow timestamp to be set on capture #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 34 additions & 11 deletions posthog/src/main/java/com/posthog/java/PostHog.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private void startQueueManager() {
// TODO handle interrupts? (via addShutdownHook)
}

private void enqueue(String distinctId, String event, Map<String, Object> properties) {
JSONObject eventJson = getEventJson(event, distinctId, properties);
private void enqueue(String distinctId, String event, Map<String, Object> properties, Instant timestamp) {
JSONObject eventJson = getEventJson(event, distinctId, properties, timestamp);
queueManager.add(eventJson);
}

Expand All @@ -89,7 +89,19 @@ private void enqueue(String distinctId, String event, Map<String, Object> proper
* @param properties an array with any event properties you'd like to set.
*/
public void capture(String distinctId, String event, Map<String, Object> properties) {
enqueue(distinctId, event, properties);
enqueue(distinctId, event, properties, Instant.now());
}

/**
*
* @param distinctId which uniquely identifies your user in your database. Must
* not be null or empty.
* @param event name of the event. Must not be null or empty.
* @param properties an array with any event properties you'd like to set.
* @param timestamp the timestamp of the event.
*/
public void capture(String distinctId, String event, Map<String, Object> properties, Instant timestamp) {
enqueue(distinctId, event, properties, timestamp);
}

/**
Expand All @@ -99,11 +111,22 @@ public void capture(String distinctId, String event, Map<String, Object> propert
* @param event name of the event. Must not be null or empty.
*/
public void capture(String distinctId, String event) {
enqueue(distinctId, event, null);
enqueue(distinctId, event, null, Instant.now());
}

/**
*
*
* @param distinctId which uniquely identifies your user in your database. Must
* not be null or empty.
* @param event name of the event. Must not be null or empty.
* @param timestamp the timestamp of the event.
*/
public void capture(String distinctId, String event, Instant timestamp) {
enqueue(distinctId, event, null, timestamp);
}

/**
*
* @param distinctId which uniquely identifies your user in your
* database. Must not be null or empty.
* @param properties an array with any person properties you'd like to
Expand All @@ -119,7 +142,7 @@ public void identify(String distinctId, Map<String, Object> properties, Map<Stri
if (propertiesSetOnce != null) {
props.put("$set_once", propertiesSetOnce);
}
enqueue(distinctId, "$identify", props);
enqueue(distinctId, "$identify", props, Instant.now());
}

/**
Expand Down Expand Up @@ -148,7 +171,7 @@ public void alias(String distinctId, String alias) {
put("alias", alias);
}
};
enqueue(distinctId, "$create_alias", props);
enqueue(distinctId, "$create_alias", props, Instant.now());
}

/**
Expand All @@ -163,7 +186,7 @@ public void set(String distinctId, Map<String, Object> properties) {
put("$set", properties);
}
};
enqueue(distinctId, "$set", props);
enqueue(distinctId, "$set", props, Instant.now());
}

/**
Expand All @@ -179,16 +202,16 @@ public void setOnce(String distinctId, Map<String, Object> properties) {
put("$set_once", properties);
}
};
enqueue(distinctId, "$set_once", props);
enqueue(distinctId, "$set_once", props, Instant.now());
}

private JSONObject getEventJson(String event, String distinctId, Map<String, Object> properties) {
private JSONObject getEventJson(String event, String distinctId, Map<String, Object> properties, Instant timestamp) {
JSONObject eventJson = new JSONObject();
try {
// Ensure that we generate an identifier for this event such that we can e.g.
// deduplicate server-side any duplicates we may receive.
eventJson.put("uuid", UUID.randomUUID().toString());
eventJson.put("timestamp", Instant.now().toString());
eventJson.put("timestamp", timestamp.toString());
eventJson.put("distinct_id", distinctId);
eventJson.put("event", event);
eventJson.put("$lib", "posthog-java");
Expand Down
35 changes: 35 additions & 0 deletions posthog/src/test/java/com/posthog/java/PostHogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public void testCaptureSimple() {
+ "\"}").isEqualTo(new JSONObject(json, "distinct_id", "event", "timestamp").toString());
}

@Test
public void testCaptureSimpleWithTimestamp() {
String providedInstantExpected = "2024-04-25T02:02:02Z";
Instant timestamp = Instant.parse(providedInstantExpected);
ph.capture("test id", "test event", timestamp);
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
// Assert JSON includes the expected distinct_id, event, and timestamp, ignoring
// any extraneus properties.
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"test event\",\"timestamp\":\"" + providedInstantExpected
+ "\"}").isEqualTo(new JSONObject(json, "distinct_id", "event", "timestamp").toString());
}

@Test
public void testEnsureEventHasGeneratedUuid() {
// To ensure we have a way to deduplicate events that may be ingested multiple
Expand Down Expand Up @@ -100,6 +115,26 @@ public void testCaptureWithProperties() {
+ "\"}").isEqualTo(new JSONObject(json, "distinct_id", "event", "properties", "timestamp").toString());
}

@Test
public void testCaptureWithPropertiesAndTimestamp() {
String providedInstantExpected = "2024-04-25T02:02:02Z";
Instant timestamp = Instant.parse(providedInstantExpected);
ph.capture("test id", "test event", new HashMap<String, Object>() {
{
put("movie_id", 123);
put("category", "romcom");
}
},
timestamp);
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"test event\""
+ ",\"properties\":{\"movie_id\":123,\"category\":\"romcom\"},\"timestamp\":\"" + providedInstantExpected
+ "\"}").isEqualTo(new JSONObject(json, "distinct_id", "event", "properties", "timestamp").toString());
}

@Test
public void testCaptureIncludesLibProperty() {
ph.capture("test id", "test event", new HashMap<String, Object>() {
Expand Down