Table of Contents
Please refer to the complete instrumentation guide for a complete reference, release notes etc.
Install the SDK
You can install the iOS SDK via Swift Package Manager (recommended) or CocoaPods.
Option A: Swift Package Manager
Xcode 13+
Go to File > Add Packages…
Enter the SDK URL:
https://github.com/blue-triangle-tech/btt-swift-sdk.gitComplete the prompt.
Xcode 11–12
Go to File > Swift Packages > Add Package Dependency…
Enter the same package URL.
Finish installation.
Option B: CocoaPods
Add the pod:
pod 'BlueTriangleSDK-Swift'
Then install:
pod install
Configure Your Site ID
Add configuration to your: AppDelegate AppDelegate.application(_:didFinishLaunchingWithOptions:) method.
Or
import BlueTriangle
BlueTriangle.configure { config in
config.siteID = "<BTT_SITE_ID>"
}
Track Checkout Events
Use Timer, Page, and PurchaseConfirmation:
Brand value example:
let timer = BlueTriangle.startTimer(
page: Page(pageName: "SignUp", brandValue: 100.0)
)
BlueTriangle.endTimer(timer)
Cart + order example:
let timer = BlueTriangle.startTimer(
page: Page(pageName: "Confirmation")
)
BlueTriangle.endTimer(
timer,
purchaseConfirmation: PurchaseConfirmation(
cartValue: 99.0,
cartCount: 2,
cartCountCheckout: 2,
orderNumber: "ORD-123345"
)
)
Note: PurchaseConfirmation automatically uses the timer’s end time as the order timestamp.
SwiftUI Views Tracking
Visit this page for instructions for SwiftUI.
Network Capture
The Blue Triangle SDK supports capturing network requests using either the bt-prefixed URLSession methods or the NetworkCaptureSessionDelegate or manual.
Option 1 : NetworkCaptureSessionDelegate
You can use NetworkCaptureSessionDelegate or a subclass as your URLSession delegate to gather information about network requests when network capture is enabled:
let session = URLSession(
configuration: .default,
delegate: NetworkCaptureSessionDelegate(),
delegateQueue: nil)
let timer = BlueTriangle.startTimer(page: Page(pageName: "MY_PAGE"))
...
let (data, response) = try await session.data(from: URL(string: "https://example.com")!)If you have already implemented and set URLSessionDelegate to URLSession, you can call NetworkCaptureSessionDelegate objects urlSession(session: task: didFinishCollecting:) method:
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
//Your code ...
let sessionDelegate = NetworkCaptureSessionDelegate()
sessionDelegate.urlSession(session, task: task, didFinishCollecting: metrics)
}Option 2: Use URLSession bt-prefixed Methods
Alternatively, use bt-prefixed URLSession methods to capture network requests:
Use these methods just as you would their standard counterparts:
let timer = BlueTriangle.startTimer(page: Page(pageName: "MY_PAGE"))
...
URLSession.shared.btDataTask(with: URL(string: "https://example.com")!) { data, response, error in
// ...
}.resume()
Option 3: Manual Network Capture
For other network capture requirements, captured requests can be manually created and submitted to the tracker.
If you have the URL, method, and requestBodyLength in the request, and httpStatusCode, responseBodyLength, and contentType in the response
let tracker = NetworkCaptureTracker.init(url: "https://example.com", method: "post", requestBodylength: 9130)
tracker.submit(200, responseBodyLength: 11120, contentType: "json")
If you have urlRequest in request and urlResponse in response
let tracker = NetworkCaptureTracker.init(request: urlRequest)
tracker.submit(urlResponse)
where urlRequest and urlResponse are of URLRequest and URLResponse types, respectively.
If you encounter an error during a network call
let tracker = NetworkCaptureTracker.init(url: "https://example.com", method: "post", requestBodylength: 9130)
tracker.failed(error)
OR
let tracker = NetworkCaptureTracker.init(request: urlRequest)
tracker.failed(error)
Comments
0 comments
Please sign in to leave a comment.