# Analytics Swift Optimizely Full Stack Plugin

Add OptimizelyFullStack session tracking support to your applications using this plugin for [Analytics-Swift](https://github.com/segmentio/analytics-swift)

> \[!WARNING]
>
> This plugin adds session data for OptimizelyFullStack, and events are sent using Cloud Mode.

## Getting started

1. In your Segment source dashboard, enable the "Optimizely Full Stack" destination (*not the "Optimizely Web" destination*).
2. Include your Optimizely project's `datafile` URL in your Segment settings.
3. Create a native Optimizely instance in your server environment so you can access Optimizely decisioning methods like `activate`, `isFeatureEnabled`.
4. Finally, define any [`events`](https://docs.developers.optimizely.com/full-stack/docs/create-events) and [`attributes`](https://docs.developers.optimizely.com/full-stack/docs/define-attributes) in your Optimizely dashboard, and to associate `metrics` with the appropriate Optimizely Experiments. Segment maps Track event names to Optimizely `eventName` - the `eventName` corresponds to an experiment `metric`. In addition, Segment maps Track event `context.traits` to Optimizely `attributes`.

## Adding the dependency

### Using 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:

```text
https://github.com/segment-integrations/analytics-swift-integration-optimizely-full-stack
```

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.

### Using Package.swift

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

```text
.package(
            name: "Segment",
            url: "https://github.com/segment-integrations/analytics-swift-integration-optimizely-full-stack.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.

```text
import Segment
import SegmentOptimizelyFullStack // <-- Add this line
```

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

```text
let analytics = Analytics(configuration: Configuration(writeKey: "<YOUR WRITE KEY>")
                    .flushAt(3)
                    .trackApplicationLifecycleEvents(true))
analytics.add(plugin: OptimizelyFullStack(optimizelyKey: "<Optimizely Production Key>"))
```

> \[!NOTE]
>
> Generate your `optimizelyKey` from the Optimizely Dashboard Settings. You can use a development or production SDK key.

Your events will now be given OptimizelyFullStack session data and start flowing to OptimizelyFullStack in Cloud Mode.

### Track

Upon invocation of a Segment Track event, Segment maps the event to an Optimizely Track event:

* If the Segment event name matches exactly the name of an active experiment `metric` set up in the Optimizely dashboard;
* If the experiment `metric` is associated with a running experiment;
* If the current user is activated in a running experiment with the associated `metric`.

Segment also handles the following mapping:

* Segment Track event name to Optimizely `eventName`.
* Segment Track event `properties` to Optimizely `eventTags`.

`revenue` values should be passed as a Segment `property`. The value should be an integer and represent the value in cents, so, for example, $1 should be represented by `100`.

> \[!NOTE]
>
> Optimizely's [Custom Event Tags](https://docs.developers.optimizely.com/full-stack/docs/include-event-tags), which include all Event Tags except `revenue` and `value`, are not displayed on the Optimizely results page. However, these tags are available in a [Data Export](https://docs.developers.optimizely.com/web/docs/data-export) report. Event Tags can be strings, integers, floating point numbers, or boolean values. Optimizely rejects events with any other data types (for example, arrays).

Segment defaults to identifying users with their `anonymousId`. Enabling "Use User ID" setting in your Segment dashboard means that only Track events triggered by identified users are passed downstream to Optimizely. You may optionally fall back to `anonymousId` when `userId` is unavailable by setting `fallbackToAnonymousId` to `true`.

### Identify

Invoking a Segment Identify event sets Segment `traits` as Optimizely `attributes`. The `attributes` are sent downstream to Optimizely upon invocation of the next Segment Track event.

### Notification listeners

Notification listeners are not available for Segment Track events when implementing Optimizely using Segment using cloud-mode. [Notification listeners](https://docs.developers.optimizely.com/full-stack/docs/notification-listeners) are still available with any native call invoked from your Optimizely client instance.

## Engage

Follow Optimizely's instructions on how to set up Engage and Optimizely:

[Using Segment Personas and Optimizely Full Stack for Omnichannel Experiments](https://www.optimizely.com/insights/blog/segment-personas-optimizely-full-stack-omnichannel-experiments/)

## GDPR support

Segment supports deleting/suppressing users in Optimizely using the [Segment app](/docs/segment/privacy/user-deletion-and-suppression/). In order to do this however, you will need to create a [Personal Access Token](https://docs.developers.optimizely.com/web-experimentation/docs/personal-access-token) in Optimizely and provide it as the value of the Access Token setting.
