# Attribution

User attribution helps you understand where a user came from before installing your app.

In OnsideKit, attribution links the app install to a prior session in a web browser. The SDK can then return a `refererUrl` that describes the source page.

### Requirements

Attribution depends on early SDK initialization.

Call `Onside.initialize()` as early as possible at app launch. Do this before any other OnsideKit API calls.

If you skip or delay initialization, attribution may fail or return incomplete metadata. See [Installation Guide](https://docs.onside.io/sdk/installation-guide#step-3-initialize-the-sdk).

### Getting attribution metadata

Call `Onside.getAttributionMetadata(completion:)`.

The completion returns either:

* `OnsideAttributionMetadata` on success.
* `OnsideAttributionMetadataError` on failure.

`OnsideAttributionMetadata` contains an optional `refererUrl`.

* `refererUrl != nil` means the SDK attributed the install to a browser session.
* `refererUrl == nil` means the install was organic, or attribution was not possible.

`refererUrl` is the full URL that was loaded in the user’s browser when the install started. This typically is your landing page URL. It can include all query parameters added by an ad network, such as UTM tags and click IDs.

#### API

```swift
Onside.getAttributionMetadata(
    completion: @escaping @MainActor (Result<OnsideAttributionMetadata, OnsideAttributionMetadataError>) -> Void
)
```

#### Example

This example initializes the SDK at launch, then fetches attribution metadata.

```swift
import UIKit
import OnsideKit

@main
final class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        // Call as early as possible for reliable attribution.
        Onside.initialize()

        Onside.getAttributionMetadata { result in
            switch result {
            case .success(let metadata):
                if let refererUrl = metadata.refererUrl {
                    print("Onside refererUrl: \(refererUrl)")
                    // Send refererUrl to your analytics / backend if needed.
                } else {
                    print("No refererUrl. Organic install or attribution unavailable.")
                }

            case .failure(let error):
                print("Failed to fetch attribution metadata: \(error)")
            }
        }

        return true
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.onside.io/sdk/attribution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
