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.com for the US data center and https://dy-api.eu for 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:

ParameterTypeDescription
apiKey
Required
StringUnique API Key for authentication generated in Experience OS.
dataCenter
Required
dataCenterThe Dynamic Yield data center to connect to (DataCenter.US or DataCenter.EU).
deviceTypeDeviceTypeThe type of device being used. Valid values are: SMARTPHONE, TABLET, KIOSK, ODMB
* KIOSK and ODMB are relevant for Restaurants only.
channelChannelThe digital channel through which the user is interacting. When used, the default value is Channel.APP. Other possible values are: KIOSK, DRIVE_THRU.
ipStringThe device IP address for geolocation-based targeting and audience building.
localeStringUsed to configure the locale that will be used for all DY experiences. Learn more about using localization and country codes for locale.
isImplicitPageviewBooleanDetermine whether to automatically report a new pageview when calling Choose Variations. This is set to false by default.
isImplicitImpressionModeBooleanDetermine 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.
customUrlStringRelevant when using a proxy server. In such cases, the dataCenter parameter is ignored.
activeConsentIntegrationBooleanSet 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.
activeConsentAcceptedBooleanThe 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
BooleanIndicates 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
StringA 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 nameParameterDescription
setDeviceTypeDeviceType
setDeviceIdstring | nullFor Restaurants only.
setChannelChannel
setIpstring | null
setLocalestring | null
setIsImplicitPageviewboolean | null
setIsImplicitImpressionModeboolean | null
setActiveConsentAcceptedboolean | nullUpdates the user's consent state. See Handling Active Consent.
setRequestTimeoutnumberThe network request timeout, in seconds (a whole number). The default is 30.
setLogLevelLogLevel

Resetting identifiers

These methods are asynchronous and return a Promise<Boolean>.

Method nameDescription
resetSessionIdStarts a new session ID.
resetUserIdStarts a new user ID (DYID).
resetUserIdAndSessionIdStarts a new user ID and session ID.

For when to reset identifiers, see Mobile SDK Identity Management.

DYSdk getters

Method nameReturn Value
getLogLevelLogLevel
getDyId (see note)String?
getSessionId (see note)String?
getConfigConfig or null (the current SDK configuration)
isInitializeboolean
📌

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.


Did this page help you?