Skip to main content

Manual Initialization & Runtime Configuration

By default the config plugin wires the Facebook SDK into your app delegate so it initializes at launch. If you need to gate initialization behind user consent (GDPR, App Tracking Transparency), set isAutoInitEnabled: false in the config plugin — the plugin then leaves the app delegate untouched — and drive the SDK from JavaScript instead.

Deferred initialization​

import {
activateApp,
initialize,
setAutoInitEnabled,
setAutoLogAppEventsEnabled,
setAdvertiserIDCollectionEnabled,
} from "expo-facebook-analytics";

function onUserGrantedConsent() {
// Optionally configure collection before initializing.
setAutoLogAppEventsEnabled(true);
setAdvertiserIDCollectionEnabled(true);

// Initialize the SDK — events logged before this are not sent.
initialize();

// With automatic event logging off, this is what publishes the install and
// launch events Meta needs to attribute installs. Call it once per launch.
activateApp();
}

function onUserWithdrewConsent() {
// Android persists the auto-init flag that initialize() switched on, so undo
// it — otherwise the next launch starts the SDK before JavaScript runs.
setAutoInitEnabled(false);
}

initialize() turns auto-init on and fully initializes the native SDK. It is safe to call once; call it before you log any events.

Fully manual configuration (no plugin)​

You can provide the app ID and client token at runtime instead of through the config plugin — useful for white-label apps or runtime-fetched credentials:

import {
setAppID,
setClientToken,
initialize,
} from "expo-facebook-analytics";

setAppID("YOUR_FACEBOOK_APP_ID");
setClientToken("YOUR_FACEBOOK_CLIENT_TOKEN");
initialize();
Order matters

Call setAppID / setClientToken before initialize(). The Facebook SDK reads these values during initialization.

Runtime toggles​

FunctionEffect
setAutoInitEnabled(enabled)Enable/disable auto-init (persisted on Android)
setAutoLogAppEventsEnabled(enabled)Enable/disable automatic event logging
setAdvertiserIDCollectionEnabled(enabled)Enable/disable advertiser-ID (IDFA/AAID) collection
setAppID(appID)Set the Facebook App ID at runtime
setClientToken(token)Set the Facebook Client Token at runtime

These map directly to the native SDK's settings on both platforms, so a flag you flip here behaves identically to setting it through the config plugin.