Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Commit

Permalink
fix: fix errors in Java SDK group examples, make Java examples more c…
Browse files Browse the repository at this point in the history
…oncise (#965)
  • Loading branch information
justin-fiedler authored Sep 7, 2023
1 parent cdce90c commit 69581c3
Showing 1 changed file with 30 additions and 53 deletions.
83 changes: 30 additions & 53 deletions docs/data/sdks/java/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,11 @@ client.logEvent(new Event("Button Clicked", "test_user_id"));
Events can also contain properties. They provide context about the event taken. For example, "hover time" may be a relevant event property to "button click."

```java
Event event = new Event("Button Clicked", "test_user_id");

JSONObject eventProps = new JSONObject();
try {
eventProps.put("Hover Time", 10).put("prop_2", "value_2");
} catch (JSONException e) {
System.err.println("Invalid JSON");
e.printStackTrace();
}
JSONObject eventProps = new JSONObject()
.put("Hover Time", 10)
.put("prop_2", "value_2");

Event event = new Event("Button Clicked", userId);
event.eventProperties = eventProps;

client.logEvent(event);
Expand All @@ -200,55 +195,41 @@ client.logEvent(event);

--8<-- "includes/groups-intro-paragraph.md"

Code [examples of group functionality can also be found in our demo application](https://github.com/amplitude/Amplitude-Java/blob/main/src/demo/java/com/demo/amplitude/LocalUploadDemo.java#L44-L71).

!!! example
If Joe is in 'orgId' '10', then the `groupName` would be '10':

```java
Event event = new Event("$identify", "test_user_id");

JSONObject groups = new JSONObject();
try {
groups.put("orgId", 10);
} catch (JSONException e) {
e.printStackTrace();
System.err.println("Invalid JSON");
}
groups.put("orgId", 10);

event.groups = groups;
event.userProperties = groups
client.logEvent(event);
Event setGroupEvent = new Event("$identify", userId);
setGroupEvent.groups = groups;
setGroupEvent.userProperties = groups;
client.logEvent(setGroupEvent);
```

If Joe is in 'sport' 'tennis' and 'soccer', then the `groupName` would be '["tennis", "soccer"]'.

```java
Event event = new Event("$identify", "test_user_id");

JSONObject groups = new JSONObject();
try {
groups.put("sport", new String[] {"tennis", "soccer"});
} catch (JSONException e) {
e.printStackTrace();
System.err.println("Invalid JSON");
}
groups.put("sport", new String[] {"tennis", "soccer"});

Event setGroupsEvent = new Event("$identify", userId);
setGroupsEvent.groups = groupProps;
setGroupsEvent.userProperties = groups;

event.groups = groupProps;
event.userProperties = groups
client.logEvent(event);
client.logEvent(setGroupsEvent);
```

You can also use `logEvent` to set **event-level groups**. With event-level groups, the group designation applies only to the specific event being logged, and doesn't persist on the user.

```java
Event event = New Event('event type', 'test_user_id');
JSONObject groups = new JSONObject();
groups.put("orgId", 10);

JsonObject groups = new JSONObject();
try {
groups.put("orgId", 10);
} catch (JSONException e) {
e.printStackTrace();
System.err.println("Invalid JSON");
}
Event event = new Event('event type', userId);
event.groups = groups;

client.logEvent(event);
Expand All @@ -259,20 +240,16 @@ After setting groups, you can then set or update the properties of particular gr
--8<-- "includes/editions-growth-enterprise-with-accounts.md"

```java
Event event = new Event("$groupidentify");

JsonObject groups = new JSONObject();
JSONObject groupProps = new JSONObject();

try {
groups.put("org", "engineering");
groupProps.put("technology", "java");
} catch (JSONException e) {
e.printStackTrace();
System.err.println("Invalid JSON");
}
event.groups = groups
event.groupProperties = groupProps
JSONObject groups = new JSONObject()
.put("org", "engineering")
.put("department", "sdk");
JSONObject groupProps = new JSONObject()
.put("technology", "java")
.put("location", "sf");

Event event = new Event("$groupidentify", userId);
event.groups = groups;
event.groupProperties = groupProps;

client.logEvent(event);
```
Expand Down

0 comments on commit 69581c3

Please sign in to comment.