Use in other plugins

You can use this plugin to trigger events on Google Analytics from your own plugin. You can use it for free, but it would be nice if you link the plugin if you use it.

Triggering events

The base of the plugin is the Tracker class. It is used to track events at Google Analytics. You should create one instance of this class, it is thread safe. Tracking an events is done on a thread pool, because it is creating a HTTP connection that can take some time. This might be a problem for events that happend very often (mutliple times per second).

public Tracker(Plugin plugin, String analyticsServerDomain, String analyticsServerAccount, boolean enableDebug)

The constructor allows you to supply the google analytics account data. plugin is the instance of your plugin, it is used to access the logger of the pluging, e.g. for debug or error messages. analyticsServerDomain is the name of the domain configured in your Google Anayltics account, or an empty string if no domain is configured. analyticsServerAccount is the id of you account, something like UA-XXXXXXXX-X or MO-XXXXXXXX-X. If enableDebug is true, the url for every event is outputted.

public void Track(String clientName, String visitorId, String visitorIp, String category, String action, String label)

//or 

public void TrackAction(String clientName, String visitorId, String visitorIp, String category, String action, String label)

This method allows you to track both a page view and an action at once. category, action and label are the parameters that are visible in the events section on Google Analytics. You can choose that data format that you need. The plugin is using category for the player name that tirggers something. The action is what is done, like "killed" and the label is adintional information for the action, like "sheep".

The first 3 parameters are important for all other Google Analytics features, like tracking the location, you need to supply them by your own. Here is an example of the call to TrackAction:

plugin.getTracker().TrackAction(getClientName(plugin, player), 
  getClientId(player), 
  getClientIP(player.getAddress().getAddress()), 
  player.getName(), 
  "Respawn", 
  event.isBedSpawn() ? "At bed" : "At spawn");

You can use these implementations for the getClientName, getClientIdand getClientIP methods:

private static String getClientName(GoogleAnalyticsPlugin plugin, Player player) {
    boolean usesPluginChannels = player.getListeningPluginChannels().size() != 0;
    String serverVersion = plugin.getServer().getVersion().substring("git-Bukkit-".length());	    
    String clientVersion = serverVersion.substring(0, serverVersion.indexOf('-'));
    String clientName = "Minecraft";
	    
    // Check for other clients here...
	    
    return clientName + " " + clientVersion + (usesPluginChannels ? " [Supports Plugin Channels]" : "");
}

private static String getClientIP(InetAddress inetAddress) {
    return inetAddress != null ? inetAddress.toString().substring(1) : "0.0.0.0";
}
	
private static String getClientId(Player player) {
    return player.getName();
}

Comments

Posts Quoted:
Reply
Clear All Quotes