# Analytics Swift Adjust Plugin

[Adjust](https://adjust.com) is the mobile attribution provider of choice for hundreds of organizations across the globe. They unify all your marketing activities into one powerful platform, giving you the insights you need to scale your business. The Adjust Destination is open-source. You can browse the code on GitHub in the [@segment-integrations/analytics-swift-integration-adjust](https://github.com/segment-integrations/analytics-swift-integration-adjust) repo.

> \[!NOTE]
>
> Session data events are sent in Cloud Mode.

## Getting started

1. From the Segment web app, click **Catalog**.
2. Search for "Adjust" in the Catalog, select it, and choose which of your sources to connect the destination to.
   You don't need to include Adjust's SDK natively, as this prevent you from successfully implementing the Adjust.
3. Depending on the source you've selected, include Adjust's library by adding the following lines to your dependency configuration.

## Adding the 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 repo.

[https://github.com/segment-integrations/analytics-swift-integration-adjust](https://github.com/segment-integrations/analytics-swift-integration-adjust)

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-integration-adjust.git",
            from: "1.0.0"
        ),
```

## 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 SegmentAdjust // <-- 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: AdjustDestination())
```

## Identify

Here's an example of the [Identify method](/docs/segment/connections/spec/identify/):

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

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

When you call Identify, Segment calls Adjust's [addSessionPartnerParameter](https://github.com/adjust/ios_sdk#session-partner-parameters) method and sets the `userId` and/or `anonymousId`. This sets these values within Adjust, and allows Adjust to send back attribution data from their servers.

## Track

Here's an example of the [Track method](/docs/segment/connections/spec/track/):

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

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

When you call Track, Segment maps the event to your pre-defined Adjust custom event. You **must** map your Track events to your custom Adjust Event Token in your Adjust destination settings.

If you don't provide a mapping, Adjust cannot accept the event. Segment includes all the event `properties` as callback parameters on the Adjust event, and automatically translates `revenue` and `currency` to the appropriate Adjust event properties based on Segment's [spec'd properties](/docs/segment/connections/spec/track/#properties).

## Install attributed

### Client

Segment will trigger an `Install Attributed` event if you have **trackAttributionData** enabled in your settings and the Segment-Adjust integration installed in your app.

Using Adjust's [Attribution callback](https://github.com/adjust/ios_sdk#attribution-callback), Segment listens for an attribution change from Adjust's SDK and triggers the call with the following Adjust attribution parameters:

| Key                 | Value                    | Description                                        |
| ------------------- | ------------------------ | -------------------------------------------------- |
| provider            | Adjust                   | hardcoded by Segment                               |
| trackerToken        | attribution.trackerToken | the tracker token of the current install           |
| trackerName         | attribution.trackerName  | the tracker name of the current install            |
| campaign.source     | attribution.network      | the network grouping level of the current install  |
| campaign.name       | attribution.campaign     | the campaign grouping level of the current install |
| campaign.content    | attribution.clickLabel   | the click label of the current install             |
| campaign.adCreative | attribution.creative     | the creative grouping level of the current install |
| campaign.adGroup    | attribution.adgroup      | the ad group grouping level of the current install |

If any value is unavailable, it will default to nil. This call will be sent to all enabled [device and cloud mode](/docs/segment/connections/destinations/#connection-modes) destinations.

## Additional features

### Environments

By default, Segment's destination sends data to the Adjust Sandbox Environment. When you release your app to the App Store, enable the `Production` option in the Adjust destination settings on Segment (or use two separate sources, one for dev and one for prod, with different environment settings for Adjust).

### Callback parameters

The destination sends all event `properties` as callback parameters to Adjust. To set [Partner Parameters](https://github.com/adjust/ios_sdk#partner-parameters), you can [access the Adjust SDK directly](https://dev.adjust.com/en/sdk/ios/v4/integrations/segment/).

### Transaction deduplication

The destination will automatically recognize the spec'd `orderId` property, and send it as the transaction ID to Adjust for revenue de-duplication.

### In-app purchase receipts

The destination does not currently support in-app purchase receipts. If this is important to you, [reach out to Segment support](https://segment.com/help/contact/).

### Push notifications

The destination automatically forwards push notification tokens through to Adjust.

### Event buffering

By default, Segment's Adjust destination enables event buffering. This saves your customers' battery life. However, you can disable this in the options on the Adjust destination settings in the Segment app.
