Hide

Annotate App Activity

The App Indexing API lets you annotate the actions users take in your app, and send that data to Google. Google can then surface the content from those actions to users via query autocompletions from the Google app.

For example, suppose a user has previously viewed a page in the Zagat app about the "Calafia Cafe." Zagat uses the App Indexing API to note the fact that the user viewed the page. Later, the same user wants to find that cafe they were reading about, and searches for "cala..." in the Google app. Using the data reported through the API, a query autocompletion for "Calafia Cafe" appears for the user, who can quickly return to the same page in the Zagat app from before.

To annotate any activity for your app, you'll do the following:

  1. Update your project
  2. Modify activities that support deep links
  3. Support Google Search quality guidelines

See the App Indexing API Reference for details on supported actions and helper classes.

Update your project

Make sure your project includes Google Play Services and update your configuration as shown below:

manifest
<application
  ...
  <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  ...
</application>
      
build.gradle
dependencies {
  ...
  compile 'com.google.android.gms:play-services-appindexing:7.0.0'
  ...
}
      

Modify deep link activities

The sample below illustrates how you'd modify the deep link activities in your app to send content view activity to Google, which is a common use case for using the App Indexing API. You can use the API to annotate other kinds of activities as well, such as System Voice Actions. Here, AppIndexApi.start() method accepts the action performed by the user within the app. The action contains the title of the content, which can appear in Google app query autocompletions. It also contains the deep link leading users to the specific app view, and the web URL for any corresponding web content.

In this example, you'll need to modify any view activity that supports deep links if you want to annotate and send the views to Google. Here's a common modification:

  1. Create an instance of GoogleApiClient in the onCreate() method of your activity.
  2. Use the newAction() helper convenience method to generate an Action object.

    This object represents the type of activity taken in your app by the user, and it also includes the page title and relevant URLs for the content. If there is no corresponding web content, you can pass in null. You can also use the more generic methods on Action.Builder to create an action with a description and other rich metadata.

  3. Use the AppIndexApi.start() method to indicate the start of the activity, passing in the Action object you created above.
  4. Use AppIndexApi.end() when the user completes the action.
  5. Disconnect your client in your onStop() method.

Typically, you'll set this up within the onStart() and onStop() methods of your app, but it's not strictly necessary, such as when sending fragments or other scrolling-type UIs.

MainActivity.java

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
...
public class MainActivity extends Activity {

  static final Uri APP_URI = Uri.parse("android-app://com.google.developers/appindexingdemo/restaurant1/");
  static final Uri WEB_URL = Uri.parse("http://appindexingdemo.google.com/restaurant1/");
  private GoogleApiClient mClient;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    ...
  }

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

    // Connect your client
    mClient.connect();

    // Define a title for your current page, shown in autocompletion UI
    String title = "App Indexing API Title";

    // Construct the Action performed by the user
    Action viewAction = Action.newAction(Action.TYPE_VIEW, title, WEB_URL, APP_URI);

    // Call the App Indexing API start method after the view has completely rendered
    AppIndex.AppIndexApi.start(mClient, viewAction);
    ...
  }

  @Override
  public void onStop() {
    ...
    // Call end() and disconnect the client
    String title = "App Indexing API Title";
    Action viewAction = Action.newAction(Action.TYPE_VIEW, title, WEB_URL, APP_URI);
    AppIndex.AppIndexApi.end(mClient, viewAction);
    mClient.disconnect();
    ...

    super.onStop();
  }
}

Quality Guidelines and Practices

In order to ensure a great search experience for users, Google may take corrective action in cases where we see abuse, deception, or other actions that hurt the search experience for users. This can include actions such as demoting or removing your app deep links from Google Search results and query autocompletions. Note that Google may react to other misleading practices not listed here.

Next, you can test your implementation.

Send feedback about...

App Indexing