# AppsFlyer連携

[サポートサイト](https://support.karte.io/post/5FHKKheJ0P5k3arzXvtfxg)

***

[AppsFlyer](https://www.appsflyer.com/jp)とデータ連携が可能です。

## セットアップ

以下の手順に従って、それぞれセットアップを行ってください。

## AppsFlyer のセットアップ

AppsFlyerのダッシュボード上で設定が必要です。以下の手順に沿ってセットアップを行ってください。

1. 設定＞連携済みパートナー＞「KARTE」で検索
2. インテグレーション設定で、KARTEのプロジェクトの「api\_key」を入力
3. Default Postbacks＞オプション送信を「Events attributed to any partner or organic」を選択
4. In-App Events Settingで、KARTEのプロジェクトの「api\_key」を入力
5. In-App Events PostbackをONにする
6. KARTEに連携するイベントを指定する

![AppsFlyer設定画面](/files/gm05qQErqFDOQ9TkRsRI)

## KARTE のセットアップ

1. AppsFlyer プラグインの有効化
2. In-Appイベントの連携が必要な場合は、プラグイン設定からIn-App Eventをカンマ区切りで指定してください

![KARTE設定画面](/files/QG1TPZ2xKdW8hQFpEB36)

## AppsFlyerのイベントをKARTEに送信

AppsFlyerで発生したイベントをKARTEに連携するには、AppsFlyer SDKで取得可能な情報をKARTE SDKを使って送信する必要があります。

## インストールイベント

{% tabs %}
{% tab title="Swift" %}
{% code title="AppDelegate.swift" overflow="wrap" %}

```swift
import KarteCore
import AppsFlyerLib

class AppDelegate: UIResponder, UIApplicationDelegate {
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    KarteApp.setup()

    // AppsFlyer SDK の最新の初期化実装に関しては公式ドキュメントも併せてご覧ください
    // see: https://dev.appsflyer.com/hc/docs/integrate-ios-sdk
    AppsFlyerLib.shared().appsFlyerDevKey = "<AF_DEV_KEY>"
    AppsFlyerLib.shared().appleAppID = "<APPLE_APP_ID>"
    AppsFlyerLib.shared().delegate = self
    AppsFlyerLib.shared().start()
    return true
  }
}

extension AppDelegate: AppsFlyerTrackerDelegate {
  func onConversionDataSuccess(_ conversionInfo: [AnyHashable : Any]) {
    let isFirstLaunch = (conversionInfo["is_first_launch"] as? Bool) ?? false
    if (isFirstLaunch) {
      Tracker.track("_appsflyer_install", values: conversionInfo as! [String: JSONConvertible])
    }
  }

  func onConversionDataFail(_ error: Error) {
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Objective-C" %}
{% code title="AppDelegate.m" overflow="wrap" %}

```objectivec
#import <AppsFlyerLib/AppsFlyerLib.h>
@import KarteCore;

@interface AppDelegate () <AppsFlyerLibDelegate>
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  [KRTApp setupWithConfiguration:[KRTConfiguration default]];

  // AppsFlyer SDK の最新の初期化実装に関しては公式ドキュメントも併せてご覧ください
  // see: https://dev.appsflyer.com/hc/docs/integrate-ios-sdk
  [[AppsFlyerLib shared] setAppsFlyerDevKey:@"<AF_DEV_KEY>"];
  [[AppsFlyerLib shared] setAppleAppID:@"<APPLE_APP_ID>"];
  [[AppsFlyerLib shared] setDelegate:self];
  [[AppsFlyerLib shared] start];
  return YES;
}

#pragma mark - AppsFlyerLibDelegate implementations

- (void)onConversionDataSuccess:(NSDictionary *)conversionInfo {
  BOOL isFirstLaunch = [conversionInfo objectForKey:@"is_first_launch"];
  if (isFirstLaunch) {
    [KRTTracker track:@"_appsflyer_install" values:conversionInfo];
  }
}

- (void)onConversionDataFail:(NSError *)error {
}

@end
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code title="MainApplication.kt" overflow="wrap" %}

```kotlin
import android.app.Application
import com.appsflyer.AppsFlyerConversionListener
import com.appsflyer.AppsFlyerLib
import io.karte.android.KarteApp
import io.karte.android.tracking.Tracker

class MainApplication: Application() {
  override fun onCreate() {
    super.onCreate()

    KarteApp.setup(this)

    // AppsFlyer SDK の最新の初期化実装に関しては公式ドキュメントも併せてご覧ください
    // see: https://dev.appsflyer.com/hc/docs/integrate-android-sdk
    AppsFlyerLib.getInstance().init("<AF_DEV_KEY>", object: AppsFlyerConversionListener {
      override fun onConversionDataSuccess(map: MutableMap<String, Any>?) {
        val isFirstLaunch = map?.get("is_first_launch") as? Boolean ?: false
        if (isFirstLaunch) {
          Tracker.track("_appsflyer_install", map)
        }
      }

      override fun onConversionDataFail(p0: String?) {
      }

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

      override fun onAttributionFailure(p0: String?) {
      }
    }, this)
    AppsFlyerLib.getInstance().start(this)
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code title="MainApplication.java" overflow="wrap" %}

```java
import android.app.Application;
import com.appsflyer.AppsFlyerConversionListener;
import com.appsflyer.AppsFlyerLib;
import java.util.Map;
import io.karte.android.KarteApp;
import io.karte.android.tracking.Tracker;

public class MainApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();

    KarteApp.setup(this);

    // AppsFlyer SDK の最新の初期化実装に関しては公式ドキュメントも併せてご覧ください
    // see: https://dev.appsflyer.com/hc/docs/integrate-android-sdk
    AppsFlyerConversionListener conversionListener = new AppsFlyerConversionListener() {
      @Override
      public void onConversionDataSuccess(Map<String, Object> map) {
        if ("true".equals(map.get("is_first_launch"))) {
          Tracker.track("_appsflyer_install", map);
        }
      }

      @Override
      public void onConversionDataFail(String s) {
      }

      @Override
      public void onAppOpenAttribution(Map<String, String> map) {
      }

      @Override
      public void onAttributionFailure(String s) {
      }
    };
    AppsFlyerLib.getInstance().init("<AF_DEV_KEY>", conversionListener, this);
    AppsFlyerLib.getInstance().start(this);
  }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## In-App イベント

{% tabs %}
{% tab title="Swift" %}
{% code overflow="wrap" %}

```swift
AppsFlyerLib.shared().logEvent("EVENT_NAME", withValues: ["visitor_id": KarteApp.visitorId])
```

{% endcode %}
{% endtab %}

{% tab title="Objective-C" %}
{% code overflow="wrap" %}

```objectivec
[[AppsFlyerLib shared] logEvent:@"EVENT_NAME" withValues:@{@"visitor_id": [KRTApp visitorId]}];
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code overflow="wrap" %}

```kotlin
AppsFlyerLib.getInstance().logEvent(context, "EVENT_NAME", mapOf("visitor_id" to KarteApp.visitorId))
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code overflow="wrap" %}

```java
Map<String, Object> eventValue = new HashMap<>();
eventValue.put("visitor_id", KarteApp.getVisitorId());
AppsFlyerLib.getInstance().logEvent(context, "EVENT_NAME", eventValue);
```

{% endcode %}
{% endtab %}
{% endtabs %}

## イベント名とパラメータの対応表

KARTEに連携される AppsFlyer イベントと、KARTEで発生するイベント名およびパラメータの対応表を以下に記載します。

| AppsFlyerイベント |      KARTEで発生するイベント名     |                                                                                                                                                               KARTEイベントに設定されるパラメータ                                                                                                                                                               |                                                           備考                                                          |
| :-----------: | :----------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------: |
|    Install    |   \_appsflyer\_install   |                                                                                                                                [こちら](https://dev.appsflyer.com/hc/docs/ios-legacy-apis#deferred-deep-linking)を参考に                                                                                                                                |                                                           -                                                           |
|  In-app event | *appsflyer*{event\_name} | <p><a href="https://support.appsflyer.com/hc/ja/articles/207273946-%E3%83%9D%E3%82%B9%E3%83%88%E3%83%90%E3%83%83%E3%82%AF%E5%8F%AF%E8%83%BD%E3%81%AA%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%81%A8AppsFlyer%E3%81%AB%E3%81%8A%E3%81%91%E3%82%8B%E3%83%9E%E3%82%AF%E3%83%AD">こちら</a>を参考に +<br>AppsFlyer SDK でイベント送信時に設定した Event Value</p> | <p>・{event\_name} には AppsFlyer ダッシュボードで設定したKARTEに送信するイベント名が入ります。<br>・In-app Event の種類に応じて異なる {event\_name} が入ります。</p> |

## パラメータについての補足

AppsFlyer イベントで定義されている一部のパラメータは、 KARTE イベントでは異なる名前で連携されます。（KARTE 上で日付型のパラメータとして利用できるようにするため）

| AppsFlyer イベントでのパラメータ名 | KARTEイベントでのパラメータ名   |
| ---------------------- | ------------------- |
| click\_ts              | click\_date         |
| install\_unix\_ts      | install\_unix\_date |
| timestamp              | timestamp\_date     |

{% hint style="warning" %}
**広告IDについて**

AppsFlyer連携で、広告ID（IDFA/AAID）はKARTEに連携されません。広告IDをKARTEに連携したい場合には、[広告ID（IDFA/AAID）の送信について](https://support.karte.io/post/13v6pgNyNKtrxyIvYZcnAs)をご確認ください。
{% endhint %}


---

# 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://app.developers.karte.io/app-external-connection/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.
