# AppsFlyer 集成

<Aside type="caution" icon="setting" title="需要开发者协助">
 您需要开发团队的帮助来设置此集成。请与他们分享本指南。
</Aside>

## 集成概述

[AppsFlyer](https://www.appsflyer.com/) 是一个移动归因和营销分析平台，可帮助您分析用户获取和参与度。它提供有关应用内用户行为的全面数据，并允许您跟踪用户获取来源。利用从 AppsFlyer 获得的信息，您可以在 Pushwoosh 中创建有效的营销活动。

集成配置完成后，AppsFlyer 将向 Pushwoosh 发送以下信息：

* 用户的应用内行为（作为 Event）及其属性；
* 用户获取来源（作为 _Acquisition Source_ Tag 值）。
### 用例

利用这些数据，您可以在 Pushwoosh 中构建用户细分，并开展精确定向和个性化的营销活动。
以下是利用 AppsFlyer 数据在 Pushwoosh 中创建有效客户旅程的两个示例。

**1. 使用 AppsFlyer 跟踪的事件在 Pushwoosh 中创建高度定向的营销活动。**

假设您有一个新闻应用。您配置了 AppsFlyer 来跟踪 _SubscribedToNews_ 事件，该事件在用户每次订阅新闻频道时触发。此事件具有 category 属性，用于指示用户订阅的新闻类型：_politics_、_business_ 或 _sports_。

Pushwoosh 接收有关此事件及其属性值的信息。您可以根据三个可能的值创建三个用户细分：

<img src="/integrations-appsflyer-integration-1.webp" alt="基于新闻类别属性的用户细分"/>

现在，您可以创建针对对特定新闻类型感兴趣的用户的营销活动：

<img src="/integrations-appsflyer-integration-2.webp" alt="针对对特定新闻类别感兴趣的用户的定向营销活动"/>

**2. 基于 AppsFlyer 的用户行为数据个性化消息。**

在此示例中，我们将使用一个在线购物应用。假设 AppsFlyer 跟踪 _AddToWishlist_ 事件，该事件在用户每次将商品添加到心愿单时触发。此事件具有 item 属性，用于指示添加到心愿单的商品名称。

AppsFlyer 将 _AddToWishlist_ 事件数据发送到 Pushwoosh，您可以在其中根据用户的选择个性化消息。例如，您可以创建一个推送预设，为用户添加到心愿单的商品提供折扣。使用 _item_ 属性的 [动态内容占位符](/zh/developer/guides/personalization/dynamic-content/#personalizing-messages-sent-via-api-with-dynamic-content) 来代替商品名称：

<img src="/integrations-appsflyer-integration-3.webp" alt="Pushwoosh 推送预设中的动态内容占位符"/>

如果用户将商品添加到心愿单，他们将收到一条包含该商品信息的推送。例如，选择 iPhone 14 Pro Max 的用户将看到此消息：

<img src="/integrations-appsflyer-integration-4.webp" alt="iPhone 14 Pro Max 商品优惠的推送通知预览" width="375"/>

## 设置集成

<Aside type="note">
Android 和 iOS 平台的集成过程完全相同。
</Aside>

1. 如果您尚未将 Pushwoosh SDK 集成到您的应用中，请先进行集成。有关更详细的说明，请[阅读本指南](/zh/developer/pushwoosh-sdk/pushwoosh-sdk-overview)。

<Aside type="caution">
为确保正常运行，需要 Pushwoosh iOS SDK 6.5.7 或更高版本以及 Pushwoosh Android SDK 6.6.16 或更高版本。
</Aside>

2. 为确保将正确的数据发送到 AppsFlyer，请将此代码片段集成到您的代码中。确保它在 Pushwoosh SDK 和 AppsFlyer SDK 都初始化之后执行。

<Tabs>
<TabItem label="Java">
```
String pwhwid = Pushwoosh.getInstance().getHwid();
String pwuserid = Pushwoosh.getInstance().getUserId();
Map<String, Object> customData = new HashMap<String, Object>();
customData.put("pushwoosh_hwid", pwhwid);
customData.put("pushwoosh_user_id", pwuserid);
AppsFlyerLib.getInstance().setAdditionalData(customData);
```
</TabItem>

<TabItem label="Objective-C">
```
NSString *pwhwid = [[Pushwoosh sharedInstance] getHWID];
NSString *pwuserid = [[Pushwoosh sharedInstance] getUserId];
NSDictionary *customData = [[NSDictionary alloc] initWithObjectsAndKeys:pwuserid, @"pushwoosh_user_id", pwhwid, @"pushwoosh_hwid", nil];
[[AppsFlyerLib shared] setAdditionalData:customData];
```
</TabItem>
</Tabs>

<Aside type="danger">
当使用自定义 Pushwoosh UserID 值来识别和定位用户时，请确保在 Pushwoosh SDK 中使用自定义值调用 **setUserId()** 方法后再运行该代码。

根据您移动应用中的逻辑跟踪 Pushwoosh UserID 的变化，并及时将更新后的值发送到 AppsFlyer。这对于正确映射与您 Pushwoosh 数据库中用户一起发送的数据非常重要。
</Aside>

3. 在 AppsFlyer 中配置 Pushwoosh 集成。为此，请在您的 [AppsFlyer 账户](https://hq1.appsflyer.com/auth/login)中，转到 **Collaborate** > **Partner Marketplace**。在合作伙伴列表中，找到并选择 Pushwoosh。

<img src="/integrations-appsflyer-integration-5.webp" alt="在 AppsFlyer 中找到 Pushwoosh"/>

4. 点击 **Set up integration**。然后您将被引导至集成设置页面。在此页面，启用 **Activate partner** 开关。

<img src="/integrations-appsflyer-integration-6.webp" alt="在 AppsFlyer 中启用 Activate partner 开关"/>

<Aside type="note">
对于 iOS 应用，默认启用高级隐私模式 (Advanced Privacy Mode)，该模式允许通过 AppsFlyer 平台控制用户级归因数据的可用性。尽管 AppsFlyer 不会将此类数据发送到 Pushwoosh，但保持高级隐私 (Advanced Privacy) 开关激活状态可能会干扰数据传输。

为确保 Pushwoosh 接收所有数据，我们建议在 **App Settings** 中禁用高级隐私模式 (Advanced Privacy Mode)。这样做不会损害用户数据的隐私。Pushwoosh 致力于安全、负责地管理数据，采用行业标准的安全协议来保护机密性。
</Aside>

<img src="/integrations-appsflyer-integration-7.webp" alt="在 AppsFlyer 中禁用高级隐私模式"/>

5. 在 **General settings** 部分，输入您的 Pushwoosh 凭据：

* **API Access Token**。要获取此令牌，请在您的 Pushwoosh 账户中转到 **Settings > API Access** 并复制该代码。您可以[在本指南中](/zh/developer/api-reference/api-access-token)了解有关 API Access Token 的更多信息。
* **Application Code** (XXXXX-XXXXX)，您可以在 Pushwoosh 账户中您的应用名称下方找到它。

<img src="/integrations-appsflyer-integration-8.webp" alt="在 AppsFlyer 常规设置中添加 Pushwoosh 凭据"/>

6. 在 **Default postback** 部分，选择 **All media sources, including organic**。

<img src="/integrations-appsflyer-integration-9.webp" alt="配置 Default postback 部分"/>

7. 在 **In-app event settings** 部分，输入您的 Pushwoosh **Application Code** 和 **API Access Token**。

<img src="/integrations-appsflyer-integration-10.webp" alt="包含 Application Code 和 API 令牌的 In-app event settings 部分"/>

8. 启用 **In-app event postback** 开关，并配置要包含在发送到 Pushwoosh 的回传中的事件和数据。

<img src="/integrations-appsflyer-integration-11.webp" alt="在 AppsFlyer 中配置 In-app event postback 开关"/>

从下拉菜单中选择一个或多个要发送到 Pushwoosh 的 AppsFlyer 事件，并填写必要的参数。

| 参数 | 描述 |
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **the mapped to partner event** | 尽管此字段是必填项，但您无需担心输入的内容，因为它不会改变事件的命名方式。Pushwoosh 仍将接收到与它们在 AppsFlyer 中显示的名称相同的事件。 |
| **for users from** | 在此字段中，选择是仅为归因于此合作伙伴的用户发送回传，还是为所有媒体来源（包括自然来源）发送回传。 |
| **including** | <p>选择如何与 Pushwoosh 共享事件数据：</p><ul><li><strong>No values & no revenue:</strong> 仅发送事件本身，不带任何参数（值或收入）。</li><li><strong>Values & no revenue:</strong> 发送除收入值之外的所有参数。</li><li><strong>Values & revenue:</strong> 发送所有事件参数，包括收入值（如果事件中存在）。<br/>要接收发送到 Pushwoosh 的带有附加属性的事件，请选择 Values and Revenue。</li></ul> |

9. 集成配置完成后，点击 **Save**。

<Aside type="note">
在启动营销活动之前，请务必测试集成。设置过程中经常会发生错误，因此快速测试有助于防止在数据缺失或不正确的营销活动上浪费资源。
</Aside>

## 使用 AppsFlyer ESP 2.0 进行邮件点击跟踪

要在通过 Pushwoosh 发送邮件时使用邮件点击跟踪并保留通用链接 (Universal Links) / 应用链接 (App Links)，您可以设置 [AppsFlyer ESP 2.0 邮件集成](/zh/product/integrations/appsflyer-esp2-email-integration)。