SDK
Sessions
Read the live session of any online player: hostname, join time, attributed campaign.
A session is the live state of a player while they're connected. The SDK gives you read-only access to session data, so your plugin can do things like "only do X if the player came from campaign Y".
Getting a session
java
import net.analyse.api.Analyse;import net.analyse.api.session.PlayerSession;Analyse.sessions().getSession(player.getUniqueId()).ifPresent(session -> {String hostname = session.getHostname();Instant joinedAt = session.getJoinTime();// ...});
Every online player has exactly one session. When they leave, the session is removed.
What's on a session
| Method | What it returns |
|---|---|
getPlayerUuid() | The player's Minecraft UUID |
getHostname() | The hostname they joined through. Use this to identify the Campaign. |
getJoinTime() | An Instant for when they joined |
hasActiveSession() | true once the server has confirmed the session with Analyse |
Useful patterns
Reward players based on their Campaign
java
Analyse.sessions().getSession(player.getUniqueId()).ifPresent(session -> {String host = session.getHostname();if (host != null && host.startsWith("pewds.")) {rewardPewdsBonus(player);}});
Track playtime locally
java
long seconds = Duration.between(session.getJoinTime(),Instant.now()).getSeconds();
Only fire custom events once the session is ready
java
if (session.hasActiveSession()) {Analyse.trackEvent("custom_thing").withPlayer(session.getPlayerUuid(), player.getName()).send();}
All sessions
java
Collection<PlayerSession> online = Analyse.sessions().getAllSessions();getLogger().info("Currently " + online.size() + " tracked sessions.");
Handy for dashboards, commands, and health checks.
What's not here
- You can't modify a session. It's read-only from the SDK.
- You can't see past sessions for a player. Use the Analyse dashboard or API for history.
Related
- Sessions feature
- Custom events to enrich sessions with your data