Log User Actions

Google Search uses information about the actions users take on public and personal content in an app to improve ranking for Search results and suggestions. To improve your users' experience when they search for content in your app, log user actions through the App Indexing API.

What actions should you log?

Use the following guidelines as you log user actions:

  • Log the user's interactions with the app, including viewing content, creating new content, or sharing content.
  • Only log actions the user takes on content directly — not background actions within the app like incoming messages or playlist syncs.
  • Don't log actions for each item in a list when a user interacts with the entire list. For example, don't log view actions for each message whenever a user opens their inbox.

Build and log the actions

To build the Action object, define the following parameters:

  • Title and URL of the content
  • Appropriate Action.Builder for the action type

Then, log the action:

  • For actions that take place over a longer duration (e.g., viewing a recipe), call both the start() and end() methods, accordingly. For example, you would log separate calls for viewing a recipe (start) and then closing the recipe (end).
  • For instantaneous actions, call the end() method as soon as the user takes the action. For example, when a user comments on a note, log an instantaneous action with a single call of the end() method.

About action types: Use the correct Action.Builder constant for your content. For example, use the VIEW_ACTION constant for opening static content and the WATCH_ACTION constant for playing video content. See a list of constants for the Action.Builder class.

Regarding fragments: You structure fragments the same way the activity in the example below is structured. But, because fragments may execute many times within an activity, or there may be multiple fragments, take care to make the API call only once. Here are some guidelines to follow:

  • If the activity calls the API, then don't call the API again from any fragment within the activity.
  • If the activity doesn't call the API, and you want a fragment to call it instead, then make sure only one fragment calls the API, only one time.
Start and end

The example below uses public content.

    @Override
    public void onStart() {
        super.onStart();

        // If you’re logging an action on content that hasn’t been added to the index yet,
        // add it first.
        // See https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index.

        FirebaseUserActions.getInstance().start(getRecipeViewAction());

    }

    @Override
    public void onStop() {
        FirebaseUserActions.getInstance().end(getRecipeViewAction());
        super.onStop();
    }

        
Instantaneous

The example below uses personal content.

    private void displayNoteDialog(final String positiveText, final String negativeText) {
        ...

        // If you’re logging an action on content that hasn’t been added to the index yet,
        // add it first.
        // See https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index.

        FirebaseUserActions.getInstance().end(getNoteCommentAction());
        ...
    }

    private Action getNoteCommentAction() {
        return new Action.Builder(Action.Builder.COMMENT_ACTION)
                .setObject(mNote.getTitle(), mNote.getNoteUrl())
                // Keep action data for personal content on the device
                .setMetadata(new Action.Metadata.Builder().setUpload(false))
                .build();
    }
      

Next: Test Your Implementation

Send feedback about...

Need help? Visit our support page.