Skip to main content
Topsort’s Swift library enables our clients to easily send auction requests and track events within iOS applications.

Installation

Install using package.swift:
let package = Package(
    dependencies: [
        .package(url: "https://github.com/Topsort/topsort.swift.git", from: "1.0.0"),
    ]
)

Configuration

import SwiftUI
import Topsort

@main
struct MyApp: App {
    init() {
        Topsort.shared.configure(apiKey: "your-api-key", url: "https://api.topsort.com")
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Requesting Auctions

import Topsort

let products = AuctionProducts(ids: ["p_dsad", "p_dvra", "p_oplf"])
let category = AuctionCategory(id: "c_fdfa")

let auctions = [
    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)

Reporting Events

Impressions and Clicks

var body: some View {
    VStack {
        AsyncImage(url: self.product.image_url)
        Text(self.product.name)
    }
    .onAppear {
        Topsort.shared.track(impression: self.event())
    }
    .onTapGesture {
        Topsort.shared.track(click: self.event())
    }
}

Purchases

Button("Purchase me!") {
    let item = PurchaseItem(productId: myProduct.id, unitPrice: myProduct.price)
    let event = PurchaseEvent(items: [item], occurredAt: Date.now)
    Topsort.shared.track(purchase: event)
}

Displaying Banners

import TopsortBanners

struct ContentView: View {
    var body: some View {
        TopsortBanner(bannerAuctionBuilder: .init(slotId: "slotId", deviceType: "device"))
            .contentMode(.fill)
            .onNoWinners({ })
            .onError({ error in })
            .onImageLoad({ })
            .buttonClickedAction({ response in })
            .frame(maxHeight: 50)
            .clipped()
    }
}
For full documentation, see the GitHub repository.