From 4c85d39a7bc7cd699b9e2e4d68632cbfb6b69bc4 Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Tue, 18 Feb 2025 16:30:10 +0000 Subject: [PATCH] feature: add set methods for person properties --- .../java/net/hollowcube/posthog/PostHog.java | 46 +++++++++++++++ .../net/hollowcube/posthog/PostHogClient.java | 56 +++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/src/main/java/net/hollowcube/posthog/PostHog.java b/src/main/java/net/hollowcube/posthog/PostHog.java index 26ffc25..dd1e5d7 100644 --- a/src/main/java/net/hollowcube/posthog/PostHog.java +++ b/src/main/java/net/hollowcube/posthog/PostHog.java @@ -119,6 +119,52 @@ public static void identify(@NotNull String distinctId, @Nullable Object propert getClient().identify(distinctId, properties); } + /** + * Set the given properties with the person profile of the user (distinct id). + * + * @param distinctId Unique ID of the target in your database. May not be empty. + * @param properties Properties to set (including overwriting previous values) on the person profile + * @param propertiesSetOnce Properties to set only if missing on the person profile + */ + public static void set(@NotNull String distinctId, @Nullable Map properties, @Nullable Map propertiesSetOnce) { + getClient().set(distinctId, properties, propertiesSetOnce); + } + + /** + * Set the given properties with the person profile of the user (distinct id). + * + *

The objects must be serializable to a JSON object via Gson (not primitive or array)

+ * + * @param distinctId Unique ID of the target in your database. May not be empty. + * @param properties Properties to set (including overwriting previous values) on the person profile + * @param propertiesSetOnce Properties to set only if missing on the person profile + */ + public static void set(@NotNull String distinctId, @Nullable Object properties, @Nullable Object propertiesSetOnce) { + getClient().set(distinctId, properties, propertiesSetOnce); + } + + /** + * Set the given properties with the person profile of the user (distinct id). + * + * @param distinctId Unique ID of the target in your database. May not be empty. + * @param properties Properties to set (including overwriting previous values) on the person profile + */ + public static void set(@NotNull String distinctId, @Nullable Map properties) { + getClient().set(distinctId, properties); + } + + /** + * Set the given properties with the person profile of the user (distinct id). + * + *

The object must be serializable to a JSON object via Gson (not primitive or array)

+ * + * @param distinctId Unique ID of the target in your database. May not be empty. + * @param properties Properties to set (including overwriting previous values) on the person profile + */ + public static void set(@NotNull String distinctId, @Nullable Object properties) { + getClient().set(distinctId, properties); + } + /** * Alias the given distinct ID to the given alias. * diff --git a/src/main/java/net/hollowcube/posthog/PostHogClient.java b/src/main/java/net/hollowcube/posthog/PostHogClient.java index 1857be3..9763d16 100644 --- a/src/main/java/net/hollowcube/posthog/PostHogClient.java +++ b/src/main/java/net/hollowcube/posthog/PostHogClient.java @@ -130,6 +130,62 @@ default void identify(@NotNull String distinctId, @Nullable Object properties) { capture(distinctId, IDENTIFY, eventProps); } + /** + * Set the given properties with the person profile of the user (distinct id). + * + * @param distinctId Unique ID of the target in your database. May not be empty. + * @param properties Properties to set (including overwriting previous values) on the person profile + * @param propertiesSetOnce Properties to set only if missing on the person profile + */ + default void set(@NotNull String distinctId, @Nullable Map properties, @Nullable Map propertiesSetOnce) { + Map eventProps = new HashMap<>(); + if (properties != null) eventProps.put(SET, properties); + if (propertiesSetOnce != null) eventProps.put(SET_ONCE, propertiesSetOnce); + capture(distinctId, SET, eventProps); + } + + /** + * Set the given properties with the person profile of the user (distinct id). + * + *

The objects must be serializable to a JSON object via Gson (not primitive or array)

+ * + * @param distinctId Unique ID of the target in your database. May not be empty. + * @param properties Properties to set (including overwriting previous values) on the person profile + * @param propertiesSetOnce Properties to set only if missing on the person profile + */ + default void set(@NotNull String distinctId, @Nullable Object properties, @Nullable Object propertiesSetOnce) { + Map eventProps = new HashMap<>(); + if (properties != null) eventProps.put(SET, properties); + if (propertiesSetOnce != null) eventProps.put(SET_ONCE, propertiesSetOnce); + capture(distinctId, SET, eventProps); + } + + /** + * Set the given properties with the person profile of the user (distinct id). + * + * @param distinctId Unique ID of the target in your database. May not be empty. + * @param properties Properties to set (including overwriting previous values) on the person profile + */ + default void set(@NotNull String distinctId, @Nullable Map properties) { + Map eventProps = new HashMap<>(); + if (properties != null) eventProps.put(SET, properties); + capture(distinctId, SET, eventProps); + } + + /** + * Set the given properties with the person profile of the user (distinct id). + * + *

The object must be serializable to a JSON object via Gson (not primitive or array)

+ * + * @param distinctId Unique ID of the target in your database. May not be empty. + * @param properties Properties to set (including overwriting previous values) on the person profile + */ + default void set(@NotNull String distinctId, @Nullable Object properties) { + Map eventProps = new HashMap<>(); + if (properties != null) eventProps.put(SET, properties); + capture(distinctId, SET, eventProps); + } + /** * Alias the given distinct ID to the given alias. *