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()
andend()
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 theend()
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.
The example below uses public content.
Java
Android
@Override protected 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 <a href="https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index">https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index</a>. FirebaseUserActions.getInstance().start(getRecipeViewAction()); } @Override protected void onStop() { FirebaseUserActions.getInstance().end(getRecipeViewAction()); super.onStop(); }
Kotlin
Android
override fun onStart() { super.onStart() // If you’re logging an action on content that hasn’t been added to the index yet, // add it first. // See <a href="https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index">https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index</a>. FirebaseUserActions.getInstance().start(getRecipeViewAction()) } override fun onStop() { FirebaseUserActions.getInstance().end(getRecipeViewAction()) super.onStop() }
The example below uses personal content.
Java
Android
public 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 <a href="https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index">https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index</a>. FirebaseUserActions.getInstance().end(getNoteCommentAction()); // ... } public Action getNoteCommentAction() { return new Action.Builder(Action.Builder.COMMENT_ACTION) .setObject(mNote.getTitle(), mNote.getNoteUrl()) // Keep action data for personal connulltent on the device .setMetadata(new Action.Metadata.Builder().setUpload(false)) .build(); }
Kotlin
Android
fun displayNoteDialog(positiveText: String, negativeText: String) { // ... // If you’re logging an action on content that hasn’t been added to the index yet, // add it first. // See <a href="https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index">https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index</a>. FirebaseUserActions.getInstance().end(getNoteCommentAction()) // ... } private fun getNoteCommentAction(): Action { return Action.Builder(Action.Builder.COMMENT_ACTION) .setObject(note.title, note.noteUrl) // Keep action data for personal connulltent on the device .setMetadata(Action.Metadata.Builder().setUpload(false)) .build() }