This guide shows you how to add App Events to your new or existing app using the Facebook Unity SDK.
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(); }); } } }
The following code examples demonstrate how to manually log events.
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 );
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 );
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 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.
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);
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);