> ## Documentation Index
> Fetch the complete documentation index at: https://docs.median.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Optimizely

> Initialize the Optimizely SDK, identify users, and manage push notification permissions and registration from your website with the JavaScript Bridge.

The Optimizely plugin lets you initialize the SDK and manage push-related actions through the Median JavaScript Bridge. The plugin focuses on identifying users and controlling push registration and permission state from your web layer, giving you full control over the Optimizely SDK initialization without relying solely on App Studio auto-configuration.

## Why use Optimizely

* **Bridge-first control**: Request permissions, read permission state, and sync user identity from JavaScript without writing native code.
* **Push workflow coverage**: Request notification permission, check permission status, and register for push from a single API.
* **User identity support**: Set user email or user ID for audience targeting before registering push tokens.

## Prerequisites

* A Median app with JavaScript Bridge enabled
* Optimizely API key
* Deep linking setup if you want notification links to open directly in-app

Use Optimizely when your app needs to:

| Use case | Example |
| - | - |
| **SDK initialization control** | Initialize Optimizely at runtime when App Studio auto-initialization is not used. |
| **User identity mapping** | Set `email` or `userId` before push registration for accurate audience targeting. |
| **Push token lifecycle updates** | Re-register push after identifying a user to generate a token tied to that identity. |
| **Permission management** | Prompt users for notification permission and check grant status before registering. |
| **Deep link routing from notifications** | Open notification links directly inside the app after Deep Linking is configured. |

## What you'll do

Use this section as a fast navigation map into the full setup and implementation details:

1. **Enable the plugin config**: Go to [Plugin setup](#plugin-setup) and complete [Enable the plugin](#enable-the-plugin).
2. **Confirm runtime behavior**: Review [Configuration options](#configuration-options) for the `active` toggle behavior.
3. **Implement bridge calls**: Add core methods from [JavaScript Bridge functions](#javascript-bridge-functions).
4. **Validate integration flow**: Run the scenarios in [Code examples](#code-examples).
5. **Verify edge cases**: Complete [Testing checklist](#testing-checklist) and use [Troubleshooting](#troubleshooting).

## Plugin setup

### Enable the plugin

To enable this plugin, [book a call with the Median team](https://median.co/pricing) to discuss your requirements and get started.

### Configuration options

| Setting | Description | Default | Recommended |
| - | - | - | - |
| `active` | Enables or disables the Optimizely plugin integration. | `false` | `true` in environments where Optimizely is required. |

**Note**: Notification links can open directly in the app when Deep Linking is configured.

## JavaScript Bridge functions

### Initialize

Initialize Optimizely with an API key when not auto-configured in App Studio. For this function, provide a callback function or otherwise a promise is returned.

Median JavaScript Bridge

```javascript JavaScript theme={null}
// Using promises (recommended)
const result = await median.optimizely.initialize({
  apiKey: "YOUR_OPTIMIZELY_API_KEY",
});
console.log("initialize result:", result);

// Using callback
median.optimizely.initialize(
{ apiKey: "YOUR_OPTIMIZELY_API_KEY" },
function (result) {
console.log("initialize callback:", result);
},
function (err) {
console.error("Optimizely error (callback)", err);
},
);

```

**Parameters:**

| Parameter | Type | Required | Description |
| - | - | - | - |
| `apiKey` | `string` | Yes | Optimizely API key used to initialize the SDK. |
| `callback` | `function` | No | Optional callback; otherwise a promise is returned. |

**Return value:**

Median JavaScript Bridge

```javascript JavaScript theme={null}
interface OptimizelyInitializeResponse {
  success: boolean;
}
```

### Check if initialized

Check whether the Optimizely SDK is already initialized. For this function, provide a callback function, or a promise is returned.

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.optimizely.isInitialized({'callback': function});
```

**Parameters:**

| Parameter | Type | Required | Description |
| - | - | - | - |
| `callback` | `function` | No | Optional callback; otherwise a promise is returned. |

**Return value:**

Median JavaScript Bridge

```javascript JavaScript theme={null}
interface OptimizelyIsInitializedResponse {
  initialized: boolean;
  apiKey?: string;
}
```

### Set email

Identify the user by email address in Optimizely. Call `registerPush` after this to generate a new push token associated with the identified user. For this function, provide a callback function or otherwise a promise is returned.

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.optimizely.setEmail({'email': 'user@example.com', 'callback': function});
```

**Parameters:**

| Parameter | Type | Required | Description |
| - | - | - | - |
| `email` | `string` | Yes | User email address for identity mapping. |
| `callback` | `function` | No | Optional callback; otherwise a promise is returned. |

**Return value:**

Median JavaScript Bridge

```javascript JavaScript theme={null}
interface OptimizelySetIdentityResponse {
  success: boolean;
}
```

### Set user ID

Identify the user by a custom ID in Optimizely. Call `registerPush` after this to generate a new push token associated with the identified user. For this function, provide a callback function or a promise is returned.

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.optimizely.setUserId({'userId': 'user-123', 'callback': function});
```

**Parameters:**

| Parameter | Type | Required | Description |
| - | - | - | - |
| `userId` | `string` | Yes | Custom user identifier for audience targeting. |
| `callback` | `function` | No | Optional callback; otherwise a promise is returned. |

**Return value:**

Median JavaScript Bridge

```javascript JavaScript theme={null}
interface OptimizelySetIdentityResponse {
  success: boolean;
}
```

### Register for push notifications

Register the current user for push and generate a new push token. Call this after setting the user identity to ensure the token is associated with the identified user. For this function, provide a callback function, or a promise is returned.

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.optimizely.registerPush({'callback': function});
```

**Parameters:**

| Parameter | Type | Required | Description |
| - | - | - | - |
| `callback` | `function` | No | Optional callback; otherwise a promise is returned. |

**Return value:**

Median JavaScript Bridge

```javascript JavaScript theme={null}
interface OptimizelyRegisterPushResponse {
  success: boolean;
}
```

### Prompt for push notification permission

Prompt the user to grant notification permission. For this function, provide a callback function, or otherwise a promise is returned.

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.optimizely.requestPermission({'callback': function});
```

**Parameters:**

| Parameter | Type | Required | Description |
| - | - | - | - |
| `callback` | `function` | No | Optional callback; otherwise a promise is returned. |

**Return value:**

Median JavaScript Bridge

```javascript JavaScript theme={null}
interface OptimizelyPermissionResponse {
  granted: boolean;
}
```

### Check push notification permission status

Check the current notification permission state without prompting the user. For this function, provide a callback function, or a promise is returned.

Median JavaScript Bridge

```javascript JavaScript theme={null}
median.optimizely.permissionStatus({'callback': function});
```

**Parameters:**

| Parameter | Type | Required | Description |
| - | - | - | - |
| `callback` | `function` | No | Optional callback; otherwise a promise is returned. |

**Return value:**

Median JavaScript Bridge

```javascript JavaScript theme={null}
interface OptimizelyPermissionResponse {
  granted: boolean;
}
```

## Testing checklist

Use this checklist to ensure Optimizely is working correctly in your app:

**Basic functionality**

* [ ] `initialize` returns `{ success: true }`.
* [ ] `isInitialized` reflects the expected state after initialization.
* [ ] `setEmail` and `setUserId` each return `{ success: true }`.
* [ ] `registerPush` succeeds after user identity is set.
* [ ] Plugin loads without JavaScript errors.

**Platform testing**

* [ ] Permission request flow works on iOS.
* [ ] Android 13+ shows the system permission dialog.
* [ ] Android 12 and below grants permission without a dialog prompt.
* [ ] Plugin gracefully handles platform differences.

**Error scenarios**

* [ ] Invalid or missing API key is handled in the UI flow.
* [ ] An initialization guard prevents duplicate `initialize` calls.
* [ ] Permission-denied path is handled gracefully.
* [ ] Network errors during push registration are caught and logged.

**Edge cases**

* [ ] Push registration is re-run after identity updates.
* [ ] Deep link notification opens the correct route in-app when configured.
* [ ] Both promise and callback invocation styles work in your integration.

**User experience**

* [ ] Users receive a clear prompt when notification permission is requested.
* [ ] App provides a fallback experience when push permission is denied.
* [ ] No duplicate permission prompts appear after the user has already responded.
* [ ] Plugin does not interfere with app navigation or page transitions.

## Troubleshooting

### Optimizely functions are undefined or not working

Confirm the JavaScript Bridge is enabled in Median App Studio and that the plugin is active. Also verify you are calling bridge functions after the page and bridge have fully loaded.

### `isInitialized` remains false after calling `initialize`

Ensure you passed a valid `apiKey` to `initialize` and that the call returned `{ success: true }`. If initialization failed silently, check for network issues or an incorrect API key, then retry.

### Push notifications are not arriving after sign-in

After setting user identity with `setEmail` or `setUserId`, you must call `registerPush` to generate a new token associated with that user. Skipping this step means the push token is not linked to the newly identified user.

### Notification links do not open inside the app

Deep Linking must be configured in your app for notification links to open directly in-app. Without Deep Linking, tapping a notification will open the device's default browser instead.

### Permission dialog does not appear on Android

On Android 12 and below, push permission is granted automatically without a dialog. On Android 13+, the system permission dialog is required. Verify you are calling `requestPermission` and testing on a device or emulator running Android 13 or higher to see the dialog.

> [!NOTE]
>
> **Still having trouble?**
>
> For issues related to Optimizely's own configuration, permissions, or platform-specific behavior outside Median, refer to [Optimizely's integration documentation](https://docs.developers.optimizely.com/).


## Related topics

- [Push notifications overview](https://docs.median.co/docs/push-notifications-overview.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.