public class AppEventsLogger
extends java.lang.Object
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
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:
Here are some important characteristics of the logging mechanism provided by AppEventsLogger:
Some things to note when logging events:
Modifier and Type | Class and Description |
---|---|
static class |
AppEventsLogger.FlushBehavior
Controls when an AppEventsLogger sends log events to the server
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ACTION_APP_EVENTS_FLUSHED
The action used to indicate that a flush of app events has occurred.
|
static java.lang.String |
APP_EVENT_PREFERENCES |
static java.lang.String |
APP_EVENTS_EXTRA_FLUSH_RESULT |
static java.lang.String |
APP_EVENTS_EXTRA_NUM_EVENTS_FLUSHED |
Modifier | Constructor and Description |
---|---|
protected |
AppEventsLogger(java.lang.String activityName,
java.lang.String applicationId,
AccessToken accessToken) |
Modifier and Type | Method and Description |
---|---|
static void |
activateApp(android.app.Application application)
Notifies the events system that the app has launched and activate and deactivate events
should start being logged automatically.
|
static void |
activateApp(android.app.Application application,
java.lang.String applicationId)
Notifies the events system that the app has launched and activate and deactivate events
should start being logged automatically.
|
static void |
activateApp(android.content.Context context)
Deprecated.
|
static void |
activateApp(android.content.Context context,
java.lang.String applicationId)
Deprecated.
|
static void |
deactivateApp(android.content.Context context)
Deprecated.
When using
activateApp(Application) deactivate app will
be logged automatically. |
static void |
deactivateApp(android.content.Context context,
java.lang.String applicationId)
Deprecated.
When using
activateApp(Application) deactivate app will
be logged automatically. |
void |
flush()
Explicitly flush any stored events to the server.
|
static java.lang.String |
getAnonymousAppDeviceGUID(android.content.Context context)
Each app/device pair gets an GUID that is sent back with App Events and persisted with this
app/device pair.
|
java.lang.String |
getApplicationId()
Returns the app ID this logger was configured to log to.
|
static AppEventsLogger.FlushBehavior |
getFlushBehavior()
Access the behavior that AppEventsLogger uses to determine when to flush logged events to the
server.
|
boolean |
isValidForAccessToken(AccessToken accessToken)
Determines if the logger is valid for the given access token.
|
void |
logEvent(java.lang.String eventName)
Log an app event with the specified name.
|
void |
logEvent(java.lang.String eventName,
android.os.Bundle parameters)
Log an app event with the specified name and set of parameters.
|
void |
logEvent(java.lang.String eventName,
double valueToSum)
Log an app event with the specified name and the supplied value.
|
void |
logEvent(java.lang.String eventName,
double valueToSum,
android.os.Bundle parameters)
Log an app event with the specified name, supplied value, and set of parameters.
|
void |
logPurchase(java.math.BigDecimal purchaseAmount,
java.util.Currency currency)
Logs a purchase event with Facebook, in the specified amount and with the specified
currency.
|
void |
logPurchase(java.math.BigDecimal purchaseAmount,
java.util.Currency currency,
android.os.Bundle parameters)
Logs a purchase event with Facebook, in the specified amount and with the specified currency.
|
void |
logPushNotificationOpen(android.os.Bundle payload)
Logs an app event that tracks that the application was open via Push Notification.
|
void |
logPushNotificationOpen(android.os.Bundle payload,
java.lang.String action)
Logs an app event that tracks that the application was open via Push Notification.
|
void |
logSdkEvent(java.lang.String eventName,
java.lang.Double valueToSum,
android.os.Bundle parameters)
This method is intended only for internal use by the Facebook SDK and other use is
unsupported.
|
static AppEventsLogger |
newLogger(android.content.Context context)
Build an AppEventsLogger instance to log events through.
|
static AppEventsLogger |
newLogger(android.content.Context context,
AccessToken accessToken)
Build an AppEventsLogger instance to log events through.
|
static AppEventsLogger |
newLogger(android.content.Context context,
java.lang.String applicationId)
Build an AppEventsLogger instance to log events that are attributed to the application but
not to any particular Session.
|
static AppEventsLogger |
newLogger(android.content.Context context,
java.lang.String applicationId,
AccessToken accessToken)
Build an AppEventsLogger instance to log events through.
|
static void |
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.
|
static void |
setFlushBehavior(AppEventsLogger.FlushBehavior flushBehavior)
Set the behavior that this AppEventsLogger uses to determine when to flush logged events to
the server.
|
static void |
setPushNotificationsRegistrationId(java.lang.String registrationId)
Sets a registration id to register the current app installation for push notifications.
|
public static final java.lang.String APP_EVENT_PREFERENCES
public static final java.lang.String ACTION_APP_EVENTS_FLUSHED
LocalBroadcastManager
.public static final java.lang.String APP_EVENTS_EXTRA_NUM_EVENTS_FLUSHED
public static final java.lang.String APP_EVENTS_EXTRA_FLUSH_RESULT
protected AppEventsLogger(java.lang.String activityName, java.lang.String applicationId, AccessToken accessToken)
public static void activateApp(android.app.Application application)
application
- The running applicationpublic static void activateApp(android.app.Application application, java.lang.String applicationId)
application
- The running applicationapplicationId
- The application id used to log activate/deactivate events.@Deprecated public static void activateApp(android.content.Context context)
activateApp(Application)
activateApp(android.content.Context, String)
.context
- Used to access the applicationId and the attributionId for non-authenticated
users.@Deprecated public static void activateApp(android.content.Context context, java.lang.String applicationId)
activateApp(Application)
context
- Used to access the attributionId for non-authenticated users.applicationId
- The specific applicationId to report the activation for.@Deprecated public static void deactivateApp(android.content.Context context)
activateApp(Application)
deactivate app will
be logged automatically.deactivateApp(android.content.Context, String)
.context
- Used to access the applicationId and the attributionId for non-authenticated
users.@Deprecated public static void deactivateApp(android.content.Context context, java.lang.String applicationId)
activateApp(Application)
deactivate app will
be logged automatically.context
- Used to access the attributionId for non-authenticated users.applicationId
- The specific applicationId to track session information for.public static AppEventsLogger newLogger(android.content.Context context)
context
- Used to access the applicationId and the attributionId for non-authenticated
users.public static AppEventsLogger newLogger(android.content.Context context, AccessToken accessToken)
context
- Used to access the attributionId for non-authenticated users.accessToken
- Access 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.public static AppEventsLogger newLogger(android.content.Context context, java.lang.String applicationId, AccessToken accessToken)
context
- Used to access the attributionId for non-authenticated users.applicationId
- Explicitly specified Facebook applicationId to log events against. If
null, the default app ID specified in the package metadata will be
used.accessToken
- Access 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.public static AppEventsLogger newLogger(android.content.Context context, java.lang.String applicationId)
context
- Used to access the attributionId for non-authenticated users.applicationId
- Explicitly specified Facebook applicationId to log events against. If
null, the default app ID specified in the package metadata will be
used.public static AppEventsLogger.FlushBehavior getFlushBehavior()
public static void setFlushBehavior(AppEventsLogger.FlushBehavior flushBehavior)
flushBehavior
- the desired behavior.public void logEvent(java.lang.String eventName)
eventName
- eventName 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.public void logEvent(java.lang.String eventName, double valueToSum)
eventName
- eventName 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
eventNamevalueToSum
- a 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.public void logEvent(java.lang.String eventName, android.os.Bundle parameters)
eventName
- eventName 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.parameters
- A 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.public void logEvent(java.lang.String eventName, double valueToSum, android.os.Bundle parameters)
eventName
- eventName 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.valueToSum
- a 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.parameters
- A 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.public void logPurchase(java.math.BigDecimal purchaseAmount, java.util.Currency currency)
purchaseAmount
- 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.public void logPurchase(java.math.BigDecimal purchaseAmount, java.util.Currency currency, android.os.Bundle parameters)
purchaseAmount
- 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.parameters
- Arbitrary 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.public void logPushNotificationOpen(android.os.Bundle payload)
payload
- Notification payload received.public void logPushNotificationOpen(android.os.Bundle payload, java.lang.String action)
payload
- Notification payload received.public void flush()
public static void onContextStop()
public boolean isValidForAccessToken(AccessToken accessToken)
accessToken
- The access token to check.public static void setPushNotificationsRegistrationId(java.lang.String registrationId)
registrationId
- RegistrationId received from GCM.public void logSdkEvent(java.lang.String eventName, java.lang.Double valueToSum, android.os.Bundle parameters)
public java.lang.String getApplicationId()
public static java.lang.String getAnonymousAppDeviceGUID(android.content.Context context)
context
- The application context.