Get Started – Unity
Updated: Oct 16, 2026
Copy for LLM
This guide shows you how to add App Events to your new or existing app using the Facebook Unity SDK.
Before you Start
- A Facebook Developer Account
- A Facebook App ID
- Facebook Unity SDK v4.27 or later – If you have not already implemented the Facebook Unity SDK please see our Getting Started guide.
Log App Launches
The SDK provides the helper method
FB.ActivateApp to log app launches. You will need to initialize the SDK with FB.Init before you can call FB.ActivateApp.Use the Awake function from MonoBehavior to log when a user launches your app.
void Awake ()
{
if (FB.IsInitialized) {
FB.ActivateApp();
} else {
//Handle FB.Init
FB.Init( () => {
FB.ActivateApp();
});
}
}
Use the OnApplicationPause function from MonoBehavior to log when a user resumes your app.
// Unity will call OnApplicationPause(false) when an app is resumed
// from the background
void OnApplicationPause (bool pauseStatus)
{
// Check the pauseStatus to see if we are in the foreground
// or background
if (!pauseStatus) {
//app resume
if (FB.IsInitialized) {
FB.ActivateApp();
} else {
//Handle FB.Init
FB.Init( () => {
FB.ActivateApp();
});
}
}
}
Manually Log Events
The following code examples demonstrate how to manually log events.
Log an In-App Purchase
In this example
priceCurrency is a string containing the 3-letter ISO code representing the currency used, priceAmount is a float containing the price of the item purchased, and packageName is a string containing your SKU code for the item purchased.var iapParameters = new Dictionary<string, object>();
iapParameters["mygame_packagename"] = packageName;
FB.LogPurchase(
priceAmount,
priceCurrency,
iapParameters
);
Track an Event in a Game
In the second example we are tracking the number credits a user spent in a game.
numGold is a float containing the number credits spent and storeItem is a string containing the name of the item the user bought.var softPurchaseParameters = new Dictionary<string, object>();
softPurchaseParameters["mygame_purchased_item"] = storeItem;
FB.LogAppEvent(
Facebook.Unity.AppEventName.SpentCredits,
(float)numGold,
softPurchaseParameters
);
Configuring automatically logged events
After you integrate Facebook SDK, certain App Events are automatically logged and collected for Events Manager, unless you disable Automatic App Event Logging. You may change this in your app code or through a toggle in Events Manager or App Dashboard. Please note that if either the
AutoLogAppEventsEnabled flag or the toggle is set to disable, automatic event logging will be disabled. For details about what information is collected and how to disable Automatic App Event Logging, see Automatic App Event Logging.
You can disable automatic event logging or limit the app details included with logged events in Events Manager by editing dataset settings.
Alternatively, you can configure both settings in the Unity IDE. To disable automatic event logging, go to Facebook -> Edit Settings menu bar and select or unselect Auto Logging App Events and Auto Logging App Metadata under the App Events Settings section.
In some cases, you may want to delay the collection of automatically logged events, such as to obtain user consent or fulfill legal obligations, instead of disabling it. In this case, set the Auto Logging App Events setting to false as described above. After the end-user provides consent, call the
SetAutoLogAppEventsEnabled() method of Fb.Mobile class and set it to true.FB.Mobile.SetAutoLogAppEventsEnabled(true);
To suspend automatic logging again for any reason, set the
SetAutoLogAppEventsEnabled() method to false.FB.Mobile.SetAutoLogAppEventsEnabled(false);
Note: If you’ve turned off automatic event logging in Events Manager, the feature will be disabled even if you enable it in code.
Disable Collection of Advertiser IDs
Our Unity SDK enables collection of Advertiser IDs by default. To disable or enable Advertiser ID collection, go to Facebook -> Edit Settings in Unity IDE menu bar and select or unselect AdvertiserID Collection under the App Events Settings section.
You can programmatically disable Advertiser ID collection by setting the
SetAdvertiserIDCollectionEnabled () method of Fb.Mobile to false.FB.Mobile.SetAdvertiserIDCollectionEnabled (false);
In some cases, you may wish to re-enable Advertiser ID collection after an end-user provides consent. You can do this by setting the
SetAdvertiserIDCollectionEnabled() method to true.FB.Mobile.SetAdvertiserIDCollectionEnabled (true);