|
| 1 | +# Copilot Instructions for mixpanel-java |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +This is the official Mixpanel tracking library for Java - a production-ready library for sending analytics events and user profile updates to Mixpanel from server-side Java applications. |
| 6 | + |
| 7 | +**Project Type:** Java library (JAR) |
| 8 | +**Build Tool:** Maven 3.x |
| 9 | +**Java Version:** 8+ (compiled for Java 8, tested on 8, 11, 17, 21) |
| 10 | +**Main Dependency:** org.json:json:20231013 |
| 11 | +**Test Framework:** JUnit 4.13.2 |
| 12 | + |
| 13 | +## Build Commands |
| 14 | + |
| 15 | +Always run these commands from the repository root directory. |
| 16 | + |
| 17 | +### Essential Commands |
| 18 | + |
| 19 | +```bash |
| 20 | +# Run all tests (81 tests, ~5-20 seconds) |
| 21 | +mvn test |
| 22 | + |
| 23 | +# Build JAR without tests |
| 24 | +mvn clean package -DskipTests |
| 25 | + |
| 26 | +# Full build with tests (~20-30 seconds) |
| 27 | +mvn clean package |
| 28 | + |
| 29 | +# Clean build artifacts |
| 30 | +mvn clean |
| 31 | + |
| 32 | +# Generate JavaDoc |
| 33 | +mvn javadoc:javadoc |
| 34 | +``` |
| 35 | + |
| 36 | +### Running Specific Tests |
| 37 | + |
| 38 | +```bash |
| 39 | +# Run a specific test class |
| 40 | +mvn test -Dtest=MixpanelAPITest |
| 41 | + |
| 42 | +# Run a specific test method |
| 43 | +mvn test -Dtest=MixpanelAPITest#testBuildEventMessage |
| 44 | +``` |
| 45 | + |
| 46 | +## Project Structure |
| 47 | + |
| 48 | +``` |
| 49 | +mixpanel-java/ |
| 50 | +├── pom.xml # Maven build configuration |
| 51 | +├── src/ |
| 52 | +│ ├── main/java/com/mixpanel/mixpanelapi/ |
| 53 | +│ │ ├── MixpanelAPI.java # Main API class, HTTP communication |
| 54 | +│ │ ├── MessageBuilder.java # Constructs JSON messages |
| 55 | +│ │ ├── ClientDelivery.java # Batches messages for transmission |
| 56 | +│ │ ├── Config.java # API endpoints and constants |
| 57 | +│ │ ├── Base64Coder.java # Base64 encoding utility |
| 58 | +│ │ ├── MixpanelMessageException.java # Client-side errors |
| 59 | +│ │ ├── MixpanelServerException.java # Server-side errors |
| 60 | +│ │ ├── featureflags/ # Feature flags implementation |
| 61 | +│ │ │ ├── config/ # Flag configuration classes |
| 62 | +│ │ │ ├── model/ # Flag data models |
| 63 | +│ │ │ ├── provider/ # Flag evaluation providers |
| 64 | +│ │ │ └── util/ # Utility classes |
| 65 | +│ │ └── internal/ # Internal serialization (Jackson/org.json) |
| 66 | +│ ├── main/resources/ |
| 67 | +│ │ └── mixpanel-version.properties # Version info (filtered by Maven) |
| 68 | +│ ├── test/java/com/mixpanel/mixpanelapi/ |
| 69 | +│ │ ├── MixpanelAPITest.java # Main test class (27 tests) |
| 70 | +│ │ ├── featureflags/provider/ # Feature flags tests (~54 tests) |
| 71 | +│ │ └── internal/ # Serializer tests |
| 72 | +│ └── demo/java/com/mixpanel/mixpanelapi/demo/ |
| 73 | +│ └── MixpanelAPIDemo.java # Demo application |
| 74 | +└── .github/workflows/ |
| 75 | + ├── ci.yml # CI pipeline (tests on Java 8, 11, 17, 21) |
| 76 | + └── release.yml # Release to Maven Central |
| 77 | +``` |
| 78 | + |
| 79 | +## CI/CD Pipeline |
| 80 | + |
| 81 | +The CI workflow (`.github/workflows/ci.yml`) runs on PRs and pushes to master: |
| 82 | + |
| 83 | +1. **Tests:** `mvn clean test` (on Java 8, 11, 17, 21) |
| 84 | +2. **Build:** `mvn clean package` |
| 85 | +3. **JavaDoc:** `mvn javadoc:javadoc` |
| 86 | +4. **Dependency check:** `mvn versions:display-dependency-updates` |
| 87 | + |
| 88 | +**Before submitting changes, always run:** |
| 89 | +```bash |
| 90 | +mvn clean test |
| 91 | +``` |
| 92 | + |
| 93 | +## Architecture Notes |
| 94 | + |
| 95 | +### Core Design Pattern |
| 96 | +The library uses a **Producer-Consumer** pattern: |
| 97 | +1. `MessageBuilder` creates JSON messages on application threads |
| 98 | +2. `ClientDelivery` batches messages (max 50 per request, 2000 for imports) |
| 99 | +3. `MixpanelAPI` sends batched messages to Mixpanel servers |
| 100 | + |
| 101 | +### Message Types and Endpoints |
| 102 | +- **Events** (`/track`): User actions via `messageBuilder.event()` |
| 103 | +- **People** (`/engage`): Profile updates via `messageBuilder.set()`, `increment()`, etc. |
| 104 | +- **Groups** (`/groups`): Group profile updates |
| 105 | +- **Import** (`/import`): Historical events via `messageBuilder.importEvent()` |
| 106 | + |
| 107 | +### Key Constants (Config.java) |
| 108 | +- `MAX_MESSAGE_SIZE = 50` (regular batches) |
| 109 | +- `IMPORT_MAX_MESSAGE_SIZE = 2000` (import batches) |
| 110 | +- Connection timeout: 2 seconds, Read timeout: 10 seconds |
| 111 | + |
| 112 | +## Testing Guidelines |
| 113 | + |
| 114 | +Tests use JUnit 4's `TestCase` style. When adding functionality: |
| 115 | + |
| 116 | +1. Add tests in `MixpanelAPITest.java` for core API changes |
| 117 | +2. Add tests in `featureflags/provider/` for flag-related changes |
| 118 | +3. Follow existing test patterns - verify both JSON structure and encoded format |
| 119 | + |
| 120 | +Example test pattern: |
| 121 | +```java |
| 122 | +public void testNewFeature() { |
| 123 | + JSONObject message = mBuilder.newMethod("distinctId", params); |
| 124 | + // Verify message structure |
| 125 | + assertTrue(delivery.isValidMessage(message)); |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +## Common Tasks |
| 130 | + |
| 131 | +### Adding a New Message Type |
| 132 | +1. Add method to `MessageBuilder.java` |
| 133 | +2. Validate required fields in the method |
| 134 | +3. Add tests in `MixpanelAPITest.java` |
| 135 | +4. Update `ClientDelivery.java` if special handling needed |
| 136 | + |
| 137 | +### Modifying Network Behavior |
| 138 | +Network configuration is in `MixpanelAPI.sendData()`. Timeouts are hardcoded but can be made configurable via `Config.java`. |
| 139 | + |
| 140 | +## Dependencies |
| 141 | + |
| 142 | +**Runtime:** |
| 143 | +- `org.json:json:20231013` - JSON manipulation (required) |
| 144 | +- `com.fasterxml.jackson.core:jackson-databind:2.20.0` - High-performance serialization (optional, provided scope) |
| 145 | + |
| 146 | +**Test:** |
| 147 | +- `junit:junit:4.13.2` |
| 148 | + |
| 149 | +## Important Notes |
| 150 | + |
| 151 | +- `MessageBuilder` instances are NOT thread-safe; create one per thread |
| 152 | +- Messages are JSON → Base64 → URL encoded for transmission |
| 153 | +- The library does NOT start background threads; applications manage their own threading |
| 154 | +- JavaDoc warnings during build are expected and do not indicate failures |
| 155 | + |
| 156 | +Trust these instructions. Only search for additional information if commands fail or behavior differs from what is documented here. |
0 commit comments