# Integrate with AppsFlyer for Advocate Programs

{% hint style="info" %}
**Tip:** We recommend testing your AppsFlyer integration on a mobile device, not a desktop, to ensure you see the most accurate and updated information.
{% endhint %}

{% stepper %}
{% step %}

### Activate Advocate as an integrated partner

Advocate is an AppsFlyer Integrated Partner (partner id: `saasquatch_int`). To begin, you must activate the partner integration from your dashboard.

1. Open your [AppsFlyer dashboard](https://hq1.appsflyer.com/).
2. Select **Integrated Partners** from the left sidebar menu.
3. Search for the code `SaaSquatch`.
4. Select the `SaaSquatch` partner and enable the **Activate Partner** toggle.
5. Select **Save Integration**.
   {% endstep %}

{% step %}

### Create a OneLink template and link

Follow AppsFlyer's documentation to create a:

* [OneLink template](https://support.appsflyer.com/hc/en-us/articles/207032246-OneLink-templates)
* [OneLink link](https://support.appsflyer.com/hc/en-us/articles/208874366-OneLink-links-and-experiences)

After creating the OneLink link:

1. Copy the Long or Short URL.
2. Replace **"CHANGEME"** in the link with **"saasquatch\_int"**. This is the URL you will add to your Advocate program's integration settings.

For example, if your Long URL is:

```
https://my-one-link-for-saasquatch.onelink.me/JFlJ?pid=CHANGEME&c=MyCampaign
```

You will need to use the following URL in your Advocate program:

```
https://my-one-link-for-saasquatch.onelink.me/JFlJ?pid=saasquatch_int&c=MyCampaign
```

{% endstep %}

{% step %}

### Set up the AppsFlyer integration in impact.com

1. In your impact.com account, from the top navigation bar, select ![](/files/UDyrrRTGbHJFFllCZxyw) **\[User profile]** → [**Settings**](https://app.impact.com/secure/advertiser/account-settings-flow.ihtml?).
2. Under *Advocate Settings*, select **Integrations**.
3. On the AppsFlyer integration card, select **+ \[Expand]**.
4. Select **Enable AppsFlyer**.
5. Add your modified Long or Short URL into the **AppsFlyer Base Attribution Link** field.
6. **Save** the integration configuration.

You are now set up to use AppsFlyer with your Advocate program.
{% endstep %}

{% step %}

### Update your Mobile App

To access the AppsFlyer deep linking data from within your mobile application, you will need to install the AppsFlyer SDK. The [AppsFlyer documentation](https://dev.appsflyer.com/hc/docs/getting-started) contains all the steps required to get their SDK setup on your application.

Once set up, you can retrieve the deep link data from AppsFlyer. This data contains information about the referral and can be used to attribute the referral and customize the signup experience for new users.

AppsFlyer may add other parameters depending on how your OneLink is configured. The most important parameter for your referral program is `_saasquatch` (or the equivalent `deep_link_sub1`, if you are using Unified Deep Linking). It contains the referral code and share link used for the referral, and is needed for attributing the referral. For more detail on data fields and behavior, refer to the [AppsFlyer technical reference](https://integrations.impact.com/impact-brand/docs/appsflyer-technical-reference-advocate).

Here is an example of the referral program data that will be appended to the AppsFlyer OneLink:

{% tabs %}
{% tab title="JSON" %}

```json
{  
  "pid": "saasquatch_int",  
  "c": "saasquatch",  
  "af_web_dp": "http://myReferralLandingPage.com",  
  "deep_link_sub1": "eyJhcHAucmVmZXJyYWxzYWFzcXVhdGNoLmNvbSI6eyJ0ZXN0XzEyMzQ1NjdfQ09ERSI6eyJjb2RlcyI6eyJyZWZlcnJhbCI6IlJFRkVSUkFMQ09ERSJ9LCJjb2Rlc0V4cCI6eyJSRUZFUlJBTENPREUiOjE2Mjk1ODIxOTl9LCJsaW5rcyI6eyJyZWZlcnJhbCI6Imh0dHBzOi8vc3NxdC5jby9temFBMjIifSwibGlua3NFeHAiOnsiaHR0cHM6Ly9zc3F0LmNvL216YUEyMiI6MTYyOTU4MjE5OX19fX0=",  
  "deep_link_sub2": "invite",  
  "deep_link_sub3": "link",  
  "deep_link_sub4": "saasquatch",  
  "deep_link_sub5": "REFERRALCODE",  
  "deep_link_sub6": "UNKNOWN",  
  "deep_link_sub7": "UNKNOWN"  
}
```

{% endtab %}
{% endtabs %}

### Unified Deep Linking on iOS

Prior to implementing deep linking in your mobile application, be sure to follow the AppsFlyer [iOS SDK initial setup documentation](https://dev.appsflyer.com/hc/docs/dl_ios_init_setup) to initialize and implement the SDK in your application and read their [iOS developer documentation](https://dev.appsflyer.com/hc/docs/ios-sdk) for general information on setting up deep linking within your application.

Once you have followed the [steps to set up unified deep linking](https://dev.appsflyer.com/hc/docs/dl_ios_unified_deep_linking) and configured the AppsFlyer integration in Advocate, referral information will be available in the `deep_link_sub1` through `deep_link_sub7` params. Below is a modified code example showing how to extract the `_saasquatch` cookie from the DeepLink data.

{% tabs %}
{% tab title="Swift" %}

```swift
func application(\_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {  
  ...  
  AppsFlyerLib.shared().deepLinkDelegate = self  
  ...  
}

// For Swift version < 4.2 replace function signature with the commented out code  
// func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { // this line for Swift \< 4.2  
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {  
  AppsFlyerLib.shared().continue(userActivity, restorationHandler: nil)  
  return true  
}

// Open URI-scheme for iOS 9 and above  
func application(\_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {  
  AppsFlyerLib.shared().handleOpen(url, options: options)  
  return true  
}

extension AppDelegate: DeepLinkDelegate {  
    func didResolveDeepLink(\_ result: DeepLinkResult) {
    switch result.status {
    case .notFound:
        NSLog("[AFSDK] Deep link not found")
        return
    case .failure:
        print("Error %@", result.error!)
        return
    case .found:
        NSLog("[AFSDK] Deep link found")
    }

    guard let deepLinkObj:DeepLink = result.deepLink else {
        NSLog("[AFSDK] Could not extract deep link object")
        return
    }

    if deepLinkObj.clickEvent.keys.contains("deep_link_sub1") {
        let saasquatchCookie:String = deepLinkObj.clickEvent["deep_link_sub1"] as! String
        NSLog("[AFSDK] AppsFlyer: _saasquatch param: \(saasquatchCookie)")
    } else {
        NSLog("[AFSDK] Could not extract _saasquatch param")
    }

    let deepLinkStr:String = deepLinkObj.toString()
    NSLog("[AFSDK] DeepLink data is: \(deepLinkStr)")

    if( deepLinkObj.isDeferred == true) {
        NSLog("[AFSDK] This is a deferred deep link")
    }
    else {
        NSLog("[AFSDK] This is a direct deep link")
    }

    // ...
  }
}
```

{% endtab %}
{% endtabs %}

### Unified Deep Linking on Android

Prior to implementing deep linking in your mobile application, be sure to follow the AppsFlyer [Android SDK initial setup documentation](https://dev.appsflyer.com/hc/docs/dl_android_init_setup) to initialize and implement the SDK in your application and read their [Android developer documentation](https://dev.appsflyer.com/hc/docs/android-sdk) for general information on setting up deep linking within your application.

Once you have followed the [steps to set up unified deep linking](https://dev.appsflyer.com/hc/docs/dl_android_unified_deep_linking) and configured the Appsflyer integration in Advocate, referral information will be available in the `deep_link_sub1` through `deep_link_sub7` params. Below is a modified code example showing how to extract the `_saasquatch` cookie from the DeepLink data.

{% tabs %}
{% tab title="Java" %}

```java
appsflyer.subscribeForDeepLink(new DeepLinkListener() {
  @Override
  public void onDeepLinking(@NonNull DeepLinkResult deepLinkResult) {
    DeepLinkResult.Status dlStatus = deepLinkResult.getStatus();
    if (dlStatus == DeepLinkResult.Status.FOUND) {
      Log.d(LOG_TAG, "Deep link found");
    } else if (dlStatus == DeepLinkResult.Status.NOT_FOUND) {
      Log.d(LOG_TAG, "Deep link not found");
      return;
    } else {
      // dlStatus == DeepLinkResult.Status.ERROR
      DeepLinkResult.Error dlError = deepLinkResult.getError();
      Log.d(LOG_TAG, "There was an error getting Deep Link data: " + dlError.toString());
      return;
    }

    DeepLink deepLinkObj = deepLinkResult.getDeepLink();
    try {
      Log.d(LOG_TAG, "The DeepLink data is: " + deepLinkObj.toString());
    } catch (Exception e) {
      Log.d(LOG_TAG, "DeepLink data came back null");
      return;
    }

    // An example for using is_deferred
    if (deepLinkObj.isDeferred()) {
      Log.d(LOG_TAG, "This is a deferred deep link");
    } else {
      Log.d(LOG_TAG, "This is a direct deep link");
    }

    if (deepLinkObj.has("deep_link_sub1")) {
      String _saasquatch = deepLinkObj.getStringValue("deep_link_sub1");
      Log.d(LOG_TAG, "The SaaSquatch cookie is: " + _saasquatch);
    } else {
      Log.d(LOG_TAG, "deep_link_sub1/_saasquatch not found");
    }

    // ...
  }
});
```

{% endtab %}
{% endtabs %}
{% endstep %}
{% endstepper %}


---

# 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://integrations.impact.com/integration-guides/for-brands/advocate/mobile-options-for-advocate/appsflyer-for-advocate/integrate-with-appsflyer-for-advocate-programs.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.
