Track Player Events
Player events are the foundation of understanding your server’s activity. This guide shows you how to track the most common player interactions using Hoglin.
-
Set up your event listener class
Section titled “Set up your event listener class”Create a class to handle all your player events:
class PlayerEventListener(private val analytics: Hoglin) : Listener {// Event handlers will go here}public class PlayerEventListener implements Listener {private final Hoglin analytics;public PlayerEventListener(Hoglin analytics) {this.analytics = analytics;}// Event handlers will go here} -
Register your listener
Section titled “Register your listener”Don’t forget to register your event listener in your plugin’s
onEnable()
:override fun onEnable() {val analytics = Hoglin.Builder("<your server key>").autoFlushInterval(5_000L).maxBatchSize(50).build()server.pluginManager.registerEvents(PlayerEventListener(analytics),this)}@Overridepublic void onEnable() {Hoglin analytics = new Hoglin.Builder("<your server key>").autoFlushInterval(5_000L).maxBatchSize(50).build();getServer().getPluginManager().registerEvents(new PlayerEventListener(analytics),this);}
Common Player Events
Section titled “Common Player Events”Player Joins
Section titled “Player Joins”Track when players join your server:
@EventHandlerfun onPlayerJoin(event: PlayerJoinEvent) { analytics.track("player_join", mapOf( "player_uuid" to event.player.uniqueId, "first_join" to !event.player.hasPlayedBefore() ))}
@EventHandlerpublic void onPlayerJoin(PlayerJoinEvent event) { Map<String, Object> properties = new HashMap<>(); properties.put("player_uuid", event.getPlayer().getUniqueId()); properties.put("first_join", !event.getPlayer().hasPlayedBefore()); analytics.track("player_join", properties);}
Player Leaves
Section titled “Player Leaves”Track when players disconnect:
@EventHandlerfun onPlayerQuit(event: PlayerQuitEvent) { analytics.track("player_quit", mapOf( "player_uuid" to event.player.uniqueId ))}
@EventHandlerpublic void onPlayerQuit(PlayerQuitEvent event) { Map<String, Object> properties = new HashMap<>(); properties.put("player_uuid", event.getPlayer().getUniqueId()); analytics.track("player_quit", properties);}
Chat Messages
Section titled “Chat Messages”Track player chat activity:
@EventHandlerfun onPlayerChat(event: AsyncPlayerChatEvent) { analytics.track("player_chat", mapOf( "player_uuid" to event.player.uniqueId, ))}
@EventHandlerpublic void onPlayerChat(AsyncPlayerChatEvent event) { Map<String, Object> properties = new HashMap<>(); properties.put("player_uuid", event.getPlayer().getUniqueId()); analytics.track("player_chat", properties);}
Player Deaths
Section titled “Player Deaths”Track player deaths and their causes:
@EventHandlerfun onPlayerDeath(event: PlayerDeathEvent) { analytics.track("player_death", mapOf( "player_uuid" to event.entity.uniqueId, "death_cause" to event.entity.lastDamageCause?.cause?.name, ))}
@EventHandlerpublic void onPlayerDeath(PlayerDeathEvent event) { Map<String, Object> properties = new HashMap<>(); properties.put("player_uuid", event.getEntity().getUniqueId()); properties.put("death_cause", event.getEntity().getLastDamageCause().getCause().name());
analytics.track("player_death", properties);}
Player Commands
Section titled “Player Commands”Track command usage:
@EventHandlerfun onPlayerCommand(event: PlayerCommandPreprocessEvent) { val command = event.message.split(" ")[0].removePrefix("/")
analytics.track("player_command", mapOf( "player_uuid" to event.player.uniqueId, "command" to command, "arg_count" to event.message.split(" ").size - 1, "is_op" to event.player.isOp ))}
@EventHandlerpublic void onPlayerCommand(PlayerCommandPreprocessEvent event) { String command = event.getMessage().split(" ")[0].substring(1);
Map<String, Object> properties = new HashMap<>(); properties.put("player_uuid", event.getPlayer().getUniqueId()); properties.put("command", command); properties.put("arg_count", event.getMessage().split(" ").length - 1); properties.put("is_op", event.getPlayer().isOp());
analytics.track("player_command", properties);}
/gamemode
or /ban
.
Best Practices
Section titled “Best Practices”Recommended Properties
Section titled “Recommended Properties”For all player events, consider including:
player_uuid
- The UUID of the player, the Hoglin dashboard uses this to automatically display usernames.world
- Which world the event occurred in
What’s Next?
Section titled “What’s Next?”Now that you’re tracking player events, you might want to: