Skip to content

The Cxense Java SDK enables Java developers to easily work with the Cxense APIs.

Notifications You must be signed in to change notification settings

cXense/cx-java-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cx-java-sdk

Note: that this API is currently experimental and not supported by Cxense. A fully supported version might be released in the future.

Features

The Cxense Java SDK enables Java developers to easily work with the Cxense APIs. You can get started in minutes using Maven or by downloading a single zip file.

Getting Started

Sign up for Cxense

Before you begin, you need a Cxense account. Please see the Cxense homepage for information about how to create a Cxense account and retrieve your Cxense credentials.

Install the SDK

Using the SDK with Maven
<dependencies>
  <dependency>
    <groupId>com.cxense.sdk</groupId>
    <artifactId>cx-java-sdk</artifactId>
    <version>1.0.3</version>
  </dependency>
</dependencies>
Using the SDK with just one .jar file

Download the single zip file and add the .jar file to the classpath.

Building From Source

Check out the code from GitHub and build it using Maven.

mvn clean package

Sample usage

Send a page view event

Send a basic page view event

import com.cxense.sdk.Cxense;

public class SDKSample1 {
    public static void main(String[] args) throws Exception {

        //Cxense.pageViewEvent(<site ID>, <page url>, <end user ID>).send();
        Cxense.pageViewEvent("1234", "http://www.site.com/", "abcd").send();

    }
}
Send an advanced page view event
import com.cxense.sdk.Cxense;

public class SDKSample2 {
    public static void main(String[] args) throws Exception {
        
        //Cxense.pageViewEvent(<site ID>, <page url>, <end user ID>);
        Cxense.pageViewEvent("1234", "http://www.site.com/", "abcd")
            .setReferrer("http://www.siteb.com/")
            .setScreenSize(1024, 768)
            .setWindowSize(927, 349)
            .setDevicePixelRatio(1.4)
            .setGeoPosition(7.6145, 110.7122)
            .addCustomParameter("section", "sports")
            .addExternalUserId("fbk", "ABCD") // Add FaceBook user ID
            .send();
    }
}
Send a page view event with maps of values

For custom parameters and external IDs, you can provide a map of values instead of individual values:

import com.cxense.sdk.Cxense;

public class SDKSample3 {
    public static void main(String[] args) throws Exception {

        Map<String, String> customParameters = new HashMap<String, String>() {{
            put("a", "1");
            put("b", "2");
            put("c", "3");
        }};

        Map<String, String> externalUserIds = new HashMap<String, String>() {{
            put("fbk", "1234");
            put("slk", "ABCD");
        }};
        
        //Cxense.pageViewEvent(<site ID>, <page url>, <end user ID>);
        Cxense.pageViewEvent("1234", "http://www.site.com/", "abcd")
            .addCustomParameters(customParameters)
            .addExternalUserIds(externalUserIds)
            .send();
    }
}

Generic API requests

Request and response as JSON strings

A generic API request sending the request as a JSON string and getting the response back as a JSON string

import com.cxense.sdk.Cxense;

public class SDKSample4 {
    public static void main(String[] args) throws Exception {

        Cxense cx = new Cxense("<username (email)>", "<api key>");
        String apiPath = "/site";
        String request = "{}";
        String response = cx.apiRequest(apiPath, request);

    }
}
Request and response as JSON objects

A generic API request sending the request as a JSON object and getting the response back as a JSON object

import com.cxense.sdk.Cxense;
import javax.json.Json;
import javax.json.JsonObject;

public class SDKSample5 {
    public static void main(String[] args) throws Exception {

        Cxense cx = new Cxense("<username (email)>", "<api key>");
        String apiPath = "/site";
        JsonObject requestObject = Json.createObjectBuilder().build();
        JsonObject responseObject = cx.apiRequest(apiPath, requestObject);

    }
}
API request using a persisted query

A persisted query can be setup that require no authentication. Only the persisted query ID need to be supplied.

import com.cxense.sdk.Cxense;

public class SDKSample6 {
    public static void main(String[] args) throws Exception {

        Cxense cx = new Cxense();
        String apiPath = "/site";
        String request = "{}";
        String persistedQueryId = "<persisted query id>";
        String response = cx.apiRequest(apiPath, request, persistedQueryId);

    }
}
Using a custom HTTP client

If you want to use a custom HTTP client, there is a helper method that will build the required authentication headers for you. Here is an example using Java's URLConnection:

import com.cxense.sdk.Cxense;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class SDKSample7 {
    public static void main(String[] args) throws Exception {

        String username = "<username (email)>";
        String apiKey = "<api key>";
        String apiBaseUrl = "https://api.cxense.com";
        String apiPath = "/site";
        String jsonStringQuery = "{}";

        String encodedJsonQuery = URLEncoder.encode(jsonStringQuery, "UTF-8");
        URLConnection connection = new URL(apiBaseUrl + apiPath + "?json=" + encodedJsonQuery).openConnection();
        connection.setRequestProperty("X-cXense-Authentication", Cxense.getHttpAuthenticationHeader(username, apiKey));
        connection.connect();
        String jsonResponse = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();

    }
}

Resources

About

The Cxense Java SDK enables Java developers to easily work with the Cxense APIs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages