Using Keen IO with Codenvy

Get started with Keen.IO using Codenvy Factory:


Pre Requisites : The user has a basic understanding of KeenIO and Codenvy from the URLs : and

This document will explain the details of integrating the Keen IO API into Codenvy. The explanation has been done with a very basic Java web project that makes use of the KeenIO api to explain some basic features provided by the api.

Register with

Before proceeding to the creation of the Java project, please register at with your email id and then create a KeenIO project that will be used for the integration –


The Show API Keys will provide a Write key and a Read key which along with the Project Id will be required for the integration. The Event Explorer will have the Event Collection details such as the events that are part of the event collection.

Create a project in Codenvy

Now login to your Codenvy workspace and create a WAR project named as ‘KeenSample’. This will have the following structure :


Specify dependencies in pom.xml

To make use of the Keen IO api, please update the pom.xml to include the Keen API client and its dependencies into your project as Maven dependencies –

<project xmlns="" xmlns:xsi=""


      <name>Maven Repository Switchboard</name>


This will include these jars in your project as follows:


Create Java classes

Now create a Java class named ‘’ that will first register the KeenClient api with your project id and read/write keys and then add an event that you want to track into an event collection. If the event collection does not exist, the KeenIO api will create that event collection for the first time and then subsequently use that event collection to store similar events. All this is done inside a method onInitialize() in the sample source Java class.

For an example, the event collection used in the sample source is ‘purchaseitems’ which will be filled with a ‘item’ event to track the purchases.

package com.codenvy;

import java.util.Map;
import java.util.HashMap;

public class KeenIO {

    public void onInitialize() {

String keenProjectId = "51e6875038433153ff000002";
String writeKey = "f3097eaa409601a1fd08b6e7647dec2eae48982c5076e7e04adb63a0541d4223ffad8fa5c255034ab3a266f0ab5a86171ecdfb0b328011d81";
String readKey = "250145b2d3da98e640a5b4860ee7222c1625ac9d17913a5bc6b8740ee8aba5ebbd7abcf5c0d747cc52c50f551bee73160d5dbc73fc76c48e868";

      // initialize the Keen Client with your Project ID.

      // add an event "item"
      Map<String, Object> event = new HashMap<String, Object>();
      event.put("item", "Item2");

      // add it to the "purchases" collection in your Keen Project
      try {
         KeenClient.client().addEvent("purchaseitems", event, null, new MyCallback());
      } catch (KeenException e) {

There is a MyCallback() class which implements a KeenIO api and tells whether the adding of the event to your collection was successfull or not.

This is the MyCallback() class –

package com.codenvy;


public class MyCallback implements AddEventCallback {
    public void onSuccess() {
        System.out.println("It worked!");

    public void onError(String responseBody) {
        System.out.println("It failed! Reason: " + responseBody);

Create index.jsp

Your Codenvy project will have an index.jsp which will invoke the KeenIO class and call the onInitialise() method to add the events to the event collection -

com.codenvy.KeenIO keenIO = new com.codenvy.KeenIO();

Now you build and run your Codenvy project and if you check the logs, it will show the message as “It worked” if the events have been successfully added to the purchaseitems event collection :

If you now check your KeenIO project, you will see the “purchaseitems” event collection with 1 event added to it – Item1 as per our KeenIO java class.


If you click on the event, you will see the properties of that event:


Now we will enhance this sample application to analyse your events by counting the number of occurrences of an event and creating a visualisation data of the analysis such as a pie chart.

For this we will add code to our index.jsp. We will use Keen IO’s data analysis and data visualisation apis and embed them within a simple HTML code to generate a pie chart showing the count of event in the purchaseitems event collection.

We will now add Javascript code to the index.jsp. The code will create a Keen api object and register the object to the Keen Project Id and read/write keys. The following code does that –

 var Keen=Keen || {configure:function(e){this._cf=e},addEvent:function(e,t,n,i){this._eq=this._eq||[],
                      {this._ocrq=this._ocrq||[],this._ocrq.push(e)}};(function(){var e=document.createElement("script");
         +"";var t=document.getElementsByTagName("script")[0];
        projectId: "51e6875038433153ff000002",
        readKey: "250145b2d3da98e640a5b4860ee7222c1625ac9d17913a5bc6b8740ee8aba5ebbd7abcf5c0d747cc52c50f551bee73160d5dbc73fc76c48",
        writeKey: "f3097eaa409601a1fd08b6e7647dec2eae48982c5076e7e04adb63a0541d4223ffad8fa5c255034ab3a266f0ab5a86171ecdfb0b328011d81"

The next function will be the function where the data analysis and data visualisation code will be placed using the apis –

Keen.onChartsReady(function() {
var myMetric = new Keen.Metric("purchaseitems", {
analysisType: "count",
targetProperty: "item",
groupBy: "item"
var myPieChart = new Keen.PieChart(myMetric, {
height: 300,
width: 600,
minimumSlicePercentage: 5,
colors: ["red", "green", "#fef584"],
backgroundColor: "transparent",
title: "count of items",

//Draw the visualization into a div

Here the Keen.Metric is a data analysis api object detailing event collection , the analysis function and the event type that needs to be analysed.

The Keen.PieChart is the data visualisation api object to create the pie chart. The “visualisation-test” id will be used for the HTML element which will contain the pie chart such as


<div id="visualization-test"></div>

On building and running this code, the index.jsp will again create an event Item1 and will also generate a pie chart with the count of items in the event collection “purchaseitems”. The output will be as follows –


Now, to do a comparison between different ”purchaseitems”, build and run the same project again but with a small update in the by changing the event to Item2 –

Map<String, Object> event = new HashMap<String, Object>();
event.put("item", "Item2");

This will then generate the Pie Chart as follows –



The Keen IO provides more apis that can be used in a similar way as above to perform data analysis and data visualisation. More details would be available on their website –