forked from segmentio/analytics-java
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
103 lines (87 loc) · 3.15 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package sample;
import com.rudderstack.sdk.java.analytics.RudderAnalytics;
import com.rudderstack.sdk.java.analytics.messages.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.OkHttpClient;
public class Main {
public static void main(String... args) throws Exception {
final BlockingFlush blockingFlush = BlockingFlush.create();
// https://rudder.com/rudder-engineering/sources/test-java/debugger
final RudderAnalytics analytics =
RudderAnalytics.builder("write_key")
.endpoint("data_plane_url")
.plugin(blockingFlush.plugin())
.plugin(new LoggingPlugin())
.client(createClient())
.build();
final String userId = System.getProperty("user.name");
final String anonymousId = UUID.randomUUID().toString();
final AtomicInteger count = new AtomicInteger();
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("key1", "value1");
properties.put("key2", "value2");
properties.put("count", count.incrementAndGet());
analytics.enqueue(
TrackMessage.builder("Java Test")
.properties(properties)
.anonymousId(anonymousId)
.userId(userId));
// }
//}
analytics.enqueue(
TrackMessage.builder("Java Test")
.properties(properties)
.anonymousId(anonymousId)
.userId(userId)
);
Map<String, Object> traits = new LinkedHashMap<>();
traits.put("name", "Sample Name");
traits.put("email", "sample@abc.com");
analytics.enqueue(IdentifyMessage.builder()
.userId("f4ca124298")
.traits(traits)
);
analytics.enqueue(PageMessage.builder("Schedule")
.userId("abcfgrg")
.properties(properties)
);
analytics.enqueue(ScreenMessage.builder("Schedule")
.userId("f4ca124298")
.properties(properties)
);
analytics.enqueue(GroupMessage.builder("group123")
.userId("f4ca124298")
.traits(properties
)
);
analytics.enqueue(AliasMessage.builder("previousId")
.userId("newId")
);
analytics.enqueue(
TrackMessage.builder("Java Test 2")
.properties(properties)
.anonymousId(anonymousId)
.userId(userId)
);
analytics.flush();
blockingFlush.block();
analytics.shutdown();
}
/**
* By default, the analytics client uses an HTTP client with sane defaults. However you can
* customize the client to your needs. For instance, this client is configured to automatically
* gzip outgoing requests.
*/
private static OkHttpClient createClient() {
return new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
// .addInterceptor(new GzipRequestInterceptor())
.build();
}
}