# Analytics Swift AppsFlyer Plugin

AppsFlyer is the world's leading mobile attribution & marketing analytics platform, helping app marketers around the world make better decisions. The AppsFlyer destination code is open-source. You can browse the code on GitHub for iOS and Android.

Segment's AppsFlyer destination plugin code is open source and available on GitHub. You can view it [here.](https://github.com/segment-integrations/analytics-swift-appsflyer)

## Getting started

1. From the Segment web app, click **Catalog**.
2. Search for "AppsFlyer" in the Catalog, select it, and choose which of your sources to connect the destination to.
3. In the destination settings, enter your `AppsFlyer Dev Key`, which can be retrieved from the App Settings section of your AppsFlyer account.
4. After you build and release to the app store, Segment starts translating and sending your data to AppsFlyer automatically.

## Adding the dependency

> \[!WARNING]
>
> the AppsFlyer library itself will be installed as an additional dependency.

### Xcode

In the Xcode `File` menu, click `Add Packages`. You'll see a dialog where you can search for Swift packages. In the search field, enter the URL to this repository.

```text
        https://github.com/segment-integrations/analytics-swift-AppsFlyer
```

You'll then have the option to pin to a version, or specific branch, as well as which project in your workspace to add it to. Once you've made your selections, click the `Add Package` button.

### Package.swift

Open your Package.swift file and add the following do your the `dependencies` section:

```swift
.package(
            name: "Segment",
            url: "https://github.com/segment-integrations/analytics-swift-appsflyer.git",
            from: "1.1.3"
        ),
```

## Using the plugin in your app

Open the file where you setup and configure the Analytics-Swift library. Add this plugin to the list of imports.

```swift
import Segment
import SegmentAppsFlyer // <-- Add this line
```

Just under your Analytics-Swift library setup, call `analytics.add(plugin: ...)` to add an instance of the plugin to the Analytics timeline.

```swift
let analytics = Analytics(configuration: Configuration(writeKey: "<YOUR WRITE KEY>")
                    .flushAt(3)
                    .trackApplicationLifecycleEvents(true))
analytics.add(plugin: AppsFlyerDestination())
```

Your events will now begin to flow to AppsFlyer in device mode.

## Identify

If you're not familiar with the Segment Specs, take a look to understand what the [Identify method](/docs/segment/connections/spec/identify/) does. An example iOS call would look like:

```swift
struct MyTraits: Codable {
        let favoriteColor: String
}

analytics.identify(userId: "a user's id", MyTraits(favoriteColor: "fuscia"))
```

When you call Identify, Segment uses AppsFlyer's `setCustomerUserID` to send the Segment `userId` you passed in.

> \[!WARNING]
>
> You can only send Identify calls if you have the AppsFlyer SDK bundled.

## Track

If you're not familiar with the Segment Specs, take a look to understand what the [Track method](/docs/segment/connections/spec/track/) does. An example iOS call would look like:

```swift
struct TrackProperties: Codable {
        let someValue: String
}

analytics.track(name: "My Event", properties: TrackProperties(someValue: "Hello"))
```

When you call Track, Segment automatically translates the event and sends it to AppsFlyer.

Segment includes all the event properties as callback parameters on the AppsFlyer event, and translates `properties.revenue` to the appropriate AppsFlyer purchase event properties based on the spec-matching properties.

Segment uses AppsFlyer's `transactionId` deduplication when you send an `orderId` (see Segment's [e-commerce spec](/docs/segment/connections/spec/ecommerce/v2/) for more details).

## Install attributed

Segment automatically triggers an `Install Attributed` event if you have **trackAttributionData** enabled in your settings and the Segment-AppsFlyer integration installed in your app. The event payload will adhere to the `Install Attributed` event specification documented [in Segment's Mobile Spec](/docs/segment/connections/spec/mobile/#install-attributed) and will propagate to your other downstream destinations.

## Revenue tracking

The AppsFlyer destination automatically recognizes spec-matching `revenue` property and translates them to AppsFlyer's revenue tracking method.

## Transaction de-duplication

The AppsFlyer destination automatically recognizes the spec-matching `orderId` property, and sends it as the Transaction ID to AppsFlyer for revenue de-duplication.

## In-app purchase receipts

The AppsFlyer destination does not currently support in-app purchase receipts. If this is important to you, email [support@appsflyer.com](mailto:support@appsflyer.com).

## Deeplinking

The AppsFlyer destination does not automatically support out-of-the-box deeplinking (you need to write code here regardless!).

You can use AppsFlyer's OneLink integration which is a single, smart, tracking link that can be used to track on both Android and iOS. OneLink tracking links can launch your app when it is already installed instead of redirecting the user to the app store.

For more details, review the [AppsFlyer OneLink set up Guide](https://support.appsflyer.com/hc/en-us/articles/207032246-OneLink-Setup-Guide). More information is available in the AppsFlyer SDK Integration Guides ([iOS](https://support.appsflyer.com/hc/en-us/articles/207032066-AppsFlyer-SDK-Integration-iOS), [Android](https://support.appsflyer.com/hc/en-us/articles/207032126-AppsFlyer-SDK-Integration-Android)) and Segment's mobile FAQs ([iOS](/docs/segment/connections/sources/catalog/libraries/mobile/ios/#faq), [Android](/docs/segment/connections/sources/catalog/libraries/mobile/android/#faq)).
