> ## 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.

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

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

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

## 概要

Airbridgeのアトリビューション結果をNotiflyのユーザー属性に保存します。保存したチャネルやキャンペーンの値を、配信対象の条件に使えます。

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

| フィールド | 説明 |
| - | - |
| `attributedChannel` | チャネル |
| `attributedCampaign` | キャンペーン |
| `attributedAdGroup` | 広告グループ |
| `attributedAdCreative` | 広告クリエイティブ |
| `attributedContent` | コンテンツ |
| `attributedTerm` | キーワード |
| `attributedSubPublisher` | サブパブリッシャー |

## 連携を設定する

Airbridge SDKのコールバックでアトリビューション結果を受け取ります。コールバック内でNotifly SDKの `setUserProperties` を呼び出し、必要なフィールドをユーザー属性に保存します。

> [!NOTE]
>
> コード例の `password` 引数は、Notifly SDKの後方互換性のために残っています。`username` と同じ値を指定し、空欄にしないでください。実際のパスワードやAPIのSecret Keyはアプリのコードに入れないでください。

### 事前準備

Airbridge SDKとNotifly SDKをインストールします。以下のコールバックをアプリのSDK設定に追加してください。AndroidとiOSでは、Airbridgeの初期化オプションにコールバックを登録します。コールバックが実行される前にNotiflyを初期化してください。

> [tab] Android (Kotlin)
>
> ```kotlin title="MainApplication.kt" theme={null}
> import android.app.Application
> import co.ab180.airbridge.Airbridge
> import co.ab180.airbridge.AirbridgeOptionBuilder
> import co.ab180.airbridge.listener.OnAttributionResultReceiveListener
> import tech.notifly.Notifly
>
> class MainApplication : Application() {
>     override fun onCreate() {
>         super.onCreate()
>
>         Notifly.initialize(applicationContext, "PROJECT_ID", "USERNAME", "PASSWORD")
>
>         val option = AirbridgeOptionBuilder("YOUR_APP_NAME", "YOUR_APP_SDK_TOKEN")
>             .setOnAttributionReceived(object : OnAttributionResultReceiveListener {
>                 override fun onAttributionResultReceived(result: Map<String, String>) {
>                     val properties = mutableMapOf<String, Any>()
>                     result["attributedChannel"]?.let { properties["attributedChannel"] = it }
>                     result["attributedCampaign"]?.let { properties["attributedCampaign"] = it }
>                     result["attributedAdGroup"]?.let { properties["attributedAdGroup"] = it }
>
>                     if (properties.isNotEmpty()) {
>                         Notifly.setUserProperties(applicationContext, properties)
>                     }
>                 }
>             })
>             .build()
>
>         Airbridge.initializeSDK(this, option)
>     }
> }
> ```

> [tab] iOS (Swift)
>
> ```swift theme={null}
> import notifly_sdk
> import Airbridge
>
> func application(_ application: UIApplication,
>                  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
>
>   Notifly.initialize(projectId: "PROJECT_ID", username: "USERNAME", password: "PASSWORD")
>
>   let option = AirbridgeOptionBuilder(name: "YOUR_APP_NAME", token: "YOUR_APP_SDK_TOKEN")
>     .setOnAttributionReceived { attribution in
>       var props: [String: Any] = [:]
>       if let v = attribution["attributedChannel"]  { props["attributedChannel"] = v }
>       if let v = attribution["attributedCampaign"] { props["attributedCampaign"] = v }
>       if let v = attribution["attributedAdGroup"]  { props["attributedAdGroup"] = v }
>
>       if !props.isEmpty {
>         Notifly.setUserProperties(userProperties: props)
>       }
>     }
>     .build()
>
>   Airbridge.initializeSDK(option: option)
>   return true
> }
> ```

> [tab] React Native
>
> ```javascript title="index.js (or App.js)" theme={null}
> import notifly from 'notifly-sdk';
> import { Airbridge } from 'airbridge-react-native-sdk';
>
> notifly.initialize({
>   projectId: 'PROJECT_ID',
>   username: 'USERNAME',
>   password: 'PASSWORD',
> });
>
> Airbridge.setOnAttributionReceived((attribution) => {
>   const props = {};
>
>   if (attribution?.attributedChannel) props.attributedChannel = attribution.attributedChannel;
>   if (attribution?.attributedCampaign) props.attributedCampaign = attribution.attributedCampaign;
>   if (attribution?.attributedAdGroup) props.attributedAdGroup = attribution.attributedAdGroup;
>
>   if (Object.keys(props).length) {
>     notifly.setUserProperties(props);
>   }
> });
> ```

> [tab] Flutter
>
> 以下のアトリビューション設定を既存の `main()` に追加します。`runApp()` など、アプリの既存の起動処理はそのまま使います。`WidgetsFlutterBinding` を使うには、`package:flutter/widgets.dart` をインポートします。
>
> ```dart theme={null}
> import 'package:notifly_flutter/notifly_flutter.dart';
> import 'package:airbridge_flutter_sdk/airbridge_flutter_sdk.dart';
>
> Future<void> main() async {
>   WidgetsFlutterBinding.ensureInitialized();
>
>   await NotiflyPlugin.initialize(
>     projectId: 'PROJECT_ID',
>     username: 'USERNAME',
>     password: 'PASSWORD',
>   );
>
>   Airbridge.setOnAttributionReceived((Map<String, String> attribution) async {
>     final props = <String, Object>{};
>
>     final c = attribution['attributedChannel'];
>     final p = attribution['attributedCampaign'];
>     final g = attribution['attributedAdGroup'];
>
>     if (c != null) props['attributedChannel'] = c;
>     if (p != null) props['attributedCampaign'] = p;
>     if (g != null) props['attributedAdGroup'] = g;
>
>     if (props.isNotEmpty) {
>       await NotiflyPlugin.setUserProperties(props);
>     }
>   });
> }
> ```

## キャンペーンの設定例

* `attributedChannel` が `Facebook` のユーザーに、初回利用を案内するメッセージを送ります。
* `attributedCampaign` が特定のプロモーションに該当するユーザーに、次のキャンペーンを案内します。
* 広告チャネルにひも付いたユーザーと、`attributedChannel` が `unattributed` のユーザーには、別々のメッセージを送ります。

## よくある質問

> [accordion] アトリビューションデータはいつ収集されますか？
>
> Airbridge SDKはインストールのアトリビューション結果をコールバックで返します。コード例では、受け取った値をNotiflyのユーザー属性に保存します。アプリの起動後に結果が届くまで時間がかかることがあります。受信前にアプリが終了した場合は、次回の起動時に届くことがあります。

> [accordion] すでにアプリをインストールしたユーザーのデータも収集できますか？
>
> Airbridge SDKを導入する前にアプリをインストールしたユーザーのデータは、この連携でさかのぼって取り込めません。

> [accordion] オーガニックインストールではどの値が返りますか？
>
> Airbridge SDK v4では、アトリビューション結果がない場合に `attributedChannel` が `unattributed` になります。コード例では、値が返ったフィールドだけをNotiflyに保存します。

> [accordion] 属性名は変更できますか？
>
> はい。Notiflyのユーザー属性に保存するときの名前は、`attributedChannel` や `attributedCampaign` から変更できます。変更後は、キャンペーンの条件でも同じ属性名を指定してください。


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