Purchases and Abandoned Carts
Contents
Send purchase and abandoned cart events 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.iOS (Objective-C)
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
}];
iOS (Swift)
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
}
Android (Java)
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
}
});
Android (Kotlin)
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
}
})
React Native (JavaScript)
// 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.iOS (Objective-C)
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
}];
iOS (Swift)
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
}
Android (Java)
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
}
});
Android (Kotlin)
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
}
})
React Native (JavaScript)
// 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
});
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 thegetRecommendations
method.
iOS (Objective-C)
STMPurchaseItem *purchaseItem = [[STMPurchaseItem alloc] initWithContentItem:contentItem];
iOS (Swift)
let purchaseItem = STMPurchaseItem(contentItem: contentItem)
Android (Java)
PurchaseItem purchaseItem = new PurchaseItem(contentItem);
Android (Kotlin)
val purchaseItem = PurchaseItem(contentItem)
React Native (JavaScript)
var purchaseItem = SailthruMobile.PurchaseItem.fromContentItem(contentItem);
iOS (Objective-C)
STMPurchase *purchase = [[STMPurchase alloc] initWithContentItems:contentItems];
iOS (Swift)
let purchase = STMPurchase(contentItems: contentItems)
Android (Java)
Purchase purchase = new Purchase(contentItems);
Android (Kotlin)
val purchase = Purchase(contentItems)
React Native (JavaScript)
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.iOS (Objective-C)
STMPurchaseAdjustment *purchaseAdjustment = [[STMPurchaseAdjustment alloc] initWithTitle:@"tax" price:1234];
[purchase addPurchaseAdjustment:purchaseAdjustment];
iOS (Swift)
let purchaseAdjustment = STMPurchaseAdjustment(title:"tax", price:1234)
purchase?.add(purchaseAdjustment!)
Android (Java)
PurchaseAdjustment purchaseAdjustment = new PurchaseAdjustment("tax", 123);
purchase.getPurchaseAdjustments().add(purchaseAdjustment);
Android (Kotlin)
val purchaseAdjustment = PurchaseAdjustment("tax", 123)
purchase.purchaseAdjustments.add(purchaseAdjustment)
React Native (JavaScript)
var purchaseAdjustment = new SailthruMobile.PurchaseAdjustment("tax", 1234);
purchase.setPurchaseAdjustments([purchaseAdjustment]);