Skip to main content

Overview

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:
  • User-level ROAS analysis
  • Cohort tracking across devices
  • Accurate LTV calculation

Setting User ID

Call setUserId() after the user logs in or registers:
// After successful authentication
SpendOwl.setUserId(user.id)
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
    }
}

Clearing User ID

Call clearUserId() when the user logs out:
func logout() {
    // Clear SpendOwl user ID
    SpendOwl.clearUserId()

    // Clear other user data
    AuthService.logout()
}

Best Practices

Use your internal database ID or UUID, not email addresses or usernames which can change.
// Good
SpendOwl.setUserId(user.id)  // "usr_abc123"

// Avoid
SpendOwl.setUserId(user.email)  // Can change
Set the user ID as soon as authentication succeeds. Clear it immediately on logout.
// Set on any successful auth
func onAuthSuccess(user: User) {
    SpendOwl.setUserId(user.id)
}

// Clear on any logout path
func onLogout() {
    SpendOwl.clearUserId()
}
Set the user ID when restoring a session from keychain/token:
func restoreSession() async {
    guard let user = try? await AuthService.restoreSession() else {
        return
    }
    SpendOwl.setUserId(user.id)
}

Anonymous ID Behavior

When no user ID is set, SpendOwl uses an anonymous ID:
  • Generated on first launch
  • Stored securely in Keychain
  • Persists across app reinstalls (if Keychain is preserved)
  • Replaced by user ID when setUserId() is called

User ID in Attribution

The user ID is included with attribution data:
// Get attribution with user context
SpendOwl.setUserId("user-123")
let attribution = try await SpendOwl.attribution()
// Attribution is now linked to "user-123"

User ID in Purchases

Purchases are automatically tagged with the current user ID:
SpendOwl.setUserId("user-123")

// Any StoreKit 2 purchase is now linked to "user-123"
let result = try await product.purchase()
If the user ID changes during a purchase, the ID at transaction time is used.

Multiple Accounts

If your app supports multiple accounts:
func switchAccount(to user: User) {
    // Clear previous user
    SpendOwl.clearUserId()

    // Set new user
    SpendOwl.setUserId(user.id)
}

Privacy Considerations

  • User IDs are transmitted securely over HTTPS
  • IDs are hashed before storage in SpendOwl’s database
  • You can request user data deletion through the dashboard

Next Steps