Guides
Guides

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
MARPurchaseItem *item = [[MARPurchaseItem alloc] initWithQuantity:2 title:@"Made Up Object" price:1234 itemId:@"2345" itemUrl:itemUrl];

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

// log purchase
[[EngageBySailthru 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
EngageBySailthru().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 EngageBySailthru().logPurchase(purchase, new Marigold.MarigoldHandler<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
EngageBySailthru().logPurchase(purchase, object : MarigoldHandler<Void?> {
    override fun onSuccess(value: Void?) {
        // handle success
    }

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

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

// log purchase
EngageBySailthru.logPurchase(purchase).then(result => {
  // handle success
}).catch(e => {
  // handle error
});
JSONClass itemVars = new JSONClass();
itemVars["item"] = "var";
string[] tags = { "tag1", "tag2" };
PurchaseItem[] purchaseItems = new PurchaseItem[] {
  new () { 
    quantity = 2,
    title = "test title",
    price = 1234,
    id = "test id",
    url = new Uri("https://www.sailthru.com/not-a-real-item"),
    vars = itemVars,
    tags = tags,
  }
};

JSONClass purchaseVars = new JSONClass();
purchaseVars["purchase"] = "with var";
Purchase purchase = new Purchase() {
  purchaseItems = purchaseItems,
  vars = purchaseVars
};

new EngageBySailthru().LogPurchase(purchase);

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
MARPurchaseItem *item = [[MARPurchaseItem alloc] initWithQuantity:2 title:@"Made Up Object" price:1234 itemId:@"2345" itemUrl:itemUrl];

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

// log abandoned cart
[[EngageBySailthru 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 = MARPurchaseItem(quantity: 2, title: "Made Up Object", price: 1234, itemId: "2345", itemUrl: url!)

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

// log abandoned cart
EngageBySailthru().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 EngageBySailthru().logAbandonedCart(purchase, new Marigold.MarigoldHandler<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
EngageBySailthru().logAbandonedCart(purchase, object : MarigoldHandler<Void?> {
    override fun onSuccess(value: Void?) {
      // handle success
    }

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

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

// log abandoned cart
EngageBySailthru.logAbandonedCart(purchase).then(result => {
  // handle success
}).catch(e => {
  // handle error
});
PurchaseItem[] purchaseItems = new PurchaseItem[] {
  new () { 
    quantity = 2,
    title = "test title",
    price = 1234,
    id = "test id",
    url = new Uri("https://www.sailthru.com/not-a-real-item"),
  }
};

Purchase purchase = new Purchase() {
  purchaseItems = purchaseItems,
};

new EngageBySailthru().LogAbandonedCart(purchase);

📘

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.


Purchase Adjustments

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

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

[purchase addPurchaseAdjustment:purchaseAdjustment];
let purchaseAdjustment = MARPurchaseAdjustment(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 EngageBySailthru.PurchaseAdjustment("tax", 1234);

purchase.setPurchaseAdjustments([purchaseAdjustment]);
PurchaseAdjustment[] purchaseAdjustments = new PurchaseAdjustment[] {
  new () {
    title = "tax",
    price = -20
  }
};

purchase.purchaseAdjustments = purchaseAdjustments;