Skip to content

Tracking with our Java client

You can find our Java SDK here.

Start by making a Tracker instance:

java
var tracker = new Tracker(
  "00000000-0000-0000-0000-000000000001",
  "your api key",
  "the server URL for the dataset");

Before you proceed, make sure that you have read and understood the following:

  • How to authenticate against our API, found here

You can read about how to authenticate against our API here

UserFactory

In order to utilize Relewise personalization, it is important to be able to track users who give consent to being tracked (by accepting marketing cookies). We do this with a unique ID. This ID can be accessed through your cookie consent supplier.

If the user has declined cookies, use the following UserFactory method:

java
UserFactory.anonymous()

If the user has accepted cookies:

java
UserFactory.byTemporaryId("unique ID")

Best Practices with TemporaryId

  • Use an ID that is "long-living" and classified as 1st party, e.g. cookie or localStorage.
  • Use same ID as the site uses to recognize the user.
  • Make sure the ID does not change when the user completes an order.

If the user is logged in:

java
UserFactory.byAuthenticatedId("PermanentId", "unique ID")

Best Practices with AuthenticatedId

If possible, use an ID that is semantic and constant For a user journey that "starts" as a Temporary User, make sure to map the Temporary ID on the first request to Relewise. – this can also be done through a UserUpdate-request.

Implementing getting a User-instance

java
private User getUser() {
    //"true" for demo-purposes, this should reflect the users cookie-consent choice.
    boolean userHasAcceptedCookies = true;
    boolean userIsLoggedIn = true;

    User user;

    if (userHasAcceptedCookies) {
        if (userIsLoggedIn) {
            user = UserFactory.byAuthenticatedId("PermanentId", "unique ID");
        } else {
            user = UserFactory.byTemporaryId("unique ID");
        }
    } else {
        user = UserFactory.anonymous();
    }

    return user;
}

If the data is segmented, e.g. on country, you may provide this segmentation via a custom key/value pair:

java
user.addToClassifications("Country", "DK");

We recommend seeking advice from Relewise before introducing segmentation by Classifications. Incorrect or excessive usage may yield worse recommendation and search results.

Product Details Page (PDP)

On the Product Details Page (PDP), the following code is called to track a product view:

java

var request = TrackProductViewRequest.create(
    ProductView.create(
        getUser(),
        Product.create("product id"),
        ProductVariant.create("theVariantIdIfAny otherwise don't parse ProductVariant to constructor")
    )
);

tracker.track(request);

Product Listing Page / Collection Pages (PLP)

On the Product Listing Page (PLP), the following code is called to track a category page view:

java
var request = TrackProductCategoryViewRequest.create(
    ProductCategoryView.create(
        getUser(),
        "category id",
        "sub-category id if any"
    )
);

tracker.track(request);

Content Page

On the Content page, the following code is called to track a content page view:

java
var request = TrackContentViewRequest.create(
    ContentView.create(
        getUser(),
        Content.create("content id")
    )
);

tracker.track(request);

For more advanced usages, you can provide additional data about the content:

java

// Optional: Provide additional information on the content.
// This can be used later during recommendations,
// if there is a need to e.g. filter and/or boost/bury content recommendation

var content = Content.create("content id")
    .addToData("Type", DataValueFactory.create("<page-type>"));

var request = TrackContentViewRequest.create(
    ContentView.create(
        getUser(),
        content
    )
);

tracker.track(request);

Cart / Basket updates

If the user's basket changes, the following is called with the full contents of the basket:

java
var cart = Cart.create(
    getUser(),
    Money.create(Currency.create("DKK"), 150.45)
);

cart.addToLineItems(
    LineItem.create(
        Product.create("product id 1"),
        ProductVariant.create("variant id if any otherwise null instead of ProductVariant"),
        1f,
        24.95
    )
);

cart.addToLineItems(
    LineItem.create(
        Product.create("product id 2"),
        ProductVariant.create("variant id if any otherwise null instead of ProductVariant"),
        2f,
        125.50
    )
);

var request = TrackCartRequest.create(cart);

tracker.track(request);

A note on Cart Name

When tracking a cart, it is not necessary to specify the Name value If you need to track the ID of the cart, such as for the AbandonedCart trigger, Cart ID should not be added as the Name, but instead be added as a Data Field. In this case, Name should be set to default.

Specifying Name is only relevant in the instance where multiple carts need to be tracked simultaneously. For all other cases, Name does not need to be specified, and if you do decide to include it in the request, it should be set to default.

Orders

When user places an order, the following is called with the full contents of the order:

java
var order = Order.create(
    getUser(),
    Money.create(Currency.create("DKK"), 124.95),
    "the order number"
);

order.addToLineItems(
    LineItem.create(
        Product.create("product id 1"),
        ProductVariant.create("variant id if any otherwise null instead of ProductVariant"),
        1f,
        24.95
    )
);

order.addToLineItems(
    LineItem.create(
        Product.create("product id 2"),
        ProductVariant.create("variant id if any otherwise null instead of ProductVariant"),
        2f,
        100.00
    )
);

var request = TrackOrderRequest.create(order);

tracker.track(request);

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com