-
public class AppEventsLoggerThe AppEventsLogger class allows the developer to log various types of events back to Facebook. In order to log events, the app must create an instance of this class via a newLogger method, and then call the various "log" methods off of that.
This client-side event logging is then available through Facebook App Insights and for use with Facebook Ads conversion tracking and optimization.
The AppEventsLogger class has a few related roles:
- Logging predefined and application-defined events to Facebook App Insights with a numeric value to sum across a large number of events, and an optional set of key/value parameters that define "segments" for this event (e.g., 'purchaserStatus' : 'frequent', or 'gamerLevel' : 'intermediate'). These events may also be used for ads conversion tracking, optimization, and other ads related targeting in the future.
- Methods that control the way in which events are flushed out to the Facebook servers.
Here are some important characteristics of the logging mechanism provided by AppEventsLogger:
- Events are not sent immediately when logged. They're cached and flushed out to the Facebook servers in a number of situations:
- when an event count threshold is passed (currently 100 logged events).
- when a time threshold is passed (currently 15 seconds).
- when an app has gone to background and is then brought back to the foreground.
- Events will be accumulated when the app is in a disconnected state, and sent when the connection is restored and one of the above 'flush' conditions are met.
- The AppEventsLogger class is intended to be used from the thread it was created on. Multiple AppEventsLoggers may be created on other threads if desired.
- The developer can call the setFlushBehavior method to force the flushing of events to only occur on an explicit call to the `flush` method.
- The developer can turn on console debug output for event logging and flushing to the server by calling FacebookSdk.addLoggingBehavior(LoggingBehavior.APP_EVENTS);
Some things to note when logging events:
- There is a limit on the number of unique event names an app can use, on the order of 1000.
- There is a limit to the number of unique parameter names in the provided parameters that can be used per event, on the order of 25. This is not just for an individual call, but for all invocations for that eventName.
- Event names and parameter names must be between 2 and 40 characters, and must consist of alphanumeric characters, _, -, or spaces.
- The length of each parameter value can be no more than on the order of 100 characters.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public enumAppEventsLogger.FlushBehaviorControls when an AppEventsLogger sends log events to the server
public enumAppEventsLogger.ProductAvailabilityProduct availability for Product Catalog product item update
public enumAppEventsLogger.ProductConditionProduct condition for Product Catalog product item update
-
Field Summary
Fields Modifier and Type Field Description public final static StringACTION_APP_EVENTS_FLUSHEDpublic final static StringAPP_EVENTS_EXTRA_NUM_EVENTS_FLUSHEDpublic final static StringAPP_EVENTS_EXTRA_FLUSH_RESULT
-
Method Summary
Modifier and Type Method Description static voidactivateApp(Application application)Notifies the events system that the app has launched and activate and deactivate events shouldstart being logged automatically. static voidactivateApp(Application application, String applicationId)Notifies the events system that the app has launched and activate and deactivate events shouldstart being logged automatically. static voidactivateApp(Context _context)static voidactivateApp(Context context, String _str)static voiddeactivateApp(Context _context)static voiddeactivateApp(Context _context, String _str)static voidinitializeLib(Context context, String applicationId)Notifies the events system which internal SDK Libraries, and some specific external Librariesthat the app is utilizing. static AppEventsLoggernewLogger(Context context)Build an AppEventsLogger instance to log events through. static AppEventsLoggernewLogger(Context context, AccessToken accessToken)Build an AppEventsLogger instance to log events through. static AppEventsLoggernewLogger(Context context, String applicationId, AccessToken accessToken)Build an AppEventsLogger instance to log events through. static AppEventsLoggernewLogger(Context context, String applicationId)Build an AppEventsLogger instance to log events that are attributed to the application but notto any particular Session. static AppEventsLogger.FlushBehaviorgetFlushBehavior()Access the behavior that AppEventsLogger uses to determine when to flush logged events to theserver. static voidsetFlushBehavior(AppEventsLogger.FlushBehavior flushBehavior)Set the behavior that this AppEventsLogger uses to determine when to flush logged events to theserver. voidlogEvent(String eventName)Log an app event with the specified name. voidlogEvent(String eventName, double valueToSum)Log an app event with the specified name and the supplied value. voidlogEvent(String eventName, Bundle parameters)Log an app event with the specified name and set of parameters. voidlogEvent(String eventName, double valueToSum, Bundle parameters)Log an app event with the specified name, supplied value, and set of parameters. voidlogPurchase(BigDecimal purchaseAmount, Currency currency)Logs a purchase event with Facebook, in the specified amount and with the specified currency. voidlogPurchase(BigDecimal purchaseAmount, Currency currency, Bundle parameters)Logs a purchase event with Facebook explicitly, in the specified amount and with the specifiedcurrency. voidlogPurchaseImplicitly(BigDecimal purchaseAmount, Currency currency, Bundle parameters)voidlogPushNotificationOpen(Bundle payload)Logs an app event that tracks that the application was open via Push Notification. voidlogPushNotificationOpen(Bundle payload, String action)Logs an app event that tracks that the application was open via Push Notification. voidlogProductItem(String itemID, AppEventsLogger.ProductAvailability availability, AppEventsLogger.ProductCondition condition, String description, String imageLink, String link, String title, BigDecimal priceAmount, Currency currency, String gtin, String mpn, String brand, Bundle parameters)Uploads product catalog product item as an app event. voidflush()Explicitly flush any stored events to the server. static voidonContextStop()Call this when the consuming Activity/Fragment receives an onStop() callback in order topersist any outstanding events to disk so they may be flushed at a later time. booleanisValidForAccessToken(AccessToken accessToken)Determines if the logger is valid for the given access token. static voidsetPushNotificationsRegistrationId(String registrationId)Sets and sends registration id to register the current app for push notifications. static voidaugmentWebView(WebView webView, Context context)Intended to be used as part of a hybrid webapp. static voidsetUserID(String userID)Sets a user id to associate with all app events. static StringgetUserID()Returns the set user id else null. static voidclearUserID()Clears the currently set user id. static voidsetUserData(Bundle userData)Sets user data to associate with all app events. static voidsetUserData(@Nullable() String email, @Nullable() String firstName, @Nullable() String lastName, @Nullable() String phone, @Nullable() String dateOfBirth, @Nullable() String gender, @Nullable() String city, @Nullable() String state, @Nullable() String zip, @Nullable() String country)Sets user data to associate with all app events. static StringgetUserData()Returns the set user data else null. static voidclearUserData()Clears the current user data static voidupdateUserProperties(Bundle parameters, GraphRequest.Callback callback)static voidupdateUserProperties(Bundle parameters, String applicationID, GraphRequest.Callback callback)voidlogSdkEvent(String eventName, Double valueToSum, Bundle parameters)This method is only for internal and use by the Facebook SDK account kit for legacy reason.Other usage is not allowed. StringgetApplicationId()Returns the app ID this logger was configured to log to. static StringgetAnonymousAppDeviceGUID(Context context)Each app/device pair gets an GUID that is sent back with App Events and persisted with thisapp/device pair. static voidsetInstallReferrer(String referrer)-
-
Method Detail
-
activateApp
static void activateApp(Application application)
Notifies the events system that the app has launched and activate and deactivate events shouldstart being logged automatically. By default this function is called automatically fromsdkInitialize() flow. In case 'com.facebook.sdk.AutoLogAppEventsEnabled' manifest setting isset to false, it should typically be called from the OnCreate method of you application.
- Parameters:
application- The running application
-
activateApp
static void activateApp(Application application, String applicationId)
Notifies the events system that the app has launched and activate and deactivate events shouldstart being logged automatically. By default this function is called automatically fromsdkInitialize() flow. In case 'com.facebook.sdk.AutoLogAppEventsEnabled' manifest setting isset to false, it should typically be called from the OnCreate method of you application.
Call this if you wish to use a different Application ID then the one specified in theFacebook SDK.
- Parameters:
application- The running applicationapplicationId- The application id used to log activate/deactivate events.
-
activateApp
@Deprecated() static void activateApp(Context _context)
-
activateApp
@Deprecated() static void activateApp(Context context, String _str)
-
deactivateApp
@Deprecated() static void deactivateApp(Context _context)
-
deactivateApp
@Deprecated() static void deactivateApp(Context _context, String _str)
-
initializeLib
static void initializeLib(Context context, String applicationId)
Notifies the events system which internal SDK Libraries, and some specific external Librariesthat the app is utilizing. This is called internally and does NOT need to be called externally.
- Parameters:
context- The ContextapplicationId- The String applicationId
-
newLogger
static AppEventsLogger newLogger(Context context)
Build an AppEventsLogger instance to log events through. The Facebook app that these events aretargeted at comes from this application's metadata. The application ID used to log events willbe determined from the app ID specified in the package metadata.
- Parameters:
context- Used to access the applicationId and the attributionId for non-authenticatedusers.
-
newLogger
static AppEventsLogger newLogger(Context context, AccessToken accessToken)
Build an AppEventsLogger instance to log events through.
- Parameters:
context- Used to access the attributionId for non-authenticated users.accessToken- Access token to use for logging events.
-
newLogger
static AppEventsLogger newLogger(Context context, String applicationId, AccessToken accessToken)
Build an AppEventsLogger instance to log events through.
- Parameters:
context- Used to access the attributionId for non-authenticated users.applicationId- Explicitly specified Facebook applicationId to log events against.accessToken- Access token to use for logging events.
-
newLogger
static AppEventsLogger newLogger(Context context, String applicationId)
Build an AppEventsLogger instance to log events that are attributed to the application but notto any particular Session.
- Parameters:
context- Used to access the attributionId for non-authenticated users.applicationId- Explicitly specified Facebook applicationId to log events against.
-
getFlushBehavior
static AppEventsLogger.FlushBehavior getFlushBehavior()
Access the behavior that AppEventsLogger uses to determine when to flush logged events to theserver. This setting applies to all instances of AppEventsLogger.
-
setFlushBehavior
static void setFlushBehavior(AppEventsLogger.FlushBehavior flushBehavior)
Set the behavior that this AppEventsLogger uses to determine when to flush logged events to theserver. This setting applies to all instances of AppEventsLogger.
- Parameters:
flushBehavior- the desired behavior.
-
logEvent
void logEvent(String eventName)
Log an app event with the specified name.
- Parameters:
eventName- eventName used to denote the event.
-
logEvent
void logEvent(String eventName, double valueToSum)
Log an app event with the specified name and the supplied value.
- Parameters:
eventName- eventName used to denote the event.valueToSum- a value to associate with the event which will be summed up in Insights foracross all instances of the event, so that average values can be determined, etc.
-
logEvent
void logEvent(String eventName, Bundle parameters)
Log an app event with the specified name and set of parameters.
- Parameters:
eventName- eventName used to denote the event.parameters- A Bundle of parameters to log with the event.
-
logEvent
void logEvent(String eventName, double valueToSum, Bundle parameters)
Log an app event with the specified name, supplied value, and set of parameters.
- Parameters:
eventName- eventName used to denote the event.valueToSum- a value to associate with the event which will be summed up in Insights foracross all instances of the event, so that average values can be determined, etc.parameters- A Bundle of parameters to log with the event.
-
logPurchase
void logPurchase(BigDecimal purchaseAmount, Currency currency)
Logs a purchase event with Facebook, in the specified amount and with the specified currency.
- Parameters:
purchaseAmount- Amount of purchase, in the currency specified by the 'currency'parameter.currency- Currency used to specify the amount.
-
logPurchase
void logPurchase(BigDecimal purchaseAmount, Currency currency, Bundle parameters)
Logs a purchase event with Facebook explicitly, in the specified amount and with the specifiedcurrency. Additional detail about the purchase can be passed in through the parameters bundle.
- Parameters:
purchaseAmount- Amount of purchase, in the currency specified by the 'currency'parameter.currency- Currency used to specify the amount.parameters- Arbitrary additional information for describing this event.
-
logPurchaseImplicitly
@Deprecated() void logPurchaseImplicitly(BigDecimal purchaseAmount, Currency currency, Bundle parameters)
-
logPushNotificationOpen
void logPushNotificationOpen(Bundle payload)
Logs an app event that tracks that the application was open via Push Notification.
- Parameters:
payload- Notification payload received.
-
logPushNotificationOpen
void logPushNotificationOpen(Bundle payload, String action)
Logs an app event that tracks that the application was open via Push Notification.
- Parameters:
payload- Notification payload received.
-
logProductItem
void logProductItem(String itemID, AppEventsLogger.ProductAvailability availability, AppEventsLogger.ProductCondition condition, String description, String imageLink, String link, String title, BigDecimal priceAmount, Currency currency, String gtin, String mpn, String brand, Bundle parameters)
Uploads product catalog product item as an app event.
- Parameters:
itemID- Unique ID for the item.availability- If item is in stock.condition- Product condition: new, refurbished or used.description- Short text describing product.imageLink- Link to item image used in ad.link- Link to merchant's site where someone can buy the item.title- Title of item.priceAmount- Amount of purchase, in the currency specified by the 'currency' parameter.This value will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).currency- Currency used to specify the amount.gtin- Global Trade Item Number including UPC, EAN, JAN and ISBNmpn- Unique manufacture ID for productbrand- Name of the brand Note: Either gtin, mpn or brand is required.parameters- Optional fields for deep link specification.
-
flush
void flush()
Explicitly flush any stored events to the server. Implicit flushes may happen depending on thevalue of getFlushBehavior. This method allows for explicit, app invoked flushing.
-
onContextStop
static void onContextStop()
Call this when the consuming Activity/Fragment receives an onStop() callback in order topersist any outstanding events to disk so they may be flushed at a later time. The next flush(explicit or not) will check for any outstanding events and if present, include them in thatflush. Note that this call may trigger an I/O operation on the calling thread. Explicit use ofthis method is necessary.
-
isValidForAccessToken
boolean isValidForAccessToken(AccessToken accessToken)
Determines if the logger is valid for the given access token.
- Parameters:
accessToken- The access token to check.
-
setPushNotificationsRegistrationId
static void setPushNotificationsRegistrationId(String registrationId)
Sets and sends registration id to register the current app for push notifications.
- Parameters:
registrationId- RegistrationId received from FCM.
-
augmentWebView
static void augmentWebView(WebView webView, Context context)
Intended to be used as part of a hybrid webapp. If you call this method, the FB SDK will add anew JavaScript interface into your webview. If the FB Pixel is used within the webview, andreferences the app ID of this app, then it will detect the presence of this injected JavaScriptobject and pass Pixel events back to the FB SDK for logging using the AppEvents framework.
- Parameters:
webView- The webview to augment with the additional JavaScript behaviourcontext- Used to access the applicationId and the attributionId for non-authenticatedusers.
-
setUserID
static void setUserID(String userID)
Sets a user id to associate with all app events. This can be used to associate your own user idwith the app events logged from this instance of an application.
The user ID will be persisted between application instances.
- Parameters:
userID- A User ID
-
clearUserID
static void clearUserID()
Clears the currently set user id.
-
setUserData
@Deprecated() static void setUserData(Bundle userData)
Sets user data to associate with all app events. All user data are hashed and used to matchFacebook user from this instance of an application.
The user data will be persisted between application instances.
- Parameters:
userData- user data to identify the user.
-
setUserData
static void setUserData(@Nullable() String email, @Nullable() String firstName, @Nullable() String lastName, @Nullable() String phone, @Nullable() String dateOfBirth, @Nullable() String gender, @Nullable() String city, @Nullable() String state, @Nullable() String zip, @Nullable() String country)
Sets user data to associate with all app events. All user data are hashed and used to matchFacebook user from this instance of an application.
The user data will be persisted between application instances.
- Parameters:
email- user's emailfirstName- user's first namelastName- user's last namephone- user's phonedateOfBirth- user's date of birthgender- user's gendercity- user's citystate- user's statezip- user's zipcountry- user's country
-
getUserData
static String getUserData()
Returns the set user data else null.
-
clearUserData
static void clearUserData()
Clears the current user data
-
updateUserProperties
@Deprecated() static void updateUserProperties(Bundle parameters, GraphRequest.Callback callback)
-
updateUserProperties
@Deprecated() static void updateUserProperties(Bundle parameters, String applicationID, GraphRequest.Callback callback)
-
logSdkEvent
@Deprecated() void logSdkEvent(String eventName, Double valueToSum, Bundle parameters)
This method is only for internal and use by the Facebook SDK account kit for legacy reason.Other usage is not allowed.
-
getApplicationId
String getApplicationId()
Returns the app ID this logger was configured to log to.
-
getAnonymousAppDeviceGUID
static String getAnonymousAppDeviceGUID(Context context)
Each app/device pair gets an GUID that is sent back with App Events and persisted with thisapp/device pair.
- Parameters:
context- The application context.
-
setInstallReferrer
static void setInstallReferrer(String referrer)
-
-
-
-