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