App Events for Android
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 mobile app ads. This is done by logging events from your app via Facebook's Android SDK. The event can be one of 14 predefined events such as added to cart in a commerce app or achieved level in a game, 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. See our getting started guide to learn more.
Implementation
Beginning with the Facebook SDK for Android v4.19, the SDK supports automatic initialization and automatic app event logging. If your app uses the SDK, the SDK is automatically initialized for each application session, and the SDK logs this as an app activation event. Logging app activation enables you to observe how many users install your app, how frequently users activate your app, how much time they spend using it, and other demographic information through Facebook Analytics. For more information on the events that are logged automatically, see Automatic App Event Logging.
To disable automatic event logging, see Manual Event Logging.
Important: For the Facebook SDK for Android v4.18, and earlier, SDK initialization is a manual process that differs from the manual event logging process described below. For more information, see Legacy SDK Initialization.
Manual Event Logging
The process described here for manual event logging of the SDK is provided so that you have the option of controlling when app events are logged. We recommend that you do not disable automatic logging because it may impact how data is collected for your app.
To disable automatic event logging, add the following line to your AndroidManifest.xml
file:
<meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled" android:value="false"/>
With automatic event logging disabled, you must manually log the app activation event in order to see app installs, activations, time spent in the application, and user demographics. Unlike earlier versions of the SDK, the Facebook SDK initialization is performed automatically. To log app activations, import com.facebook.appevents.AppEventsLogger
into your Application
class and call the activateApp
method. The following is an example Application
class demonstrating the process:
package com.example.hellofacebook; import android.app.Application; import com.facebook.appevents.AppEventsLogger; public class HelloFacebookApplication extends Application { @Override public void onCreate() { super.onCreate(); AppEventsLogger.activateApp(this); } }
Legacy SDK Initialization
In the Facebook SDK for Android v4.18, and earlier, the SDK must be initialized and app activation events logged explicitly. The SDK provides a helper method to log the app activation event.
On Android, invoke the SDK's app activation helper once when the application is created. The following code demonstrates how to initialize the SDK and log the app activation event in the Application
class:
package com.example.hellofacebook; import android.app.Application; import com.facebook.FacebookSdk; import com.facebook.appevents.AppEventsLogger; public class HelloFacebookSampleApplication extends Application { @Override public void onCreate() { super.onCreate(); FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); } }
Logging Events Manually
Create an AppEventsLogger
object and use the helper methods to log your events. To create a AppEventsLogger
:
AppEventsLogger logger = AppEventsLogger.newLogger(this);
Where this
is the Activity
your method is in.
You can then log your event to logger
:
logger.logEvent(AppEventsConstants.EVENT_NAME_{XXXXX});
Where AppEventConstants.EVENT_NAME_{XXXX}
is one of the constants shown in the Events table below.
You can also specify a set of parameters in a Bundle
and a valueToSum
property which is an arbitrary number that can represent any value (e.g., a price or a quantity). When reported, all of the valueToSum
properties will be summed together. For example, if 10 people each purchased one item that cost $10 (and passed in valueToSum
) then they would be summed to report a number of $100.
Bundle parameters = new Bundle(); parameters.putString(AppEventsConstants.EVENT_PARAM_CURRENCY, "USD"); parameters.putString(AppEventsConstants.EVENT_PARAM_CONTENT_TYPE, "product"); parameters.putString(AppEventsConstants.EVENT_PARAM_CONTENT_ID, "HDFU-8452"); logger.logEvent(AppEventsConstants.EVENT_NAME_ADDED_TO_CART, 54.23, parameters);
Note that both the valueToSum
and parameters
arguments are optional.
The full list of pre-defined events and pre-defined parameters are listed below.
Events
Each of these events can be logged with a valueToSum
and a set of parameters (up to 25 parameters). The table below shows the recommended events and what are typically useful parameters for each event (the common parameter names are defined in the next table). You should include the events that make sense based on the user actions in your app.
You can use the following app event builder to create code for both custom events and predefined events.
The following table lists the event names.
AppEventsConstants Value | valueToSum | Parameters |
---|---|---|
Achieved Level:
|
| |
App Launched:
| ||
Added Payment Info:
|
| |
Added to Cart:
| Price of item added |
|
Added to Wishlist:
| Price of item added |
|
Completed Registration:
|
| |
Completed Tutorial:
|
| |
Initiated Checkout:
| Total price of items in cart |
|
Purchased:
| purchase price |
|
Rated:
| Rating given |
|
Searched:
|
| |
Spent Credits:
| Total value of credits spent |
|
Unlocked Achievement:
|
| |
Viewed Content:
| Price of item viewed (if applicable) |
|
Parameters
The table below are typically useful parameters for inclusion with the events shown above or with your own custom events. Future enhancements to our insights product will use the recommended parameters, but note that you can provide your own parameters as well.
These pre-defined parameters are intended to provide guidance on typically useful logging patterns, and may have a more readable form in reporting and other UI. Your app should log the set of parameters that it's interested in seeing breakdowns on in insights. The recommended description for these are guidance only - you can use these parameters for whatever makes sense for your app.
If you need to remove obsolete parameters - you can deactivate parameters by following the instructions in our help center.
The parameters are passed via a Bundle
where the key holds the parameter name and the value either a String
or an int
, as shown below. If you provide another type of value that is not compliant such as a boolean
, the SDK logs a warning to LogginBehavior.APP_EVENT
.
AppEventConstants::EVENT_PARAM_* | Possible Values | Description |
---|---|---|
Content ID:
|
| International Article Number (EAN) when applicable, or other product or content identifier |
Content Type:
|
| 'product' or 'product_group' |
Currency:
|
| ISO 4217 code, e.g., "EUR", "USD", "JPY" |
Description:
|
| A string description |
Level:
|
| Level of a game |
Max. Rating Value:
|
| Upper bounds of a rating scale, for example 5 on a 5 star scale |
Number of Items:
|
| Number of items |
Payment Info Available:
|
|
|
Registration Method:
|
| Facebook, Email, Twitter, etc. |
Search String:
|
| The text string that was searched for |
Success:
|
|
|
Purchase Method Example
The Facebook SDK provides a helper method to log purchases with a single line of code. For example, a purchase of USD $4.32, can be be logged with:
logger.logPurchase(BigDecimal.valueOf(4.32), Currency.getInstance("USD"));
The currency specification is expected to be an ISO 4217 currency code. This is used to determine a uniform value for use in ads optimization.
The maximum number of different event names is 1,000. Note no new event types will be logged once this cap is hit. However it is possible to deactivate obsolete events. Read more about event limits in the FAQ.
Important: There are many reasons that a user may initiate a purchase that can't be completed. For accurate logging of purchase events, only log the purchase after a purchase has been completed.
Custom App Events
You can also choose to create your own custom events, which is done simply by specifying their name as a string:
logger.logEvent("battledAnOrc");
API reference documentation
More details about the AppEventsLogger
class can be found in the reference documentation.
Verifying Event Logging in Facebook Analytics
If you're an Admin or a Developer of an app, you can validate that you've correctly implemented app events in Facebook Analytics. Please see our documentation on debugging events in Facebook Analytics to learn how to verify that events are being logged.
Enabling Debug Logs
Developers can enable debug logs so they can easily verify App Event usages from client sides. The debug logs contain detailed request and response in JSON. You can enable debug logs by adding this code after initializing Facebook SDK:
FacebookSdk.setIsDebugEnabled(true); FacebookSdk.addLoggingBehavior(LoggingBehavior.APP_EVENTS);
This is only for debugging purpose. Please make sure to disable debug logs before deploying app in production.
Facebook Analytics
Enabling App Events means that you can automatically start using Facebook Analytics. This analytics channel provides demographic info about the people using your app, offers tools for better understanding the flows people follow in your app, and lets you compare cohorts of different kinds of people performing the same actions.
See our documentation for Facebook Analytics for more information.
Facebook Ads
Mobile App Ad Performance
You can use app events to better understand the engagement and ROI coming from your mobile ads on Facebook. If you've set up app events, they will be reported as different types of actions in Ads Manager so you can see which ads and campaigns resulted in what actions within your app. For example, you may see that a particular ad resulted in 10 total actions. Those actions could be 8 checkouts and 2 registrations, which will be specified in Ads Manager.
Mobile App Ad Targeting
Measuring app events also opens up an easier way to reach your existing app users for mobile app ads. After integrating app events, you will be able to reach your existing users, specific subsets of users based on what actions they have taken within your app, or even percentages of your most active users. For instance you can run ads to bring recent purchasers back into your app to complete more purchases or to your top 25% of purchasers. You can choose an audience to reach based on the app events you are measuring in your app.
To learn more about our mobile app ads, go to our tutorial on mobile app ads for installs or mobile app ads for engagement.
Ads Attribution
Once you've implemented app events into your Android app, the App Events you've added will automatically be tracked when you run Facebook mobile app ads for installs or mobile app ads for engagement and conversion. Initially, we will only track the 14 predefined events described above.
If using Power Editor to set up your ad, please ensure you use the default tracking settings, or if you manually configure tracking settings that the following tracking_spec is present, with {app_id}
replaced with your app's Facebook App Id.
{"action.type" : ["app_custom_event"], "application" : [{app_id}]}
What's reported:
If you have set up app events, they will be reported as different types of actions in Ads Manager. For example, you may see that a particular ad resulted in 10 total actions. Those actions could be 8 checkouts and 2 registrations, which will be specified in Ads Manager. You can learn more about measuring performance of mobile app ads in Ads Manager here and Post Install Reports in Power Editor
Data Control
The Facebook SDK offers a method to give your users control over how app events data is used by the Facebook ads system. Platform Policy requires that you provide users with an option to opt-out when sharing this information with Facebook. We recommend that you use our SDK method to offer the opt-out. The method is described below.
setLimitEventAndDataUsage (context, limitEventUsage) method behavior
When you set the limitEventUsage
parameter to true
, your app will continue to send event data to Facebook, but Facebook will not use the data to serve targeted ads. Facebook may continue to use the information for other purposes, including frequency capping, conversion events, estimating the number of unique users, security and fraud detection, and debugging.
The limitEventUsage
setting persists across app sessions. You are responsible for providing the user interface that enables the user of your app to control this value.
Example usage
In this example, this
is used for the context parameter. The context could be an Activity
or other Context
object. The limitEventUsage parameter is set to true
.
FacebookSdk.setLimitEventAndDataUsage(this, true);
Data Sharing
App Event data shared by you with Facebook will not be shared with advertisers or other third parties, unless we have your permission or are required to do so by law.