Initializing the Kotlin 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.
  • context: The application context object.

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, logs an error, and returns false. To check whether the SDK is initialized, use DYSdk.isInitialized.

Parameters

The following table lists the parameters the initialize method accepts:

Kotlin 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).
context
Required
android.content.ContextThe application context object.
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. This is set to false by default. 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

DYSdk.getInstance()
    .initialize(
        apiKey = "your-api-key",
        dataCenter = DataCenter.US,
        context = applicationContext,
        deviceId = "example-deviceId",
        channel = Channel.APP,
        ip = "1.2.3.4",
        locale = "en",
        isImplicitPageview = true,
        isImplicitImpressionMode = false,
)

DYSdk setters

Method nameParameterDescription
setDeviceTypeDeviceType
setDeviceIdString?For Restaurants only.
setChannelChannel
setIpString?
setLocaleString?
setIsImplicitPageviewBoolean?
setIsImplicitImpressionModeBoolean?
setActiveConsentAcceptedBoolean?A suspend function. Updates the user's consent state. See Handling Active Consent.
setRequestTimeoutUIntThe network request timeout, in seconds. The default is 30.
setDefaultProductDataTypeKSerializer<out RecsProductData>Sets the product data type that recommendations return. See Setting the Returned Product Attributes.
setLogLevelLogLevel

Resetting identifiers

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?
getConfigExperienceConfig? (the current SDK configuration)
DYSdk.isInitializedBoolean (a property of the DYSdk companion object)
📌

Note: Return values might be null or an empty string if not assigned yet (before the first call to the Choose Variations function).


Did this page help you?