By default, SpendOwl uses an anonymous device ID to track attribution and purchases. When you set a user ID, all events are associated with that user, enabling:
Call setUserId() after the user logs in or registers:
Copy
// After successful authenticationSpendOwl.setUserId(user.id)
SwiftUI
UIKit
Copy
struct LoginView: View { @StateObject private var viewModel = LoginViewModel() var body: some View { // ... login UI }}class LoginViewModel: ObservableObject { func login(email: String, password: String) async throws { let user = try await AuthService.login(email: email, password: password) // Set SpendOwl user ID SpendOwl.setUserId(user.id) // Continue with login flow }}
Copy
class LoginViewController: UIViewController { func login(email: String, password: String) { Task { do { let user = try await AuthService.login(email: email, password: password) // Set SpendOwl user ID SpendOwl.setUserId(user.id) // Navigate to main app navigateToHome() } catch { showError(error) } } }}
Use your internal database ID or UUID, not email addresses or usernames which can change.
Copy
// GoodSpendOwl.setUserId(user.id) // "usr_abc123"// AvoidSpendOwl.setUserId(user.email) // Can change
Set Early, Clear on Logout
Set the user ID as soon as authentication succeeds. Clear it immediately on logout.
Copy
// Set on any successful authfunc onAuthSuccess(user: User) { SpendOwl.setUserId(user.id)}// Clear on any logout pathfunc onLogout() { SpendOwl.clearUserId()}
Handle Session Restoration
Set the user ID when restoring a session from keychain/token:
Copy
func restoreSession() async { guard let user = try? await AuthService.restoreSession() else { return } SpendOwl.setUserId(user.id)}
// Get attribution with user contextSpendOwl.setUserId("user-123")let attribution = try await SpendOwl.attribution()// Attribution is now linked to "user-123"