AppEventsLogger

The 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.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.

Extends:Object
Package:appevents
Constructors
AppEventsLogger(String, String, AccessToken)
protected AppEventsLogger(String activityName, String applicationId, AccessToken accessToken)
ParameterDescription
activityName
applicationId
accessToken
Class Methods
activateApp(Application)

Notifies the events system that the app has launched and activate and deactivate events should start being logged automatically. This should be called from the OnCreate method of you application.

public static void activateApp(Application application)
ParameterDescription
applicationThe running application
activateApp(Application, String)

Notifies the events system that the app has launched and activate and deactivate events should start being logged automatically. This should 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 the Facebook SDK.

public static void activateApp(Application application, String applicationId)
ParameterDescription
applicationThe running application
applicationIdThe application id used to log activate/deactivate events.
activateApp(Context)

Notifies the events system that the app has launched & logs an activatedApp event. Should be called whenever your app becomes active, typically in the onResume() method of each long-running Activity of your app.

Use this method if your application ID is stored in application metadata, otherwise see AppEventsLogger.activateApp(android.content.Context, String).

public static void activateApp(Context context)
ParameterDescription
contextUsed to access the applicationId and the attributionId for non-authenticated users.
activateApp(Context, String)

Notifies the events system that the app has launched & logs an activatedApp event. Should be called whenever your app becomes active, typically in the onResume() method of each long-running Activity of your app.

public static void activateApp(Context context, String applicationId)
ParameterDescription
contextUsed to access the attributionId for non-authenticated users.
applicationIdThe specific applicationId to report the activation for.
deactivateApp(Context)

Notifies the events system that the app has been deactivated (put in the background) and tracks the application session information. Should be called whenever your app becomes inactive, typically in the onPause() method of each long-running Activity of your app.

Use this method if your application ID is stored in application metadata, otherwise see AppEventsLogger.deactivateApp(android.content.Context, String).

public static void deactivateApp(Context context)
ParameterDescription
contextUsed to access the applicationId and the attributionId for non-authenticated users.
deactivateApp(Context, String)

Notifies the events system that the app has been deactivated (put in the background) and tracks the application session information. Should be called whenever your app becomes inactive, typically in the onPause() method of each long-running Activity of your app.

public static void deactivateApp(Context context, String applicationId)
ParameterDescription
contextUsed to access the attributionId for non-authenticated users.
applicationIdThe specific applicationId to track session information for.
newLogger(Context)

Build an AppEventsLogger instance to log events through. The Facebook app that these events are targeted at comes from this application's metadata. The application ID used to log events will be determined from the app ID specified in the package metadata.

public static AppEventsLogger newLogger(Context context)
ParameterDescription
contextUsed to access the applicationId and the attributionId for non-authenticated users.
Returns
AppEventsLogger instance to invoke log* methods on.
newLogger(Context, AccessToken)

Build an AppEventsLogger instance to log events through.

public static AppEventsLogger newLogger(Context context, AccessToken accessToken)
ParameterDescription
contextUsed to access the attributionId for non-authenticated users.
accessTokenAccess token to use for logging events. If null, the active access token will be used, if any; if not the logging will happen against the default app ID specified in the package metadata.
newLogger(Context, String, AccessToken)

Build an AppEventsLogger instance to log events through.

public static AppEventsLogger newLogger(Context context, String applicationId, AccessToken accessToken)
ParameterDescription
contextUsed to access the attributionId for non-authenticated users.
applicationIdExplicitly specified Facebook applicationId to log events against. If null, the default app ID specified in the package metadata will be used.
accessTokenAccess token to use for logging events. If null, the active access token will be used, if any; if not the logging will happen against the default app ID specified in the package metadata.
Returns
AppEventsLogger instance to invoke log* methods on.
newLogger(Context, String)

Build an AppEventsLogger instance to log events that are attributed to the application but not to any particular Session.

public static AppEventsLogger newLogger(Context context, String applicationId)
ParameterDescription
contextUsed to access the attributionId for non-authenticated users.
applicationIdExplicitly specified Facebook applicationId to log events against. If null, the default app ID specified in the package metadata will be used.
Returns
AppEventsLogger instance to invoke log* methods on.
getFlushBehavior()

Access the behavior that AppEventsLogger uses to determine when to flush logged events to the server. This setting applies to all instances of AppEventsLogger.

public static FlushBehavior getFlushBehavior()
Returns
Specified flush behavior.
setFlushBehavior(FlushBehavior)

Set the behavior that this AppEventsLogger uses to determine when to flush logged events to the server. This setting applies to all instances of AppEventsLogger.

public static void setFlushBehavior(FlushBehavior flushBehavior)
ParameterDescription
flushBehaviorThe desired behavior.
onContextStop()

Call this when the consuming Activity/Fragment receives an onStop() callback in order to persist 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 that flush. Note that this call may trigger an I/O operation on the calling thread. Explicit use of this method is necessary.

public static void onContextStop()
setPushNotificationsRegistrationId(String)

Sets a registration id to register the current app installation for push notifications.

public static void setPushNotificationsRegistrationId(String registrationId)
ParameterDescription
registrationIdRegistrationId received from GCM.
getAnonymousAppDeviceGUID(Context)

Each app/device pair gets an GUID that is sent back with App Events and persisted with this app/device pair.

public static String getAnonymousAppDeviceGUID(Context context)
ParameterDescription
contextThe application context.
Returns
The GUID for this app/device pair.
Instance Methods
logEvent(String)

Log an app event with the specified name.

public void logEvent(String eventName)
ParameterDescription
eventNameEventName used to denote the event. Choose amongst the EVENT_NAME_* constants in AppEventsConstants when possible. Or create your own if none of the EVENT_NAME_* constants are applicable. Event names should be 40 characters or less, alphanumeric, and can include spaces, underscores or hyphens, but must not have a space or hyphen as the first character. Any given app should have no more than 1000 distinct event names.
logEvent(String, double)

Log an app event with the specified name and the supplied value.

public void logEvent(String eventName, double valueToSum)
ParameterDescription
eventNameEventName used to denote the event. Choose amongst the EVENT_NAME_* constants in AppEventsConstants when possible. Or create your own if none of the EVENT_NAME_* constants are applicable. Event names should be 40 characters or less, alphanumeric, and can include spaces, underscores or hyphens, but must not have a space or hyphen as the first character. Any given app should have no more than 1000 distinct event names. * @param eventName
valueToSumA value to associate with the event which will be summed up in Insights for across all instances of the event, so that average values can be determined, etc.
logEvent(String, Bundle)

Log an app event with the specified name and set of parameters.

public void logEvent(String eventName, Bundle parameters)
ParameterDescription
eventNameEventName used to denote the event. Choose amongst the EVENT_NAME_* constants in AppEventsConstants when possible. Or create your own if none of the EVENT_NAME_* constants are applicable. Event names should be 40 characters or less, alphanumeric, and can include spaces, underscores or hyphens, but must not have a space or hyphen as the first character. Any given app should have no more than 1000 distinct event names.
parametersA Bundle of parameters to log with the event. Insights will allow looking at the logs of these events via different parameter values. You can log on the order of 25 parameters with each distinct eventName. It's advisable to limit the number of unique values provided for each parameter in the thousands. As an example, don't attempt to provide a unique parameter value for each unique user in your app. You won't get meaningful aggregate reporting on so many parameter values. The values in the bundles should be Strings or numeric values.
logEvent(String, double, Bundle)

Log an app event with the specified name, supplied value, and set of parameters.

public void logEvent(String eventName, double valueToSum, Bundle parameters)
ParameterDescription
eventNameEventName used to denote the event. Choose amongst the EVENT_NAME_* constants in AppEventsConstants when possible. Or create your own if none of the EVENT_NAME_* constants are applicable. Event names should be 40 characters or less, alphanumeric, and can include spaces, underscores or hyphens, but must not have a space or hyphen as the first character. Any given app should have no more than 1000 distinct event names.
valueToSumA value to associate with the event which will be summed up in Insights for across all instances of the event, so that average values can be determined, etc.
parametersA Bundle of parameters to log with the event. Insights will allow looking at the logs of these events via different parameter values. You can log on the order of 25 parameters with each distinct eventName. It's advisable to limit the number of unique values provided for each parameter in the thousands. As an example, don't attempt to provide a unique parameter value for each unique user in your app. You won't get meaningful aggregate reporting on so many parameter values. The values in the bundles should be Strings or numeric values.
logPurchase(BigDecimal, Currency)

Logs a purchase event with Facebook, in the specified amount and with the specified currency.

public void logPurchase(BigDecimal purchaseAmount, Currency currency)
ParameterDescription
purchaseAmountAmount 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).
currencyCurrency used to specify the amount.
logPurchase(BigDecimal, Currency, Bundle)

Logs a purchase event with Facebook, in the specified amount and with the specified currency. Additional detail about the purchase can be passed in through the parameters bundle.

public void logPurchase(BigDecimal purchaseAmount, Currency currency, Bundle parameters)
ParameterDescription
purchaseAmountAmount 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).
currencyCurrency used to specify the amount.
parametersArbitrary additional information for describing this event. This should have no more than 24 entries, and keys should be mostly consistent from one purchase event to the next.
logPushNotificationOpen(Bundle)

Logs an app event that tracks that the application was open via Push Notification.

public void logPushNotificationOpen(Bundle payload)
ParameterDescription
payloadNotification payload received.
logPushNotificationOpen(Bundle, String)

Logs an app event that tracks that the application was open via Push Notification.

public void logPushNotificationOpen(Bundle payload, String action)
ParameterDescription
payloadNotification payload received.
action
flush()

Explicitly flush any stored events to the server. Implicit flushes may happen depending on the value of getFlushBehavior. This method allows for explicit, app invoked flushing.

public void flush()
isValidForAccessToken(AccessToken)

Determines if the logger is valid for the given access token.

public boolean isValidForAccessToken(AccessToken accessToken)
ParameterDescription
accessTokenThe access token to check.
Returns
True if the access token is valid for this logger.
logSdkEvent(String, Double, Bundle)

This method is intended only for internal use by the Facebook SDK and other use is unsupported.

public void logSdkEvent(String eventName, Double valueToSum, Bundle parameters)
ParameterDescription
eventName
valueToSum
parameters
getApplicationId()

Returns the app ID this logger was configured to log to.

public String getApplicationId()
Returns
The Facebook app ID
Constants
APP_EVENT_PREFERENCES
public static final String APP_EVENT_PREFERENCES
ACTION_APP_EVENTS_FLUSHED
The action used to indicate that a flush of app events has occurred. This should be used as an action in an IntentFilter and BroadcastReceiver registered with the LocalBroadcastManager.
public static final String ACTION_APP_EVENTS_FLUSHED
APP_EVENTS_EXTRA_NUM_EVENTS_FLUSHED
public static final String APP_EVENTS_EXTRA_NUM_EVENTS_FLUSHED
APP_EVENTS_EXTRA_FLUSH_RESULT
public static final String APP_EVENTS_EXTRA_FLUSH_RESULT