Installation
Follow the steps on the Apple documentation to add a swift package to your project and use the Topsort SDK link while importing the package.
Configure and initialize the package.
Topsort.shared. configure ( apiKey : "your-api-key" , url : "https://api.topsort.com" )
Usage
Auctions
View all auction models and their definitions in the Swift package link .
let products = AuctionProducts ( ids : [ "p_dsad" , "p_dvra" , "p_oplf" , "p_gjfo" ])
let category = AuctionCategory ( id : "c_fdfa" )
Auction ( type : "banners" , slots : 1 , slotId : "home-banner" , device : "mobile" , category : category),
Auction ( type : "listings" , slots : 2 , device : "mobile" , products : products)
let result: AuctionResponse = await Topsort.shared. executeAuctions ( auctions : auctions)
Events
View all event models and their definitions in the Swift package link .
Impression & click
let resolvedBidId: String ?
struct ProductView : View {
public var product: Product
private func event () -> Event {
if ( self .product.resolvedBidId != nil ) {
event = Event ( resolvedBidId : self .product.resolvedBidId ! , occurredAt : Date.now)
event = Event ( entity : Entity ( type : EntityType.product, id : self .product.id), occurredAt : Date.now)
AsyncImage ( url : self .product.image_url)
Topsort.shared. track ( impression : self . event ())
Topsort.shared. track ( click : self . event ())
Purchase
struct ContentView : View {
var myProduct = Product ( id : "123" , image_url : URL ( string : "https://loremflickr.com/640/480?lock=1234" ) ! , name : "My Product" , resolvedBidId : "123" , price : 12.00 )
ProductView ( product : myProduct)
let item = PurchaseItem ( productId : myProduct.id, unitPrice : myProduct.price)
let event = PurchaseEvent ( items : [item], occurredAt : Date.now)
Topsort.shared. track ( purchase : event)
Banners
This code will display a banner, send an impression event when the banner is shown, and send a click event when the banner is clicked. Inside the callback, you can add logic to execute when the banner is clicked, such as redirecting to the product page.
response
is an AuctionResponse
object described in the AuctionResponse model.
struct ContentView : View {
url : "https://api.topsort.com/v2" ,
// function to execute when banner is clicked