Initializing the React Native SDK
Start using the SDK by instantiating the DYSdk object.
The initialize method sets up the initial global configuration for the SDK, which is used across different functions.
The following fields must be set during the initialization step, and can't be modified afterwards:
- apiKey: To use the Dynamic Yield SDK, you need an API key generated in Experience OS. To create one, follow this guide. Use a server-side API key. The SDK sends its requests to the server-side API URLs (
https://dy-api.comfor the US data center andhttps://dy-api.eufor the EU data center), which reject client-side keys. The key is embedded in your app, so it can be extracted. Treat it as visible to users. Create a dedicated key for each app, give it only the permissions the SDK needs, and don't reuse a key from your server-side integrations. If the key is exposed, revoke it and replace it. For more information, see Security. - dataCenter: Your section is in one of our two data centers: US or EU.
Other parameters can be adjusted after the initialization using setters.
Call the initialize method once at the beginning of your SDK setup. If you call it again, the SDK ignores the call and returns false. To check whether the SDK is initialized, use isInitialize().
Parameters
The following table lists the parameters the initialize method accepts:
| Parameter | Type | Description |
|---|---|---|
| apiKey Required | String | Unique API Key for authentication generated in Experience OS. |
| dataCenter Required | dataCenter | The Dynamic Yield data center to connect to (DataCenter.US or DataCenter.EU). |
| deviceType | DeviceType | The type of device being used. Valid values are: SMARTPHONE, TABLET, KIOSK, ODMB * KIOSK and ODMB are relevant for Restaurants only. |
| channel | Channel | The digital channel through which the user is interacting. When used, the default value is Channel.APP. Other possible values are: KIOSK, DRIVE_THRU. |
| ip | String | The device IP address for geolocation-based targeting and audience building. |
| locale | String | Used to configure the locale that will be used for all DY experiences. Learn more about using localization and country codes for locale. |
| isImplicitPageview | Boolean | Determine whether to automatically report a new pageview when calling Choose Variations. This is set to false by default. |
| isImplicitImpressionMode | Boolean | Determine whether to automatically report an impression for all variations returned by the Choose call. This is set to true by default, except for Restaurants, where the default is false. |
| customUrl | String | Relevant when using a proxy server. In such cases, the dataCenter parameter is ignored. |
| activeConsentIntegration | Boolean | Set to true if your app collects user consent and the SDK must track users only after they accept. This is set to false by default. Learn more about handling active consent. |
| activeConsentAccepted | Boolean | The user's current consent state: true if the user accepted, false if not. Relevant only when activeConsentIntegration is true. If not set, the user is treated as not having accepted. The SDK treats any value other than true as not accepted. The SDK doesn't save this value, so pass it every time the app starts. To change it later, use setActiveConsentAccepted. |
| sharedDevice For Restaurants only | Boolean | Indicates whether the device running the application is shared among multiple users. This parameter defaults to false. Consult your technical account manager before changing this value. |
| deviceId For Restaurants only | String | A unique identifier for the device. |
Example
import { Channel, DataCenter, initialize, LogLevel, setLogLevel } from '@dynamicyield/react-native-sdk'
async function initializeExample() {
initialize({
apiKey: 'your-api-key',
dataCenter: DataCenter.US,
deviceId: 'example-deviceId',
channel: Channel.APP,
ip: '1.2.3.4',
locale: 'en',
isImplicitPageview: true,
isImplicitImpressionMode: false,
}).then(result => {
// handle success
console.log('Initialized:', result);
}).catch(error => {
// handle error
console.error('Initialization failed:', error);
});
}DYSdk setters
The setters are asynchronous. Each returns a Promise<Boolean>, so await it before your next SDK call. For example: await setLocale('en');
| Method name | Parameter | Description |
|---|---|---|
| setDeviceType | DeviceType | |
| setDeviceId | string | null | For Restaurants only. |
| setChannel | Channel | |
| setIp | string | null | |
| setLocale | string | null | |
| setIsImplicitPageview | boolean | null | |
| setIsImplicitImpressionMode | boolean | null | |
| setActiveConsentAccepted | boolean | null | Updates the user's consent state. See Handling Active Consent. |
| setRequestTimeout | number | The network request timeout, in seconds (a whole number). The default is 30. |
| setLogLevel | LogLevel |
Resetting identifiers
These methods are asynchronous and return a Promise<Boolean>.
| Method name | Description |
|---|---|
| resetSessionId | Starts a new session ID. |
| resetUserId | Starts a new user ID (DYID). |
| resetUserIdAndSessionId | Starts a new user ID and session ID. |
For when to reset identifiers, see Mobile SDK Identity Management.
DYSdk getters
| Method name | Return Value |
|---|---|
| getLogLevel | LogLevel |
| getDyId (see note) | String? |
| getSessionId (see note) | String? |
| getConfig | Config or null (the current SDK configuration) |
| isInitialize | boolean |
Note: Return values might be null or an empty string if not assigned yet (before the first call to the Choose Variations function).
The getters are synchronous and return their value directly, unlike the setters and reset methods.
Updated 6 days ago
