Package 

Class AppEventsLogger


  • 
    public class 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 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.
    • 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 application
        applicationId - The application id used to log activate/deactivate events.
      • 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 Context
        applicationId - 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.
      • 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 ISBN
        mpn - Unique manufacture ID for product
        brand - 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 behaviour
        context - 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 email
        firstName - user's first name
        lastName - user's last name
        phone - user's phone
        dateOfBirth - user's date of birth
        gender - user's gender
        city - user's city
        state - user's state
        zip - user's zip
        country - user's country
      • clearUserData

         static void clearUserData()

        Clears the current user data

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

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