Skip to main content

Recommendations API

For programmatic access to content recommendations without UI:


Sample App

The official Teads SDK sample application includes working examples of this format. Use it for implementation examples and as a reference while you set up this placement.

Teads iOS Sample App on GitHub

Example​

import TeadsSDK

class RecommendationsManager {

private var recommendationsPlacement: TeadsAdPlacementRecommendations?

func fetchRecommendations() async throws -> [OBRecommendation] {
// Create configuration
let config = TeadsAdPlacementRecommendationsURLConfig(
articleUrl: URL(string: "https://example.com/article/123")!,
widgetId: "SDK_1",
widgetIndex: 0
)

// Create placement
recommendationsPlacement = TeadsAdPlacementRecommendations(config, delegate: nil)

// Fetch recommendations
let loader = try recommendationsPlacement?.loadAd()
let recommendations = try await loader?()
return recommendations ?? []
}

// Custom UI for recommendations
func createCustomRecommendationView(for recommendation: OBRecommendation) -> UIView {
let view = UIView()

// Add image
let imageView = UIImageView()
if let imageUrl = URL(string: recommendation.imageUrl ?? "") {
// Load image asynchronously
URLSession.shared.dataTask(with: imageUrl) { data, _, _ in
if let data = data, let image = UIImage(data: data) {
DispatchQueue.main.async {
imageView.image = image
}
}
}.resume()
}

// Add title
let titleLabel = UILabel()
titleLabel.text = recommendation.content
titleLabel.font = .systemFont(ofSize: 16, weight: .semibold)
titleLabel.numberOfLines = 2

// Add source
let sourceLabel = UILabel()
sourceLabel.text = recommendation.source
sourceLabel.font = .systemFont(ofSize: 12)
sourceLabel.textColor = .secondaryLabel

// Layout views...

// Make the whole view tappable — see "Handling clicks" below.
// A gesture recognizer (rather than a full-size button) lets child buttons,
// such as the RTB disclosure icon, keep receiving their own taps.
view.addGestureRecognizer(ClosureTapGestureRecognizer { [weak self] in
self?.openRecommendation(recommendation)
})

// Configure viewability tracking — required for accurate impression reporting
TeadsAdPlacementRecommendations.configureViewabilityPerListing(
for: view,
withRec: recommendation
)

return view
}
}

// UITapGestureRecognizer has no closure API, so this small helper runs a closure on tap
final class ClosureTapGestureRecognizer: UITapGestureRecognizer {
private let handler: () -> Void

init(handler: @escaping () -> Void) {
self.handler = handler
super.init(target: nil, action: nil)
addTarget(self, action: #selector(fire))
}

@objc private func fire() {
handler()
}
}

Viewability Tracking​

Call configureViewabilityPerListing(for:withRec:) for each recommendation view as you add it to your layout. Pass the view that displays the recommendation and its corresponding OBRecommendation:

// Call for each recommendation view once it is in the view hierarchy
TeadsAdPlacementRecommendations.configureViewabilityPerListing(
for: recommendationView,
withRec: recommendation
)

Once configured, the SDK tracks the view and automatically reports viewable impressions to Teads — no additional work is required.

AdChoices compliance​

Widget AdChoices icon​

Your recommendations widget must display a clickable AdChoices icon — a UIButton at least 30×30 pt, positioned above the widget and hidden until recommendations load. When tapped, open the URL returned by TeadsAdPlacementRecommendations.getAboutURL() in Safari or an SFSafariViewController:

import SafariServices

if let url = TeadsAdPlacementRecommendations.getAboutURL() {
let safari = SFSafariViewController(url: url)
present(safari, animated: true)
}

RTB disclosure icon (paid recommendations)​

Paid (RTB) recommendations require a per-item disclosure icon overlaid on the thumbnail. Add a UIButton (minimum 30×30 pt) above each recommendation image, hidden by default.

For each recommendation, check shouldDisplayDisclosureIcon(). If it returns true, reveal the button, load its icon from disclosure.imageUrl, and open disclosure.clickUrl on tap; otherwise keep it hidden so it never shows on organic recommendations:

if recommendation.shouldDisplayDisclosureIcon() {
disclosureButton.isHidden = false
// Load recommendation.disclosure?.imageUrl and set it as the button image
// On tap, open recommendation.disclosure?.clickUrl in Safari or an SFSafariViewController
} else {
disclosureButton.isHidden = true
}

Paid recommendations always appear in responses when test mode is enabled:

TeadsAdPlacementRecommendations.testRTB = true

Handling clicks​

Every recommendation is either organic or paid, and the two are opened differently:

OrganicPaid
What it isYour own contentA sponsored link to a third-party site
How to tellisPaidLink is falseisPaidLink is true
getUrl(_:) returnsThe article's original URLA paid.outbrain.com redirect URL
Click is countedBy getUrl(_:) itselfWhen the returned URL is opened
Open it inA native view inside your appSFSafariViewController or Safari
danger

Call getUrl(_:) only after the tap. For organic recommendations, getUrl(_:) reports the click itself, so calling it before the tap reports a false click on the recommendation. For paid recommendations, the click is counted when the returned URL is opened.

1) Call getUrl(_:) first​

When a user taps a recommendation, call TeadsAdPlacementRecommendations.getUrl(_:) to get the URL to open:

info

For paid recommendations, the returned URL is in paid.outbrain.com redirect format. For organic recommendations it's the article's original URL when available. Open the URL as is, without any manipulation such as escaping or decoding.

2) Navigate to the recommendation​

Check whether it is a paid recommendation by reading isPaidLink (it is false for organic recommendations), then navigate as follows:

Recommendation click flow
  • Organic recommendations (isPaidLink is false) — open the URL in your app, using your standard navigation mechanism (for example, your own article screen or an in-app web view).
  • Paid recommendations (isPaidLink is true) — open the URL in an SFSafariViewController (or the external Safari browser).

The following example puts the steps together:

import SafariServices

extension RecommendationsManager {
// Called from the tap action in createCustomRecommendationView(for:) above
func openRecommendation(_ recommendation: OBRecommendation) {
// For organic links getUrl reports the click, so only call it after the tap
guard let url = TeadsAdPlacementRecommendations.getUrl(recommendation) else { return }

if recommendation.isPaidLink {
// Paid: open in SFSafariViewController
guard let presenter = topViewController() else { return }
presenter.present(SFSafariViewController(url: url), animated: true)
} else {
// Organic: open in the app, i.e. by calling a function that handles opening articles natively
presentArticle(url)
}
}
}

topViewController() and presentArticle(_:) are placeholders for your own code: how your app finds the view controller to present from, and how it opens an article natively. Adjust them to your needs.

Best Practices​

Viewability

Viewability tracking is required for accurate impression reporting.

Click handling

Call getUrl(_:) only after the user taps a recommendation, and open the returned URL as is.

AdChoices

The widget AdChoices icon and the per-item disclosure icon on paid recommendations are required to ship.

RTB test mode

Remember to remove the testRTB = true call before releasing to production.

Ad Lifecycle Listener​

Implement TeadsAdPlacementEventsDelegate to observe playback, clicks, and viewability. See Placement Events Listener for the full event reference and error handling guide.

Checklist​