Skip to content

Commit c90da2d

Browse files
authored
feat: Introduce MixpanelApi Builder pattern and expose JsonSerializer option (#51)
* Simplify MixpanelApi construction through builder pattern * Add JsonSerializer option on MixpanelApi builder
1 parent 31f6e25 commit c90da2d

File tree

14 files changed

+929
-549
lines changed

14 files changed

+929
-549
lines changed

.github/workflows/release.yml

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ jobs:
7171
echo "VERSION=$VERSION" >> $GITHUB_ENV
7272
echo "version=$VERSION" >> $GITHUB_OUTPUT
7373
mvn versions:set -DnewVersion=$VERSION -DgenerateBackupPoms=false
74+
cd mixpanel-java-extension-jackson
75+
mvn versions:set -DnewVersion=$VERSION -DgenerateBackupPoms=false
76+
cd ..
7477
7578
- name: Set version from input
7679
id: set-version-input
@@ -80,15 +83,27 @@ jobs:
8083
echo "VERSION=$VERSION" >> $GITHUB_ENV
8184
echo "version=$VERSION" >> $GITHUB_OUTPUT
8285
mvn versions:set -DnewVersion=$VERSION -DgenerateBackupPoms=false
86+
cd mixpanel-java-extension-jackson
87+
mvn versions:set -DnewVersion=$VERSION -DgenerateBackupPoms=false
88+
cd ..
8389
84-
- name: Run tests
90+
- name: Run tests - Main SDK
8591
run: mvn clean test
8692

93+
- name: Run tests - Jackson Extension
94+
run: |
95+
cd mixpanel-java-extension-jackson
96+
mvn clean test
97+
cd ..
98+
8799
- name: Deploy to Maven Central
88100
env:
89101
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
90102
run: |
91103
mvn clean deploy -Dgpg.passphrase=$GPG_PASSPHRASE
104+
cd mixpanel-java-extension-jackson
105+
mvn clean deploy -Dgpg.passphrase=$GPG_PASSPHRASE
106+
cd ..
92107
93108
- name: Create GitHub Release
94109
if: startsWith(github.ref, 'refs/tags/')
@@ -98,7 +113,7 @@ jobs:
98113
script: |
99114
const releaseBody = `## Mixpanel Java SDK v${process.env.VERSION}
100115
101-
### Maven
116+
### Maven - Main SDK
102117
\`\`\`xml
103118
<dependency>
104119
<groupId>com.mixpanel</groupId>
@@ -107,11 +122,21 @@ jobs:
107122
</dependency>
108123
\`\`\`
109124
125+
### Maven - Jackson Extension (Optional)
126+
\`\`\`xml
127+
<dependency>
128+
<groupId>com.mixpanel</groupId>
129+
<artifactId>mixpanel-java-extension-jackson</artifactId>
130+
<version>${process.env.VERSION}</version>
131+
</dependency>
132+
\`\`\`
133+
110134
### Changes
111135
See [CHANGELOG](https://github.com/mixpanel/mixpanel-java/blob/master/CHANGELOG.md) for details.
112136
113137
### Links
114-
- [Maven Central](https://central.sonatype.com/artifact/com.mixpanel/mixpanel-java/${process.env.VERSION})
138+
- [Maven Central - Main SDK](https://central.sonatype.com/artifact/com.mixpanel/mixpanel-java/${process.env.VERSION})
139+
- [Maven Central - Jackson Extension](https://central.sonatype.com/artifact/com.mixpanel/mixpanel-java-extension-jackson/${process.env.VERSION})
115140
- [JavaDoc](http://mixpanel.github.io/mixpanel-java/)`;
116141
117142
await github.rest.repos.createRelease({
@@ -133,12 +158,24 @@ jobs:
133158
- name: Wait for Maven Central sync
134159
run: sleep 300 # Wait 5 minutes for synchronization
135160

136-
- name: Verify artifact on Maven Central
161+
- name: Verify artifacts on Maven Central
137162
run: |
138163
VERSION=${{ needs.release.outputs.version }}
164+
165+
# Verify main SDK
139166
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" https://repo1.maven.org/maven2/com/mixpanel/mixpanel-java/${VERSION}/mixpanel-java-${VERSION}.jar)
140167
if [ $RESPONSE -eq 200 ]; then
141-
echo "✅ Artifact successfully published to Maven Central"
168+
echo "✅ Main SDK successfully published to Maven Central"
169+
else
170+
echo "⚠️ Main SDK not yet available on Maven Central (HTTP $RESPONSE)"
171+
fi
172+
173+
# Verify Jackson extension
174+
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" https://repo1.maven.org/maven2/com/mixpanel/mixpanel-java-extension-jackson/${VERSION}/mixpanel-java-extension-jackson-${VERSION}.jar)
175+
if [ $RESPONSE -eq 200 ]; then
176+
echo "✅ Jackson extension successfully published to Maven Central"
142177
else
143-
echo "⚠️ Artifact not yet available on Maven Central (HTTP $RESPONSE). This is normal - it may take up to 30 minutes to appear."
144-
fi
178+
echo "⚠️ Jackson extension not yet available on Maven Central (HTTP $RESPONSE)"
179+
fi
180+
181+
echo "Note: Artifacts may take up to 30 minutes to appear on Maven Central"

README.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ This is the official Mixpanel tracking library for Java.
22

33
## Latest Version
44

5-
##### _May 08, 2024_ - [v1.5.3](https://github.com/mixpanel/mixpanel-java/releases/tag/mixpanel-java-1.5.3)
5+
See the [releases page](https://github.com/mixpanel/mixpanel-java/releases) for the latest version.
66

7-
```
7+
```xml
88
<dependency>
99
<groupId>com.mixpanel</groupId>
1010
<artifactId>mixpanel-java</artifactId>
11-
<version>1.5.3</version>
11+
<version>1.6.1</version>
1212
</dependency>
1313
```
1414

@@ -33,9 +33,13 @@ are built by `MessageBuilder` objects, and those messages can be consumed by the
3333

3434
### Gzip Compression
3535

36-
The library supports gzip compression for both tracking events (`/track`) and importing historical events (`/import`). To enable gzip compression, pass `true` to the `MixpanelAPI` constructor:
36+
The library supports gzip compression for both tracking events (`/track`) and importing historical events (`/import`). To enable gzip compression, use the builder:
3737

38-
MixpanelAPI mixpanel = new MixpanelAPI(true); // Enable gzip compression
38+
```java
39+
MixpanelAPI mixpanel = new MixpanelAPI.Builder()
40+
.useGzipCompression(true)
41+
.build();
42+
```
3943

4044
Gzip compression can reduce bandwidth usage and improve performance, especially when sending large batches of events.
4145

@@ -45,32 +49,37 @@ The library supports importing historical events (events older than 5 days that
4549

4650
### High-Performance JSON Serialization (Optional)
4751

48-
For applications that import large batches of events (e.g., using the `/import` endpoint), the library supports optional high-performance JSON serialization using Jackson. When Jackson is available on the classpath, the library automatically uses it for JSON serialization, providing **up to 5x performance improvement** for large batches.
52+
For applications that import large batches of events (e.g., using the `/import` endpoint), the library supports optional high-performance JSON serialization using Jackson. The Jackson extension provides **up to 5x performance improvement** for large batches.
4953

50-
To enable high-performance serialization, add the Jackson dependency to your project:
54+
To enable high-performance serialization, add the Jackson extension to your project:
5155

5256
```xml
5357
<dependency>
54-
<groupId>com.fasterxml.jackson.core</groupId>
55-
<artifactId>jackson-databind</artifactId>
56-
<version>2.20.0</version>
58+
<groupId>com.mixpanel</groupId>
59+
<artifactId>mixpanel-java-extension-jackson</artifactId>
60+
<version>1.6.1</version>
5761
</dependency>
5862
```
5963

64+
Then configure the MixpanelAPI to use it:
65+
66+
```java
67+
import com.mixpanel.mixpanelapi.internal.JacksonSerializer;
68+
69+
MixpanelAPI mixpanel = new MixpanelAPI.Builder()
70+
.jsonSerializer(new JacksonSerializer())
71+
.build();
72+
```
73+
6074
**Key benefits:**
61-
- **Automatic detection**: The library automatically detects and uses Jackson when available
62-
- **Backward compatible**: No code changes required - all public APIs remain unchanged
6375
- **Significant performance gains**: 2-5x faster serialization for batches of 50+ messages
6476
- **Optimal for `/import`**: Most beneficial when importing large batches (up to 2000 events)
65-
- **Fallback support**: Gracefully falls back to org.json if Jackson is not available
6677

6778
The performance improvement is most noticeable when:
6879
- Importing historical data via the `/import` endpoint
6980
- Sending batches of 50+ events
7081
- Processing high-volume event streams
7182

72-
No code changes are required to benefit from this optimization - simply add the Jackson dependency to your project.
73-
7483
## Feature Flags
7584

7685
The Mixpanel Java SDK supports feature flags with both local and remote evaluation modes.
@@ -90,7 +99,9 @@ LocalFlagsConfig config = LocalFlagsConfig.builder()
9099
.pollingIntervalSeconds(60)
91100
.build();
92101

93-
MixpanelAPI mixpanel = new MixpanelAPI(config);
102+
MixpanelAPI mixpanel = new MixpanelAPI.Builder()
103+
.flagsConfig(config)
104+
.build();
94105

95106
// Start polling for flag definitions
96107
mixpanel.getLocalFlags().startPollingForDefinitions();
@@ -127,7 +138,7 @@ RemoteFlagsConfig config = RemoteFlagsConfig.builder()
127138
.projectToken("YOUR_PROJECT_TOKEN")
128139
.build();
129140

130-
try (MixpanelAPI mixpanel = new MixpanelAPI(config)) {
141+
try (MixpanelAPI mixpanel = new MixpanelAPI.Builder().flagsConfig(config).build()) {
131142
Map<String, Object> context = new HashMap<>();
132143
context.put("distinct_id", "user-456");
133144

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Mixpanel Java SDK - Jackson Extension
2+
3+
High-performance Jackson serializer extension for the Mixpanel Java SDK. This extension provides improved JSON serialization performance for large batch operations.
4+
5+
## Installation
6+
7+
Add this dependency to your project:
8+
9+
### Maven
10+
```xml
11+
<dependency>
12+
<groupId>com.mixpanel</groupId>
13+
<artifactId>mixpanel-java-extension-jackson</artifactId>
14+
<version>1.6.1</version>
15+
</dependency>
16+
```
17+
18+
### Gradle
19+
```gradle
20+
implementation 'com.mixpanel:mixpanel-java-extension-jackson:1.6.1'
21+
```
22+
23+
This extension includes:
24+
- `mixpanel-java` (core SDK)
25+
- `jackson-core` 2.20.0
26+
27+
## Usage
28+
29+
To use the Jackson serializer, pass an instance to the MixpanelAPI builder:
30+
31+
```java
32+
import com.mixpanel.mixpanelapi.MixpanelAPI;
33+
import com.mixpanel.mixpanelapi.internal.JacksonSerializer;
34+
35+
MixpanelAPI mixpanel = new MixpanelAPI.Builder()
36+
.jsonSerializer(new JacksonSerializer())
37+
.build();
38+
```
39+
40+
## Key benefits
41+
- **Significant performance gains**: 2-5x faster serialization for batches of 50+ messages
42+
- **Optimal for `/import`**: Most beneficial when importing large batches (up to 2000 events)
43+
44+
The performance improvement is most noticeable when:
45+
- Importing historical data via the `/import` endpoint
46+
- Sending batches of 50+ events
47+
- Processing high-volume event streams
48+
49+
## License
50+
51+
```
52+
See LICENSE File for details.
53+
```

0 commit comments

Comments
 (0)