# How can I connect Batch to AppsFlyer?

[AppsFlyer](https://www.appsflyer.com/fr/) is a mobile advertising attribution and analytics company. AppsFlyer helps marketers to pinpoint their targeting, optimize their ad spend and boost their ROI.&#x20;

On the other hand, Batch can receive information from AppsFlyer using their callbacks attribution. This will allow you to re-target your users with notifications based on the installation source.

\
​

## Understanding how the connection works <a href="#understanding-how-the-connection-works" id="understanding-how-the-connection-works"></a>

Callbacks provided by AppsFlyer SDK allow you to sync AppsFlyer data with Batch SDK. You will be able to use Batch segmentation to target users based on install source attribution data.

## iOS integration <a href="#ios-integration" id="ios-integration"></a>

After setting up the AppsFlyer attribution callback (iOS [instructions](https://dev.appsflyer.com/hc/docs/conversion-data-ios) on callbacks), you need to send Batch the appropriate data via the SDK's method.

\
Here is an example of how you can set the callback in your app code (using **Batch V2 SDK**):\
​

```
import AppsFlyerLib
import Batch

extension AppDelegate: AppsFlyerLibDelegate {
    func onConversionDataSuccess(_ conversionData: [AnyHashable : Any]) {
        BatchProfile.editor { editor in
            if let mediaSource = conversionData["media_source"] as? String {
                try? editor.set(attribute: mediaSource, forKey: "appsflyer_source")
            }
            if let campaignId = conversionData["campaign"] as? String {
                try? editor.set(attribute: campaignId, forKey: "appsflyer_campaign")
            }
            if let adSet = conversionData["af_adset"] as? String {
                try? editor.set(attribute: adSet, forKey: "appsflyer_adset")
            }
        }
    }
    
    func onConversionDataFail(_ error: any Error) {
    }
}
```

## Android integration <a href="#android-integration" id="android-integration"></a>

Similarly, you need to set up the AppsFlyer attribution callback (Android [instructions](https://dev.appsflyer.com/hc/docs/conversion-data-android)) and send Batch the attribution data via the SDK's method.

Here is an example of how you can set the callback in your app code (using **Batch V2 SDK**):

```
AppsFlyerLib.getInstance().registerConversionListener(this, object:
    AppsFlyerConversionListener {
    override fun onConversionDataSuccess(conversionData: MutableMap<String, Any>?) {
        if (conversionData == null) {
            return
        }

        Batch.Profile.editor().apply {
            (conversionData["campaign"] as? String)?.let {
                setAttribute("appsflyer_campaign", it)
            }

            (conversionData["media_source"] as? String)?.let {
                setAttribute("appsflyer_source", it)
            }

            (conversionData["af_adset"] as? String)?.let {
                setAttribute("appsflyer_adset", it)
            }

            save()
        }

    }

    override fun onConversionDataFail(error: String?) {
    }

    override fun onAppOpenAttribution(data: MutableMap<String, String>?) {
    }

    override fun onAttributionFailure(error: String?) {
    }

})
```

## What’s next? <a href="#whats-next" id="whats-next"></a>

Now, you can test your integration using our [Profile view ](/getting-started/features/customer-engagement-platform/profiles/search-profiles.md)and find out if your data work properly on the Batch dashboard.&#x20;

Once your tests are finished, you will be able to specify an installation source and more from the campaign editor:

<figure><img src="/files/2Q3DyIHwSenRejkLdQm6" alt="Specify an installation source in the campaign editor"><figcaption><p>Specify an installation source in the campaign editor</p></figcaption></figure>

It's now super easy to target users based on attribution 😉.


---

# 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://doc.batch.com/integrations/attribution/how-can-i-connect-batch-to-appsflyer.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.
