React Native — Configuring

Learn how to import Adapty iOS SDK in your app, configure it, and set up logging

To initialize Adapty SDK, import adapty and call activate in your core component such as App.tsx. Preferably, place activation before React component to make sure no other Adapty call will happen before activation call.

import { adapty } from 'react-native-adapty';

adapty.activate('PUBLIC_SDK_KEY');

const App = () => {
	// ...
}

There are a number of optional parameters, that you can pass during activation:

adapty.activate('PUBLIC_SDK_KEY', {
  observerMode: false,
  customerUserId: 'YOUR_USER_ID',
  logLevel: 'error',
  __debugDeferActivation: false,
  ios: {
    storeKit2Usage: 'enabled_for_introductory_offer_eligibility',
    idfaCollectionDisabled: false,
    enableUsageLogs: true,
  },
});
import { IosStorekit2Usage, LogLevel } from 'react-native-adapty';

adapty.activate('PUBLIC_SDK_KEY', {
  observerMode: false,
  customerUserId: 'YOUR_USER_ID',
  logLevel: LogLevel.ERROR,
  __debugDeferActivation: false,
  ios: {
    storeKit2Usage: IosStorekit2Usage.EnabledForIntroductoryOfferEligibility,
    idfaCollectionDisabled: false,
    enableUsageLogs: true,
  },
});

  • Public SDK key (required): found in your app settings in Adapty Dashboard App settings > General.
  • Observer mode (optional): a boolean value controlling Observer mode. Turn it on if you handle purchases and subscription status yourself and use Adapty for sending subscription events and analytics.
  • Customer user ID (optional): an identifier of the user in your system. We send it in subscription and analytical events, to attribute events to the right profile. You can also find customers by customerUserId in the Profiles section.
    If you don't have a user ID at the time of Adapty initialization, you can set it later using adapty.identify() method. Read more in the Identifying Users section.
  • Log level (optional): a string parameter, that sets Adapty logs errors and other important information to help you understand what is going on.
  • Deferred activation (optional): a boolean parameter, that let's you defer SDK activation until you perform next Adapty call. This is designed only to assist during development and not in production.
  • Disabling IDFA collection (optional): a boolean parameter, that allows you to disable IDFA collection for your iOS app. Read more
  • Enable usage logs (optional): a boolean parameter, that allows us to collect usage logs to support you. Read more

📘

Note that storeKit2Usage refers to the method the SDK uses to determine eligibility for introductory offers, not the payment process itself. Pass the value 'enabled_for_introductory_offer_eligibility' if you want to utilize the StoreKit 2 API for this specific purpose.

📘

Make sure you use the Public SDK key for Adapty initialization, the Secret key should be used for server-side API only.

📘

SDK keys are unique for every app, so if you have multiple apps make sure you choose the right one.

Logging

Adapty logs errors and other important information to help you understand what is going on. There are four levels available:

  1. error: only errors will be logged
  2. warn: messages from the SDK that do not cause critical errors, but are worth paying attention to
  3. info: various information messages, such as those that log the lifecycle of various modules
  4. verbose: any additional information that may be useful during debugging, such as function calls, API queries, etc.

You can set logLevel at any time in the application's lifespan, but we recommend that you do this before configuring Adapty.

adapty.setLogLevel('verbose');
import { LogLevel } from 'react-native-adapty';

adapty.setLogLevel(LogLevel.VERBOSE);

For both activate and setLogLevel methods TypeScript would validate the string that you pass as an argument. However, if you are using JavaScript you might want to use LogLevel enum, that would guarantee to provide you a safe value:

Handling logs

If you save your stdout logs, you might want to filter Adapty logs from others. To do this you can add a prefix for all AdaptyError instances that would be consoled:

import { AdaptyError } from 'react-native-adapty';

AdaptyError.prefix = "[ADAPTY]";

You also can handle all the raised errors from any place you like with onError. Errors would be thrown where expected, but also duplicated to your event listener:

import { AdaptyError } from 'react-native-adapty';

AdaptyError.onError = error => {
	// ... 
  console.error(error);
};

Using StoreKit 2 features (iOS only)

adapty.activate('PUBLIC_SDK_KEY', {
  ios: {
    storeKit2Usage: 'enabled_for_introductory_offer_eligibility',
  },
});
import { IosStorekit2Usage } from 'react-native-adapty';

adapty.activate('PUBLIC_SDK_KEY', {
  ios: {
    storeKit2Usage: IosStorekit2Usage.EnabledForIntroductoryOfferEligibility,
  },
});

Collecting usage logs (iOS only)

The Adapty SDK allows you to activate the remote logging system. If you are experiencing any problems during a review process, or you need help with analyzing something in the production environment, you will need to activate this system both in the application's build itself and in our system (Contact the technical support team for the assistance).

To activate this system, pass the enableUsageLogs parameter while activating the SDK:

adapty.activate('PUBLIC_SDK_KEY', {
  ios: {
    enableUsageLogs: true,
  },
});

Delaying SDK activation

Adapty pre-fetches all required data about a user when SDK is activating. It allows Adapty to provide you fresh data faster.

Apparently, it leads to a problem with iOS simulator, when Simulator constantly prompts you to authenticate during development. While Adapty cannot control StoreKit authentication flow, it can delay the requests SDK makes to obtain fresh user data.

Turning __debugDeferActivation property on, holds activate call until the moment you make a next Adapty SDK call. This way, you would not be prompted to provide authentication data if you don't really need it.

This feature should be used in development only, as it does not consider all the edge cases of how users use your application. There is likely no need to delay activation in production as iPhones (or any real devices) remember authentication data and usually don't prompt to re-enter credentials again.

Thus, here is the preferred way to use it:

adapty.activate('PUBLIC_SDK_KEY', {
  __debugDeferActivation: isSimulator(), // 'isSimulator' from any 3rd party library
});