{"_id":"5d2568d53c2c3b00e68ab360","project":"55e67aaa9cc7c62b00c4a1ea","version":{"_id":"5b720760c44b7600034b7a08","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"forked_from":"5b1f2cbdfd653400031d8d9f","createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z","categories":["5b720760c44b7600034b79a7","5b720760c44b7600034b79a8","5b720760c44b7600034b79a9","5b720760c44b7600034b79aa","5b720760c44b7600034b79ab","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","5b720760c44b7600034b79ac","5b720760c44b7600034b79ad","5af0fe494ca2730003cbc98a","5af0fe55ec80af0003804ca2"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"API V6","version_clean":"1.4.0","version":"1.4"},"category":{"_id":"5b720760c44b7600034b79ac","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"version":"5b720760c44b7600034b7a08","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-02-08T21:31:11.878Z","from_sync":false,"order":6,"slug":"advanced-techniques","title":"Advanced Techniques"},"user":"5b0b7a46a26e6400036604fd","__v":0,"parentDoc":null,"metadata":{"title":"","description":"","image":[]},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2019-07-10T04:25:57.488Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":29,"body":"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.\n\n### Purchase\nPurchases can be logged by creating a Purchase with an array containing all the PurchaseItems that are being purchased and providing it to the SDK.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"NSURL *itemUrl = [NSURL URLWithString::::at:::\\\"https://www.mobile.sailthru.com/not-a-real-item\\\"];\\n\\n// create purchase items\\nCarnivalPurchaseItem *item = [[CarnivalPurchaseItem alloc] initWithQuantity:2 title:@\\\"Made Up Object\\\" price:1234 itemId:@\\\"2345\\\" itemUrl:itemUrl];\\n\\n// create purchase\\nCarnivalPurchase *purchase = [[CarnivalPurchase alloc] initWithPurchaseItems:@[ item ]];\\n\\n// log purchase\\n[Carnival logPurchase:purchase withResponse:^(NSError * _Nullable error) {\\n  if (error) {\\n    // handle error\\n    return;\\n  }\\n  // handle success\\n}];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"let url = URL(string: \\\"https://www.mobile.sailthru.com/not-a-real-item\\\")\\n\\n// create purchase items\\nlet item = CarnivalPurchaseItem(quantity: 2, title: \\\"Made Up Object\\\", price: 1234, itemId: \\\"2345\\\", itemUrl: url!)\\n\\n// create purchase\\nlet purchase = CarnivalPurchase(purchaseItems: [ item ])\\n\\n// log purchase\\nCarnival.logPurchase(purchase!) { (errorOrNil) in\\n\\t  if let error = errorOrNil {\\n        // handle error\\n        return\\n    }\\n    // handle success\\n}\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"URI url = URI.create(\\\"https://www.mobile.sailthru.com/also-does-not-exist\\\");\\n\\n// create purchase items\\nPurchaseItem purchaseItem = new PurchaseItem(2, \\\"Made Up Object\\\", 1234, \\\"2345\\\", url);\\nArrayList<PurchaseItem> itemArrayList = new ArrayList<>();\\nitemArrayList.add(purchaseItem);\\n\\n// create purchase\\nPurchase purchase = new Purchase(itemArrayList);\\n\\n// log purchase\\nCarnival.logPurchase(purchase, new Carnival.CarnivalHandler<Void>() {\\n    @Override\\n    public void onSuccess(Void value) {\\n        // handle success\\n    }\\n\\n    @Override\\n    public void onFailure(Error error) {\\n        // handle error\\n    }\\n});\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"// create purchases\\nvar purchaseItem1 = new Carnival.PurchaseItem(1, \\\"title\\\", 1234, \\\"2345\\\", \\\"https://www.mobile.sailthru.com/not-a-real-item\\\");\\nvar purchaseItems = [ purchaseItem ];\\n\\n// create purchase\\nvar purchase = new Carnival.Purchase(purchaseItems);\\n\\n// log purchase\\nCarnival.logPurchase(purchase).then(result => {\\n  // handle success\\n}).catch(e => {\\n  // handle error\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]\n### Abandoned Cart\nAn abandoned cart requires the same purchase item details as a regular purchase, so the setup is very similar but with a different method call.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"NSURL *itemUrl = [NSURL URLWithString:@\\\"https://www.mobile.sailthru.com/not-a-real-item\\\"];\\n\\n// create purchase items\\nCarnivalPurchaseItem *item = [[CarnivalPurchaseItem alloc] initWithQuantity:2 title:@\\\"Made Up Object\\\" price:1234 itemId:@\\\"2345\\\" itemUrl:itemUrl];\\n\\n// create purchase\\nCarnivalPurchase *purchase = [[CarnivalPurchase alloc] initWithPurchaseItems:@[ item ]];\\n\\n// log abandoned cart\\n[Carnival logAbandonedCart:purchase withResponse:^(NSError * _Nullable error) {\\n  if (error) {\\n    // handle error\\n    return;\\n  }\\n  // handle success\\n}];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"let url = URL(string: \\\"https://www.mobile.sailthru.com/not-a-real-item\\\")\\n\\n// create purchase items\\nlet item = CarnivalPurchaseItem(quantity: 2, title: \\\"Made Up Object\\\", price: 1234, itemId: \\\"2345\\\", itemUrl: url!)\\n\\n// create purchase\\nlet purchase = CarnivalPurchase(purchaseItems: [ item ])\\n\\n// log abandoned cart\\nCarnival.logAbandonedCart(purchase!) { (errorOrNil) in\\n\\t  if let error = errorOrNil {\\n        // handle error\\n        return\\n    }\\n    // handle success\\n}\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"URI url = URI.create(\\\"https://www.mobile.sailthru.com/also-does-not-exist\\\");\\n\\n// create purchase items\\nPurchaseItem purchaseItem = new PurchaseItem(2, \\\"Made Up Object\\\", 1234, \\\"2345\\\", url);\\nArrayList<PurchaseItem> itemArrayList = new ArrayList<>();\\nitemArrayList.add(purchaseItem);\\n\\n// create purchase\\nPurchase purchase = new Purchase(itemArrayList);\\n\\n// log abandoned cart\\nCarnival.logAbandonedCart(purchase, new Carnival.CarnivalHandler<Void>() {\\n    @Override\\n    public void onSuccess(Void value) {\\n        // handle success\\n    }\\n\\n    @Override\\n    public void onFailure(Error error) {\\n        // handle error\\n    }\\n});\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"// create purchases\\nvar purchaseItem1 = new Carnival.PurchaseItem(1, \\\"title\\\", 1234, \\\"2345\\\", \\\"https://www.mobile.sailthru.com/not-a-real-item\\\");\\nvar purchaseItems = [ purchaseItem ];\\n\\n// create purchase\\nvar purchase = new Carnival.Purchase(purchaseItems);\\n\\n// log abandoned cart\\nCarnival.logAbandonedCart(purchase).then(result => {\\n  // handle success\\n}).catch(e => {\\n  // handle error\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Note\",\n  \"body\": \"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.\"\n}\n[/block]\n\n### Content Items\nPurchase Items can also be created from ContentItem instances that have been returned in the `getRecommendations` method.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"CarnivalPurchaseItem *purchaseItem = [[CarnivalPurchaseItem alloc] initWithContentItem:contentItem];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"let purchaseItem = CarnivalPurchaseItem(contentItem: contentItem)\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"PurchaseItem purchaseItem = new PurchaseItem(contentItem);\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"var purchaseItem = Carnival.PurchaseItem.fromContentItem(contentItem);\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]\nAlternatively if you have an array of ContentItem instances you can create the purchase with them directly.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"CarnivalPurchase *purchase = [[CarnivalPurchase alloc] initWithContentItems:contentItems];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"let purchase = CarnivalPurchase(contentItems: contentItems)\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"Purchase purchase = new Purchase(contentItems);\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"var purchase = Carnival.Purchase.fromContentItems(contentItems);\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]","excerpt":"How to send purchase and abandoned cart events through through the SDK.","slug":"purchases-and-abandoned-carts","type":"basic","title":"Purchases and Abandoned Carts"}

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. [block:code] { "codes": [ { "code": "NSURL *itemUrl = [NSURL URLWithString:@\"https://www.mobile.sailthru.com/not-a-real-item\"];\n\n// create purchase items\nCarnivalPurchaseItem *item = [[CarnivalPurchaseItem alloc] initWithQuantity:2 title:@\"Made Up Object\" price:1234 itemId:@\"2345\" itemUrl:itemUrl];\n\n// create purchase\nCarnivalPurchase *purchase = [[CarnivalPurchase alloc] initWithPurchaseItems:@[ item ]];\n\n// log purchase\n[Carnival logPurchase:purchase withResponse:^(NSError * _Nullable error) {\n if (error) {\n // handle error\n return;\n }\n // handle success\n}];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "let url = URL(string: \"https://www.mobile.sailthru.com/not-a-real-item\")\n\n// create purchase items\nlet item = CarnivalPurchaseItem(quantity: 2, title: \"Made Up Object\", price: 1234, itemId: \"2345\", itemUrl: url!)\n\n// create purchase\nlet purchase = CarnivalPurchase(purchaseItems: [ item ])\n\n// log purchase\nCarnival.logPurchase(purchase!) { (errorOrNil) in\n\t if let error = errorOrNil {\n // handle error\n return\n }\n // handle success\n}", "language": "swift", "name": "iOS (Swift)" }, { "code": "URI url = URI.create(\"https://www.mobile.sailthru.com/also-does-not-exist\");\n\n// create purchase items\nPurchaseItem purchaseItem = new PurchaseItem(2, \"Made Up Object\", 1234, \"2345\", url);\nArrayList<PurchaseItem> itemArrayList = new ArrayList<>();\nitemArrayList.add(purchaseItem);\n\n// create purchase\nPurchase purchase = new Purchase(itemArrayList);\n\n// log purchase\nCarnival.logPurchase(purchase, new Carnival.CarnivalHandler<Void>() {\n @Override\n public void onSuccess(Void value) {\n // handle success\n }\n\n @Override\n public void onFailure(Error error) {\n // handle error\n }\n});", "language": "java", "name": "Android (Java)" }, { "code": "// create purchases\nvar purchaseItem1 = new Carnival.PurchaseItem(1, \"title\", 1234, \"2345\", \"https://www.mobile.sailthru.com/not-a-real-item\");\nvar purchaseItems = [ purchaseItem ];\n\n// create purchase\nvar purchase = new Carnival.Purchase(purchaseItems);\n\n// log purchase\nCarnival.logPurchase(purchase).then(result => {\n // handle success\n}).catch(e => {\n // handle error\n});", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block] ### 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. [block:code] { "codes": [ { "code": "NSURL *itemUrl = [NSURL URLWithString:@\"https://www.mobile.sailthru.com/not-a-real-item\"];\n\n// create purchase items\nCarnivalPurchaseItem *item = [[CarnivalPurchaseItem alloc] initWithQuantity:2 title:@\"Made Up Object\" price:1234 itemId:@\"2345\" itemUrl:itemUrl];\n\n// create purchase\nCarnivalPurchase *purchase = [[CarnivalPurchase alloc] initWithPurchaseItems:@[ item ]];\n\n// log abandoned cart\n[Carnival logAbandonedCart:purchase withResponse:^(NSError * _Nullable error) {\n if (error) {\n // handle error\n return;\n }\n // handle success\n}];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "let url = URL(string: \"https://www.mobile.sailthru.com/not-a-real-item\")\n\n// create purchase items\nlet item = CarnivalPurchaseItem(quantity: 2, title: \"Made Up Object\", price: 1234, itemId: \"2345\", itemUrl: url!)\n\n// create purchase\nlet purchase = CarnivalPurchase(purchaseItems: [ item ])\n\n// log abandoned cart\nCarnival.logAbandonedCart(purchase!) { (errorOrNil) in\n\t if let error = errorOrNil {\n // handle error\n return\n }\n // handle success\n}", "language": "swift", "name": "iOS (Swift)" }, { "code": "URI url = URI.create(\"https://www.mobile.sailthru.com/also-does-not-exist\");\n\n// create purchase items\nPurchaseItem purchaseItem = new PurchaseItem(2, \"Made Up Object\", 1234, \"2345\", url);\nArrayList<PurchaseItem> itemArrayList = new ArrayList<>();\nitemArrayList.add(purchaseItem);\n\n// create purchase\nPurchase purchase = new Purchase(itemArrayList);\n\n// log abandoned cart\nCarnival.logAbandonedCart(purchase, new Carnival.CarnivalHandler<Void>() {\n @Override\n public void onSuccess(Void value) {\n // handle success\n }\n\n @Override\n public void onFailure(Error error) {\n // handle error\n }\n});", "language": "java", "name": "Android (Java)" }, { "code": "// create purchases\nvar purchaseItem1 = new Carnival.PurchaseItem(1, \"title\", 1234, \"2345\", \"https://www.mobile.sailthru.com/not-a-real-item\");\nvar purchaseItems = [ purchaseItem ];\n\n// create purchase\nvar purchase = new Carnival.Purchase(purchaseItems);\n\n// log abandoned cart\nCarnival.logAbandonedCart(purchase).then(result => {\n // handle success\n}).catch(e => {\n // handle error\n});", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block] [block:callout] { "type": "info", "title": "Note", "body": "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." } [/block] ### Content Items Purchase Items can also be created from ContentItem instances that have been returned in the `getRecommendations` method. [block:code] { "codes": [ { "code": "CarnivalPurchaseItem *purchaseItem = [[CarnivalPurchaseItem alloc] initWithContentItem:contentItem];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "let purchaseItem = CarnivalPurchaseItem(contentItem: contentItem)", "language": "swift", "name": "iOS (Swift)" }, { "code": "PurchaseItem purchaseItem = new PurchaseItem(contentItem);", "language": "java", "name": "Android (Java)" }, { "code": "var purchaseItem = Carnival.PurchaseItem.fromContentItem(contentItem);", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block] Alternatively if you have an array of ContentItem instances you can create the purchase with them directly. [block:code] { "codes": [ { "code": "CarnivalPurchase *purchase = [[CarnivalPurchase alloc] initWithContentItems:contentItems];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "let purchase = CarnivalPurchase(contentItems: contentItems)", "language": "swift", "name": "iOS (Swift)" }, { "code": "Purchase purchase = new Purchase(contentItems);", "language": "java", "name": "Android (Java)" }, { "code": "var purchase = Carnival.Purchase.fromContentItems(contentItems);", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block]