Purchases and Abandoned Carts

How to send purchase and abandoned cart events through through the SDK.

Purchases and abandoned carts sent to the Sailthru platform through the SDK. They can then be used for purchase attribution and to trigger abandoned cart flows in LO.

Purchase

Purchases can be logged by creating a Purchase with an array containing all the PurchaseItems that are being purchased and providing it to the SDK.

NSURL *itemUrl = [NSURL URLWithString:@"https://www.mobile.sailthru.com/not-a-real-item"];

// create purchase items
STMPurchaseItem *item = [[STMPurchaseItem alloc] initWithQuantity:2 title:@"Made Up Object" price:1234 itemId:@"2345" itemUrl:itemUrl];

// create purchase
STMPurchase *purchase = [[STMPurchase alloc] initWithPurchaseItems:@[ item ]];

// log purchase
[[SailthruMobile new] logPurchase:purchase withResponse:^(NSError * _Nullable error) {
  if (error) {
    // handle error
    return;
  }
  // handle success
}];
let url = URL(string: "https://www.mobile.sailthru.com/not-a-real-item")

// create purchase items
let item = STMPurchaseItem(quantity: 2, title: "Made Up Object", price: 1234, itemId: "2345", itemUrl: url!)

// create purchase
let purchase = STMPurchase(purchaseItems: [ item ])

// log purchase
SailthruMobile().logPurchase(purchase!) { (errorOrNil) in
      if let error = errorOrNil {
        // handle error
        return
    }
    // handle success
}
URI url = URI.create("https://www.mobile.sailthru.com/also-does-not-exist");

// create purchase items
PurchaseItem purchaseItem = new PurchaseItem(2, "Made Up Object", 1234, "2345", url);
ArrayList<PurchaseItem> itemArrayList = new ArrayList<>();
itemArrayList.add(purchaseItem);

// create purchase
Purchase purchase = new Purchase(itemArrayList);

// log purchase
new SailthruMobile().logPurchase(purchase, new SailthruMobile.SailthruMobileHandler<Void>() {
    @Override
    public void onSuccess(Void value) {
        // handle success
    }

    @Override
    public void onFailure(Error error) {
        // handle error
    }
});
val url = URI.create("https://www.mobile.sailthru.com/also-does-not-exist")

// create purchase items
val purchaseItem = PurchaseItem(2, "Made Up Object", 1234, "2345", url)
val itemArrayList= listOf(purchaseItem) as ArrayList<PurchaseItem>

// create purchase
val purchase = Purchase(itemArrayList)

// log purchase
SailthruMobile().logPurchase(purchase, object : SailthruMobileHandler<Void?> {
    override fun onSuccess(value: Void?) {
        // handle success
    }

    override fun onFailure(error: Error?) {
        // handle error
    }
})
// create purchases
var purchaseItem1 = new SailthruMobile.PurchaseItem(1, "title", 1234, "2345", "https://www.mobile.sailthru.com/not-a-real-item");
var purchaseItems = [ purchaseItem ];

// create purchase
var purchase = new SailthruMobile.Purchase(purchaseItems);

// log purchase
SailthruMobile.logPurchase(purchase).then(result => {
  // handle success
}).catch(e => {
  // handle error
});

Abandoned Cart

An abandoned cart requires the same purchase item details as a regular purchase, so the setup is very similar but with a different method call.

NSURL *itemUrl = [NSURL URLWithString:@"https://www.mobile.sailthru.com/not-a-real-item"];

// create purchase items
STMPurchaseItem *item = [[STMPurchaseItem alloc] initWithQuantity:2 title:@"Made Up Object" price:1234 itemId:@"2345" itemUrl:itemUrl];

// create purchase
STMPurchase *purchase = [[STMPurchase alloc] initWithPurchaseItems:@[ item ]];

// log abandoned cart
[[SailthruMobile new] logAbandonedCart:purchase withResponse:^(NSError * _Nullable error) {
  if (error) {
    // handle error
    return;
  }
  // handle success
}];
let url = URL(string: "https://www.mobile.sailthru.com/not-a-real-item")

// create purchase items
let item = STMPurchaseItem(quantity: 2, title: "Made Up Object", price: 1234, itemId: "2345", itemUrl: url!)

// create purchase
let purchase = STMPurchase(purchaseItems: [ item ])

// log abandoned cart
SailthruMobile().logAbandonedCart(purchase!) { (errorOrNil) in
      if let error = errorOrNil {
        // handle error
        return
    }
    // handle success
}
URI url = URI.create("https://www.mobile.sailthru.com/also-does-not-exist");

// create purchase items
PurchaseItem purchaseItem = new PurchaseItem(2, "Made Up Object", 1234, "2345", url);
ArrayList<PurchaseItem> itemArrayList = new ArrayList<>();
itemArrayList.add(purchaseItem);

// create purchase
Purchase purchase = new Purchase(itemArrayList);

// log abandoned cart
new SailthruMobile().logAbandonedCart(purchase, new SailthruMobile.SailthruMobileHandler<Void>() {
    @Override
    public void onSuccess(Void value) {
        // handle success
    }

    @Override
    public void onFailure(Error error) {
        // handle error
    }
});
val url = URI.create("https://www.mobile.sailthru.com/also-does-not-exist")

// create purchase items
val purchaseItem = PurchaseItem(2, "Made Up Object", 1234, "2345", url)
val itemArrayList= listOf(purchaseItem) as ArrayList<PurchaseItem>

// create purchase
val purchase = Purchase(itemArrayList)

// log abandoned cart
SailthruMobile().logAbandonedCart(purchase, object : SailthruMobileHandler<Void?> {
    override fun onSuccess(value: Void?) {
      // handle success
    }

    override fun onFailure(error: Error?) {
      // handle error
    }
})
// create purchases
var purchaseItem1 = new SailthruMobile.PurchaseItem(1, "title", 1234, "2345", "https://www.mobile.sailthru.com/not-a-real-item");
var purchaseItems = [ purchaseItem ];

// create purchase
var purchase = new SailthruMobile.Purchase(purchaseItems);

// log abandoned cart
SailthruMobile.logAbandonedCart(purchase).then(result => {
  // handle success
}).catch(e => {
  // handle error
});

📘

Note

When using the logAbandonedCart SDK function, the SDK sets the channel automatically. If you're sending carts by other methods (for example, your servers) ensure that they only send web carts. Sending a mobile cart by another method will overwrite mobile carts sent by the SDK.

Content Items

Purchase Items can also be created from ContentItem instances that have been returned in the getRecommendations method.

STMPurchaseItem *purchaseItem = [[STMPurchaseItem alloc] initWithContentItem:contentItem];
let purchaseItem = STMPurchaseItem(contentItem: contentItem)
PurchaseItem purchaseItem = new PurchaseItem(contentItem);
val purchaseItem = PurchaseItem(contentItem)
var purchaseItem = SailthruMobile.PurchaseItem.fromContentItem(contentItem);

Alternatively if you have an array of ContentItem instances you can create the purchase with them directly.

STMPurchase *purchase = [[STMPurchase alloc] initWithContentItems:contentItems];
let purchase = STMPurchase(contentItems: contentItems)
Purchase purchase = new Purchase(contentItems);
val purchase = Purchase(contentItems)
var purchase = SailthruMobile.Purchase.fromContentItems(contentItems);

Purchase Adjustments

Purchase adjustments can be added to the purchase in order to change the total price for extras such as tax, shipping etc.

STMPurchaseAdjustment *purchaseAdjustment = [[STMPurchaseAdjustment alloc] initWithTitle:@"tax" price:1234];

[purchase addPurchaseAdjustment:purchaseAdjustment];
let purchaseAdjustment = STMPurchaseAdjustment(title:"tax", price:1234)
        
purchase?.add(purchaseAdjustment!)
PurchaseAdjustment purchaseAdjustment = new PurchaseAdjustment("tax", 123);

purchase.getPurchaseAdjustments().add(purchaseAdjustment);
val purchaseAdjustment = PurchaseAdjustment("tax", 123)

purchase.purchaseAdjustments.add(purchaseAdjustment)
var purchaseAdjustment = new SailthruMobile.PurchaseAdjustment("tax", 1234);

purchase.setPurchaseAdjustments([purchaseAdjustment]);

Did this page help you?