Get Started – Unity

This guide shows you how to add App Events to your new or existing app using the Facebook Unity SDK.

Before you Start

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

Use the Code Generator to get code for standard events or custom events, and parameters.

To Generate Code for a Standard or Custom Event

  1. Choose the tab of the type of event code you want, Standard Event or Custom Event.
  2. In Event Name, enter the name of your event.
  3. Click Add Event Parameter if you would like to add parameters to your custom event.
  4. Click Get Code.
  5. In the window, select a language to get the code to copy and paste into your app.

Code Examples

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
);

Disable Automatically Logged Events

Our Unity SDK enables automatically logged events by default. To disable or enable automatic event logging, go to Facebook -> Edit Settings in the Unity IDE menu bar and select or unselect Auto Logging App Events under the App Events Settings section.

You can programmatically disable automatically logged events by setting the SetAutoLogAppEventsEnabled() method of Fb.Mobile to false.

FB.Mobile.SetAutoLogAppEventsEnabled(false); 

In some cases, you may wish to re-enable auto-logging after an end-user provides consent. You can do this by setting the SetAutoLogAppEventsEnabled() method to true.

FB.Mobile.SetAutoLogAppEventsEnabled(true); 

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);