App Events for Unity

App Events help you understand the makeup of people who engage with your app and measure and reach specific sets of your users with Facebook app ads. This is done by logging events from your app via the Facebook SDK for Unity. The event can be one of 14 predefined events such as 'level achieved', or any custom events you can define.

Prerequisites

Before including the code to measure events, you'll need to register your app with Facebook and download and configure the Facebook SDK for Unity. See our getting started guide to learn more.

Logging app activations

Logging app activations as an App Event enables almost all other functionality and should be the first thing you add to your app. The SDK provides a helper method to log app activation. By logging an activation event, you can observe how frequently users activate your app, how much time they spend using it, and other demographic information through Facebook Analytics.

You should be calling FB.ActivateApp both on launch and each application resume to ensure you are correctly measuring sessions.

The SDK needs to be initalized first with FB.Init before you can call FB.ActivateApp.

Consider utilizing Unity's Awake function from MonoBehavior to cover App Launches.

void Awake ()
{
  if (FB.IsInitialized) {
    FB.ActivateApp();
  } else {
    //Handle FB.Init
    FB.Init( () => {
      FB.ActivateApp();
    });
  }
}

Consider utilizing Unity's OnApplicationPause function from MonoBehavior to cover App Resumes.

// 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();
      });
    }
  }
}

The FB.ActivateApp method is the preferred way to log app activations, even though there's an event that you can send manually via the SDK. The helper method performs a few other tasks that are necessary for proper accounting for Mobile App Install Ads.

Events

You can use the following app event builder to create code for both custom events and predefined events.


Example 1: Log that a user has bought something

In this example priceCurrency is a string containing the 3-letter ISO code for the currency that the user spent, and priceAmount is a float containing the quantity spent. packageName is a string containing your SKU code for the thing they bought.

var iapParameters = new Dictionary<string, object>();
iapParameters["mygame_packagename"] = packageName;
FB.LogPurchase(
  priceAmount,
  priceCurrency,
  iapParameters
);

Example 2: Track the amount of credits spent

In the second example we keep track of how many credits a person spent. numGold is the number of in-app currency the user spent in this purchase, storeItem is a string naming 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
);

The maximum number of different event names is 1,000. Note: No new event types will be logged once this cap is hit and if you exceed this limit you may see an 100 Invalid parameter error when logging. However it is possible to deactivate obsolete events. Read more about event limits in the FAQ.