# Amplitude

Level up your behavioral cohort analysis with user-level mobile data.

Airship [Real-Time Data Streaming](https://www.airship.com/docs/reference/glossary/#rtds) (RTDS) delivers user-level information about push sends, conversions, and uninstall events. Adding Airship's unique information
to Amplitude gives you a full view of what's going on in and around your app.

By adding user-level mobile engagement data to your Amplitude data, you can
leverage
[Amplitude Behavioral Cohorts](https://amplitude.com/behavioral-cohorts) for
targeting via Airship.

You can also leverage Amplitude's behavioral cohorts in Airship as [Tags](https://www.airship.com/docs/reference/glossary/#tag).


## Amplitude Integration Requirements

* **Accounts**
	1. Amplitude — Enterprise Account
	1. Airship
		* Messaging
		* [Real-Time Data Streaming](https://www.airship.com/docs/reference/feature-packages/#data) — *Required for outbound integration only*
* **Airship project**
	* The Airship SDK must use the same user identity as the Amplitude SDK.

## Inbound Integration

The inbound integration applies Amplitude user information to Airship users as [Tags](https://www.airship.com/docs/reference/glossary/#tag). It works by syncing an Amplitude behavioral cohort with Airship. Airship then sets Tags on the applicable users. Once your data is in Airship, you can target users who do or do not have those Tags.

The Tags appear in the `amplitude` [Tag Group](https://www.airship.com/docs/reference/glossary/#tag_group) and contain the name and ID of your synced Amplitude cohort in the format `[Amplitude] <cohort name>: <cohortID>`. For example: `[Amplitude] Has Sent Notification: y4isxl9`.

See also [Identify users with similar behaviors](https://amplitude.com/docs/analytics/behavioral-cohorts) in Amplitude's documentation.

### Configuring the Inbound Integration 

In Airship:

1. Next to your project name, select the dropdown menu (
), then **Settings**.
1. Under **Project settings**, select **Partner Integrations**.
1. Select **Amplitude**.
1. Select **Configure** for the inbound integration and follow the onscreen instructions to:
	* Create a [Tag Group](https://www.airship.com/docs/reference/glossary/#tag_group) and an authentication token. Amplitude uses the token to communicate with your project in Airship.
	* Set up an Airship audience cohort in Amplitude.

From your new cohort in Amplitude, select **Sync**, then **Airship**. Airship will start setting Tags representing your Amplitude data on applicable members of your Airship audience.

You can continue syncing on demand or set up recurring or real-time syncing. See [Sync to third-party destinations](https://help.amplitude.com/hc/en-us/articles/360060055531-Sync-to-third-party-destinations) in Amplitude's *Syncs and integrations* documentation.

<!-- https://www.docs.developers.amplitude.com/data/destinations/airship-cohort/#send-a-cohort -->

### Targeting Users

In the API, target using the `"audience"` object. Specify the `amplitude` Tag Group and the Tag in the format `[Amplitude] <cohort name>: <cohortID>`.

**Targeting Amplitude Tags**

```json
{
   "audience": {
      "group": "amplitude",
      "tag": "[Amplitude] Has Sent Notification: y4isxl9"
   }
}
```


<!--

Update below for new and old and note saying "single-use segment" for TSU/targeting by conditions.

-->

To target in the dashboard, create a [Segment](https://www.airship.com/docs/reference/glossary/#segment) and add a Tag as a condition. First select the `amplitude` Tag Group, then search for a Tag and complete configuring the Segment. For individual messages, you can create a single-use Segment while creating the message. You can also create reusable Segments instead of having to recreate your audience selections.

For [In-App Automations](https://www.airship.com/docs/reference/glossary/#iaa) and [Scenes](https://www.airship.com/docs/reference/glossary/#scene), you cannot target [server-side](https://www.airship.com/docs/guides/audience/segmentation/segmentation/#client-server) Tags directly. Instead, create a Segment that includes the tags you added using this integration, then select that Segment in the Audience step in the composer. See [Segments](https://www.airship.com/docs/guides/audience/segmentation/target-specific-users/#segments) in *Targeting Specific Users*.

For more information, see [Targeting users](https://www.airship.com/docs/guides/audience/tags/#targeting-users) in the *Tags* documentation.

## Outbound Integration

The outbound integration sends your Airship events to Amplitude using [Real-Time Data Streaming](https://www.airship.com/docs/reference/glossary/#rtds).

For those events:
   1. Airship sets the [Named User](https://www.airship.com/docs/reference/glossary/#named_user) associated with the event as the Amplitude user ID.
   1. If Named User is not present, Airship sets the Android Advertising ID (AAID) or Apple Advertising ID (IDFA) associated with the event as the Amplitude device ID.
   1. If neither the AAID nor IDFA is present, Airship sets the [Channel ID](https://www.airship.com/docs/reference/glossary/#channel_id) associated with the event as the Amplitude device ID. If your app is configured to exclude collecting the IDFA by the Airship SDK, it cannot be sent to Amplitude.
   1. If Airship cannot set the Amplitude user ID or device ID, Airship will not send the event.

### Configuring the Outbound Integration 

You will need your Amplitude API key, found in your [Amplitude app settings](https://analytics.amplitude.com/settings). You will also need to know the region where you have implemented Amplitude so you can select a data center, either US or EU. If you are unsure which region to select, [contact Airship Support](https://support.airship.com).

In Airship:

1. Next to your project name, select the dropdown menu (
), then **Settings**.
1. Under **Project settings**, select **Partner Integrations**.
1. Select **Amplitude**.
1. Select **Configure** for the outbound integration and follow the onscreen instructions to:
   * Add your Amplitude API key.
   * Select a data center location.
   * Select the Airship events to send to Amplitude.

After completing setup, Airship will begin sending events from your Airship project to your Amplitude instance.

### Setting Advertising IDs

For Airship RTDS events without an associated [Named User](https://www.airship.com/docs/reference/glossary/#named_user), you can collect advertising IDs to be used as device IDs for events sent to Amplitude. See [Android](https://www.airship.com/docs/developer/sdk-integration/android/analytics/#associated-identifiers) and
[iOS](https://www.airship.com/docs/developer/sdk-integration/apple/analytics/#associated-identifiers) custom identifiers documentation for more information. The following code examples demonstrate how to add the advertising ID on each supported platform.

For iOS, associate the Apple `advertisingID` with the Airship [Channel ID](https://www.airship.com/docs/reference/glossary/#channel_id). If applicable, you can also associate Apple's `identifierForVendor` with the Channel ID. For more information, see [identifierForVendor](https://developer.apple.com/documentation/uikit/uidevice/1620059-identifierforvendor) in Apple's *UIKit* documentation.


#### iOS Swift


```swift
// Get the current identifiers
let identifiers = Airship.analytics.currentAssociatedDeviceIdentifiers()

// Set the advertising ID info
identifiers.advertisingID = ASIdentifierManager.sharedManager().advertisingIdentifier.UUIDString;
identifiers.advertisingTrackingEnabled = ASIdentifierManager.sharedManager().advertisingTrackingEnabled;
identifiers.vendorID = UIDevice.currentDevice().identifierForVendor?.UUIDString

// Associate the identifiers
Airship.analytics.associateDeviceIdentifiers(identifiers)
```



#### iOS Objective-C


```obj-c
// Get the current identifiers
UAAssociatedIdentifiers *identifiers = [UAirship.analytics currentAssociatedDeviceIdentifiers];

// Set the advertising ID info
identifiers.advertisingID = [[ASIdentifierManager sharedManager].advertisingIdentifier] UUIDString];
identifiers.advertisingTrackingEnabled = [ASIdentifierManager sharedManager].advertisingTrackingEnabled;
identifiers.vendorID = [[UIDevice currentDevice].identifierForVendor UUIDString];

// Associate the identifiers
[UAirship.analytics associateDeviceIdentifiers:identifiers];
```




For Android, associate the Android Advertising ID with the Airship Channel ID.

**Automatically track the Android Advertising ID**

```java
Airship.getAnalytics().setAutoTrackAdvertisingIdEnabled(true);
```


**Manually track the Android Advertising ID**

```java
// Get the Android Advertising ID info - This call is blocking and should be done in a
// background thread.
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getContext());

Airship.getAnalytics()
   .editAssociatedIdentifiers()
   .setAdvertisingId(adInfo.getId(), adInfo.isLimitAdTrackingEnabled())
   .apply();
```


### Tag Change Mapping

For `tag_change` events in outbound integrations, Airship uses Amplitude's [Identify API](https://www.docs.developers.amplitude.com/analytics/apis/identify-api/) to apply Tags on your Amplitude audience.

Tags in an `tag_change` event's `current` object appear as `user_properties` in an Amplitude `identifyEvent`. They appear in the format `Airship <Tag Group name>": ["<Tag name>", "<Tag name>"]`. Compare the two in the following code samples.

**An Airship `tag_change` event**


```json
{
  "id": "00000169-4a14-67b2-1ddd-d9e733622c3a",
  "occurred": "2019-03-04T19:00:45.106Z",
  "offset": "1000001260057",
  "processed": "2019-03-04T19:00:47.094Z",
  "type": "SEND_REJECTED",
  "device": {
    "ios_channel": "1ef235f0-03d5-1384-893e-a19b5cd0d110",
    "channel": "1ef235f0-03d5-1384-893e-a19b5cd0d110",
    "device_type": "IOS",
    },
  "body":{
    "add": {
        "crm": [
          "partner",
          "active"
        ],
        "loyalty": [
          "silver_member"
        ]
    },
    "remove": {
        "device": [
          "shoe_buyer"
        ]
    },
    "current": {
        "crm": [
          "partner",
          "active",
          "new_user"
        ],
        "loyalty": [
          "silver_member",
          "special_offers"
        ],
        "device": [
          "san_francisco",
          "sports"
        ]
      }
    },
  "type": "TAG_CHANGE"
}
```


**Amplitude `user_properties`**


```json
{
   "user_properties": {
      "ios_channel": "1ef235f0-03d5-1384-893e-a19b5cd0d110",
      "channel": "1ef235f0-03d5-1384-893e-a19b5cd0d110",
      "device_type": "IOS",
      "Airship crm": ["partner", "active", "new_user"],
      "Airship loyalty": ["silver_member", "special_offers"],
      "Airship device": ["san_francisco", "sports"]
   }
}
```

