> ## Documentation Index
> Fetch the complete documentation index at: https://docs.notifly.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# AppsFlyerのアトリビューション連携

> AppsFlyerのアトリビューションデータを使い、アプリへの流入経路に応じてキャンペーンの配信対象を絞り込めます。

> [!TIP]
>
> AppsFlyer連携は、NotiflyのProプランとEnterpriseプランで利用できます。

> [!NOTE]
>
> 設定する前に、お使いのプロジェクトで連携を利用できるかをNotiflyにご確認ください。連携データがデータポイントの集計対象になるか、料金がかかるかもあわせてご確認ください。

## 概要

AppsFlyerのインストール時のアトリビューションデータをNotiflyに取り込むと、流入元に応じてキャンペーンの配信対象を絞り込めます。

Notiflyは、AppsFlyerが分析パートナーに送るポストバックを、AppsFlyer IDでユーザーと照合します。アプリでこのIDを取得し、Notiflyのユーザー属性 `$appsflyer_id` に保存してください。

## アトリビューションのフィールド

AppsFlyerのインストールポストバックには、次のフィールドが含まれる場合があります。受信できる項目は、アプリのプライバシー設定と広告ネットワークのデータ共有ポリシーによって異なります。

| フィールド | 説明 |
| - | - |
| `appsflyer_id` | AppsFlyerがアプリのインストールごとに生成する一意のID |
| `app_id` | アプリの識別子 |
| `platform` | プラットフォーム（`ios` または `android`） |
| `country_code` | 国コード（ISO 3166 alpha-2） |
| `media_source` | アトリビューション先のメディアソース |
| `pid` | アトリビューション先の広告ネットワークID |
| `c` | キャンペーン名 |
| `af_ad` | 広告名 |
| `af_ad_id` | 広告ID |
| `af_adset` | 広告セット名 |
| `af_adset_id` | 広告セットID |
| `af_siteid` | パブリッシャーまたはサイトのID |
| `install_time` | インストール時刻 |
| `match_type` | アトリビューションの照合方式 |
| `customer_user_id` | 広告主が指定したユーザーID |
| `advertising_id` | AndroidのGAID |
| `idfa` | iOSのIDFA |

## 連携を設定する

### 1. AppsFlyerでNotiflyとのパートナー連携を有効にする

1. AppsFlyerのサイドメニューで **連携** > **パートナー連携** > **Marketplace** タブを開きます。
2. **Notifly** を検索し、連携設定を開きます。
3. **連携** タブで **パートナーを有効にする** をオンにし、必要な項目を設定します。
4. ポストバックの設定欄がある場合は、インストールのポストバックが有効になっていることを確認します。
5. **連携を保存** をクリックします。

> [!NOTE]
>
> 詳しくは、[AppsFlyerの連携パートナー設定ガイド](https://support.appsflyer.com/hc/ja/articles/4410395957521)を参照してください。

### 2. SDKでAppsFlyer IDを保存する

AppsFlyer SDKはアプリのインストールごとに新しいIDを生成します。`getAppsFlyerUID` でIDを取得し、Notifly SDKの `setUserProperties` でユーザー属性 `$appsflyer_id` に保存します。

### 事前準備

* AppsFlyer SDKをインストールし、初期化します。
* Notifly SDKをインストールし、初期化します。

> [tab] Android (Kotlin)
>
> ```kotlin title="MainApplication.kt" theme={null}
> import android.app.Application
> import com.appsflyer.AppsFlyerLib
> import tech.notifly.Notifly
>
> class MainApplication : Application() {
>     override fun onCreate() {
>         super.onCreate()
>
>         Notifly.initialize(
>             applicationContext,
>             NOTIFLY_PROJECT_ID,
>             NOTIFLY_USERNAME,
>             // 後方互換性のために password 引数が必要です。username と同じ値を指定し、実際のパスワードやAPIのSecret Keyは入れないでください。
>             NOTIFLY_USERNAME
>         )
>
>         AppsFlyerLib.getInstance().init(AF_DEV_KEY, null, this)
>         AppsFlyerLib.getInstance().start(this)
>
>         val appsflyerId = AppsFlyerLib.getInstance().getAppsFlyerUID(this)
>
>         Notifly.setUserProperties(
>             applicationContext,
>             mapOf("\$appsflyer_id" to appsflyerId)
>         )
>     }
> }
> ```
>
> * [AppsFlyer Android `getAppsFlyerUID` のリファレンス](https://dev.appsflyer.com/hc/docs/android-sdk-reference-appsflyerlib#getappsflyeruid)
> * [Notifly Android `setUserProperties` のガイド](https://docs.notifly.tech/ja/developer-guide/android-sdk#ユーザー属性の登録（notifly-setuserproperties-）)

> [tab] iOS (Swift)
>
> ```swift title="AppDelegate.swift" theme={null}
> import AppsFlyerLib
> import notifly_sdk
>
> func onAppLaunch() {
>     let appsflyerId = AppsFlyerLib.shared().getAppsFlyerUID()
>
>     let props: [String: Any] = [
>         "$appsflyer_id": appsflyerId
>     ]
>     Notifly.setUserProperties(userProperties: props)
> }
> ```
>
> * [AppsFlyer iOS `getAppsFlyerUID` のリファレンス](https://dev.appsflyer.com/hc/docs/ios-sdk-reference-appsflyerlib#getappsflyeruid)
> * [Notifly iOS `setUserProperties` のガイド](https://docs.notifly.tech/ja/developer-guide/ios-sdk#2-ユーザー属性の登録)

> [tab] React Native
>
> ```typescript title="App.tsx" theme={null}
> import appsFlyer from 'react-native-appsflyer';
> import notifly from 'notifly-sdk';
>
> export function setAppsFlyerIdToNotifly() {
>   appsFlyer.getAppsFlyerUID((err, appsFlyerUID) => {
>     if (err || !appsFlyerUID) return;
>     notifly.setUserProperties({ $appsflyer_id: appsFlyerUID });
>   });
> }
> ```
>
> 上記のコールバックを使う例は、AppsFlyerのReact Nativeプラグインv6.18.0向けです。v7では、コールバックを渡さずに `getAppsFlyerUID()` を `await` し、取得したIDを `$appsflyer_id` として保存します。
>
> * [AppsFlyer React Native v6.18.0のAPIリファレンス（英語）](https://github.com/AppsFlyerSDK/appsflyer-react-native-plugin/blob/v6.18.0/Docs/RN_API.md#getappsflyeruid)
> * [AppsFlyer React Native v7.0.4のAPIリファレンス（英語）](https://github.com/AppsFlyerSDK/appsflyer-react-native-plugin/blob/v7.0.4/Docs/RN_API.md#getappsflyeruid)
> * [Notifly React Native `setUserProperties` のガイド](https://docs.notifly.tech/ja/developer-guide/react-native-sdk#2-ユーザー属性を設定する)

> [tab] Flutter
>
> ```dart title="main.dart" theme={null}
> import 'package:appsflyer_sdk/appsflyer_sdk.dart';
> import 'package:notifly_flutter/notifly_flutter.dart';
>
> Future<void> setAppsFlyerIdToNotifly(AppsflyerSdk af) async {
>   final appsflyerId = await af.getAppsFlyerUID();
>   if (appsflyerId == null || appsflyerId.isEmpty) return;
>
>   await NotiflyPlugin.setUserProperties({
>     r'$appsflyer_id': appsflyerId,
>   });
> }
> ```
>
> この例はAppsFlyerのFlutterプラグインv6.18.1を使用しています。v7ではクラス名が `AppsFlyerSdk` に変わります。メソッド名は `getAppsFlyerUID()` です。
>
> * [AppsFlyer Flutter v6.18.1のAPIリファレンス（英語）](https://pub.dev/documentation/appsflyer_sdk/6.18.1/appsflyer_sdk/AppsflyerSdk/getAppsFlyerUID.html)
> * [AppsFlyer Flutter v7.0.4のAPIリファレンス（英語）](https://pub.dev/documentation/appsflyer_sdk/7.0.4/appsflyer_sdk/AppsFlyerSdk/getAppsFlyerUID.html)
> * [Notifly Flutter `setUserProperties` のガイド](https://docs.notifly.tech/ja/developer-guide/flutter-sdk#2-ユーザー属性の登録)

## キャンペーンでの活用例

Notiflyでは、連携したアトリビューション属性を配信対象の条件に使えます。

### 配信対象の条件例

* NotiflyにAppsFlyer IDを保存したユーザーを対象にするには、`$appsflyer_id` が設定されていることを条件にします。
* 特定の広告ネットワーク経由で流入したユーザーを抽出するには、`media_source` を使います。たとえば、`media_source = "googleadwords_int"` を指定できます。

### メッセージキャンペーンの例

* 広告キャンペーン名（`c`）を条件に、流入元ごとに異なる初回利用の案内を配信します。案内にはプッシュ通知やアプリ内ポップアップを使えます。

## よくある質問

> [accordion] \$appsflyer\_idを設定しないとどうなりますか？
>
> Notiflyがポストバックをユーザーにひも付けるには、`$appsflyer_id` が必要です。ユーザーグループやキャンペーンで利用できるのは、ユーザーにひも付いたデータだけです。

> [accordion] \$appsflyer\_idを設定する前にインストールのポストバックが届くとどうなりますか？
>
> `$appsflyer_id` の設定前に到着したポストバックはユーザーと照合できません。アプリの起動時に、できるだけ早く `$appsflyer_id` を設定してください。

> [accordion] 連携をテストするにはどうすればよいですか？
>
> 1. アプリを起動して `$appsflyer_id` を設定します。
> 2. Notiflyコンソールで、対象ユーザーの属性に `$appsflyer_id` が保存されていることを確認します。
> 3. [AppsFlyerのSDK実装テストガイド](https://support.appsflyer.com/hc/ja/articles/360001559405)に沿って、登録済みの端末でテストインストールを行います。ユーザーのNotiflyプロフィールにアトリビューションデータが表示されることを確認します。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.