This short tutorial will guide you through the basics of our API for Java applications. In this tutorial, we’re using NetBeans 6.9 under Ubuntu Linux 10.10 and Java 1.6.0_20.
Are you ready?
This first step is pretty basic stuff. We’ll create a “Java Application” project under Netbeans. Call it “DeskMetricsDemoApp” just for the sake of convention.
After the project was created, add the DeskMetrics’ jar as a project library. To do so, download our jar (https://github.com/downloads/deskmetrics/jDeskMetrics/jDeskMetrics.jar), and add it to your app by right-clicking Libraries and selecting the Add Jar/Folder option, just like the image below:
Most of the code we’re showing below is just Java’s boilerplate. Skipping to the important part, you just need to call deskmetrics.start and deskmetrics.stop for your app start working with minimal features. Note: you’ll need more than start and stop methods to track all events you want on your application. Continue reading :-)
package DeskMetricsDemoApp;
import com.DeskMetrics.DeskMetrics;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args) throws IOException {
Main frame = new Main();
frame.setSize(new Dimension(200, 200));
frame.setTitle("DeskMetrics DemoApp");
final DeskMetrics deskmetrics = DeskMetrics.getInstance();
String appID = "put_your_app_id_here";
deskmetrics.start(appID, "0.5");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
deskmetrics.stop();
System.out.println("Application finished");
} catch (IOException ex) {
System.out.println("Failed to finish the application correctly.");
}
System.exit(0);
}
});
frame.setVisible(true);
}
}
The two methods call (Start and Stop) are mandatory. The Start method is responsible for gathering the machine information (like OS, plugins and VMs, hardware, etc) and the Stop method will send the gathered data to DeskMetrics Analytics.
Warning
your app won’t work if you don’t call these two methods in order in your application
You need the app id for the Start method. This ID identifies your application on DeskMetrics Analytics. Go to http://analytics.deskmetrics.com/ and you’ll see something like this:
You can find this on DeskMetrics Analytics page (http://analytics.deskmetrics.com/)
Just run the app and close it in sequence. Yeah, it will be an empty form, but it is enough for you to see the impact on your DeskMetrics Analytics.
After you run and successfully close your application, you’ll be able to see the data on your Analytics page:
Mission accomplished, your first integration was done! You can see the data on your application’s Dashboard.
What we’ve done so far were just simple experiments on how DeskMetrics Analytics works. In order to develop a more useful application, we’ll need more than start and stop event trackers. We extended the previous example to show you more about DeskMetrics Analytics’ features:
package com.DeskMetrics.IntegrationTests;
import com.DeskMetrics.DeskMetrics;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args) throws IOException {
Main frame = new Main();
frame.setSize(new Dimension(200, 200));
frame.setTitle("DeskMetrics DemoApp");
final DeskMetrics deskmetrics = DeskMetrics.getInstance();
String appID = "put_your_app_id_here";
deskmetrics.start(appID, "0.5");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
deskmetrics.stop();
System.out.println("Application finished");
} catch (IOException ex) {
System.out.println("Failed to finish the application correctly.");
}
System.exit(0);
}
});
//add a button and a simple event tracking to it
JButton button = new JButton();
button.setText("Click me!");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
deskmetrics.trackEvent("ButtonClick", "ClickMe");
}
});
frame.add(button);
frame.setVisible(true);
}
}
We’ve just added a button and we used the trackEvent method to track it. If you run it and click the button, you’ll see the event showing up on DeskMetrics Analytics. There are many other pieces of information you can track. Go ahead and read the complete DeskMetrics’ Java API and be amazed by what you can do with DeskMetrics.
We hope that this basic tutorial was useful to you. If you missed anything, you can download the entire project and make things work by yourself :-)
By now, you already have some knowledge on the DeskMetrics Analytics platform. If you want to know more, we recommend reading the complete DeskMetrics Analytics Java API.