# 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

* [A Facebook Developer Account](https://developers.facebook.com/docs/apps#register)
* [A Facebook App ID](https://developers.facebook.com/docs/apps#register)
* Facebook Unity SDK v4.27 or later – If you have not already implemented the Facebook Unity SDK please see our [Getting Started guide](https://developers.facebook.com/documentation/unity/getting-started/canvas).

## Log App Launches {#appActivation}

The SDK provides the [helper method `FB.ActivateApp`](https://developers.facebook.com/documentation/unity/reference/current/FB.ActivateApp) to log app launches. You will need to initialize the SDK with [`FB.Init`](https://developers.facebook.com/documentation/unity/reference/current/FB.Init) before you can call `FB.ActivateApp`.

Use the [Awake function](http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html) 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](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html) 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 {#exampleOne}

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 {#exampleTwo}

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

**Warning:** After you integrate Facebook SDK, certain App Events are automatically logged and collected for [Events Manager](https://www.facebook.com/events_manager), unless you disable Automatic App Event Logging. You may change this in your app code or through a toggle under App Events in the App Dashboard or Events Manager. Please note in the event of conflicting values between the `AutoLogAppEventsEnabled` flag and the toggle, we will honor the value in the **Automatic event logging for the Facebook SDK** toggle. For details about what information is collected and how to disable Automatic App Event Logging, see [Automatic App Event Logging](https://www.developers.facebook.com/documentation/app-events/automatic-event-collection-detail).

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

## Learn More

- [App Events Reference for Unity](https://developers.facebook.com/documentation/unity/reference/current/FB.LogAppEvent)

- [App Events Best Practices Guide](https://developers.facebook.com/documentation/app-events/best-practices)