Events

The Loop54 engine learns from user interactions and those interactions are sent to the engine as events. All implementations must send the required events or the service will not work as intended. In addition to the required events it is also possible to send custom events.

Required Events

The set of events that must be sent are the following:

  • Click - a user clicks on a product in a search result
  • Add to cart - a user adds a product to the shopping cart
  • Purchase - a user completes a the purchase of a product

Sending an Event

This example shows how to send the three required events. The Loop54 connector will automatically keep track of the user ID and use that in the call. If there is a need to send a custom user ID, see the Events with Custom User ID example below.

// Code examples

//click event (can be called on the product page)
Entity clickedEntity = new Entity("Product", productId);
_loop54Client.CreateEvents(new CreateEventsRequest(new ClickEvent(clickedEntity)));

//addtocart event (call this when a customer adds a product to cart)
Entity addToCartEntity = new Entity("Product", productId);
_loop54Client.CreateEvents(new CreateEventsRequest(new AddToCartEvent(addToCartEntity)));

//purchase events (can be called when an order is processed, or on the "thank you" page)  
Entity purchasedEntity = new Entity("Product", productId);
_loop54Client.CreateEvents(new CreateEventsRequest(new PurchaseEvent(purchasedEntity)
{
    OrderId = "13t09j1g", //Optional but recommended
    Quantity = 5, //Optional
    Revenue = 249d //Optional
}
));
C# source code on Github: EventTrackingController.cs
// Code examples

// click event (can be called on the product page)
Entity clickedEntity = new Entity("Product", productId);
loop54Client.createEvents(new CreateEventsRequest(new ClickEvent(clickedEntity)));

// addtocart event (call this when a customer adds a product to cart)
Entity addToCartEntity = new Entity("Product", productId);
loop54Client.createEvents(new CreateEventsRequest(new AddToCartEvent(addToCartEntity)));

// purchase events (can be called when an order is processed, or on the "thank you" page)
Entity purchasedEntity = new Entity("Product", productId);
loop54Client.createEvents(new CreateEventsRequest(new PurchaseEvent(purchasedEntity)
{{
  orderId = "13t09j1g"; // Optional
  quantity = 5; // Optional
  revenue = 249.0; // Optional
}}
    ));
Java source code on Github: EventTrackingController.java
// click event (can be called on the product page)
var clickedEntity = {type: "Product", id: productId};
client.createEvent("click",clickedEntity,null,null,null,function(response){
  console.log("click event response", response);
});
  
// addtocart event (call this when a customer adds a product to cart)
var addToCartEntity = {type: "Product", id: productId};
client.createEvent("addtocart",addToCartEntity,null,null,null,function(response){
  console.log("add to cart response", response);
});

// purchase events (can be called when an order is processed, or on the "thank you" page)  
var purchasedEntity = {type: "Product", id: productId};
var orderId = "13t09j1g"; //Optional but recommended
var quantity = 5; //Optional
var revenue = 249.0; //Optional
  
var response = client.createEvent("purchase",addToCartEntity,orderId,quantity,revenue,null).then((r) => {
  console.log("purchase response", r);
});
JavaScript source code on Github: eventtracking.js
/* Indicate to the engine that user has shown interest in product. */
$connector->clickEvent($connector->entity('Product', $productId));

/* Indicate to the engine that user has added product to cart. */
$connector->addToCartEvent($connector->entity('Product', $productId));

/* Set up a multi-event request from an entire purchase order. */
$purchase = $connector->concatEvents(
    $connector->createEvent('purchase')
       ->entity($connector->entity('Product', $productId))
       ->quantity(5)
       ->revenue(249),
    $connector->createEvent('purchase')
       ->entity($connector->entity('Product', $productId + 1))
);

/* Send the event */
$connector->query($purchase);
PHP source code on Github: Simple.php

Events with Custom User ID

This is how to send events using a custom user ID.

// How it works

//click event with a custom user ID
_loop54Client.CreateEvents(new CreateEventsRequest(new ClickEvent(clickedEntity)).Wrap(new UserMetaData("testUserID")));
C# source code on Github: EventTrackingController.cs
// How it works

//click event with a custom user ID
CreateEventsRequest request = new CreateEventsRequest(new ClickEvent(clickedEntity));
UserMetaData data = new UserMetaData("testUserID");
loop54Client.createEvents(Loop54Client.getRequestContainer(request, data));
Java source code on Github: EventTrackingController.java
// Apologies, we do not yet have a code example for this.
JavaScript source code on Github: eventtracking.js
$getUserId = function () {
    return 'custom-user-id';
};
$connector->withUserId($getUserId)
    ->purchaseEvent($connector->entity('Product', $productId));
PHP source code on Github: Simple.php