Skip to content

ht-sdks/events-sdk-java

Repository files navigation

Events SDK Java

This SDK is for general Java applications. However, for Android, please see the android specific SDK.

Installation

This SDK is available through JitPack.

Option A - Maven

Add to pom.xml:

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>
<dependency>
  <groupId>com.github.ht-sdks.events-sdk-java</groupId>
  <artifactId>analytics</artifactId>
  <version>LATEST</version>
</dependency>

Option B - Gradle:

  1. Add JitPack to your build
  allprojects {
    repositories {
      ...
      maven { url 'https://jitpack.io' }
    }
  }
  1. Add your dependendcy
compile 'com.github.ht-sdks.events-sdk-java:analytics:+'

Usage

Create an instance of the Analytics object:

import com.hightouch.analytics.Analytics;

public class Main {
  public static void main(String... args) throws Exception {
    final Analytics analytics =
        Analytics.builder(WRITE_KEY)
            .endpoint(API_ENDPOINT)
            .build();
  }
}

Some of the following examples use Guava’s immutable maps, but feel free to use plain old Java maps instead.

Track

analytics.enqueue(TrackMessage.builder("Item Purchased")
    .userId("f4ca124298")
    .properties(ImmutableMap.builder()
        .put("revenue", 39.95)
        .put("shipping", "2-day")
        .build()
    )
);

Screen

analytics.enqueue(ScreenMessage.builder("Schedule")
    .userId("f4ca124298")
    .properties(ImmutableMap.builder()
        .put("category", "Sports")
        .put("path", "/sports/schedule")
        .build()
    )
);

Page

analytics.enqueue(PageMessage.builder("Schedule")
    .userId("f4ca124298")
    .properties(ImmutableMap.builder()
        .put("category", "Sports")
        .put("path", "/sports/schedule")
        .build()
    )
);

Identify

Map<String, String> map = new HashMap();
map.put("name", "Michael Bolton");
map.put("email", "[email protected]");

analytics.enqueue(IdentifyMessage.builder()
        .userId("f4ca124298")
        .traits(map));

Group

analytics.enqueue(GroupMessage.builder("some-group-id")
    .userId("f4ca124298")
    .traits(ImmutableMap.builder()
        .put("name", "Segment")
        .put("size", 50)
        .build()
    )
);

Alias

analytics.enqueue(AliasMessage.builder("previousId")
    .userId("f4ca124298")
);

Context

analytics.enqueue(TrackMessage.builder("Button Clicked")
    .userId("f4ca124298")
    .context(ImmutableMap.builder()
        .put("ip", "12.212.12.49")
        .put("language", "en-us")
        .build()
    )
);