Skip to content

This sample Android app manages items on to-do lists using Google Assistant implemented via App Actions.

License

Notifications You must be signed in to change notification settings

actions-on-google/appactions-common-biis-kotlin

 
 

Repository files navigation

Get analytics for App Actions using Google Analytics for Firebase

TL;DR

This sample demonstrates instrumenting the To-do list App Actions Android App sample to log events using the Google Analytics for Firebase SDK. You can then use Firebase Console to visualize traffic patterns and obtain insights on app usage and user engagement.

Why Google Analytics for Firebase

Google Analytics for Firebase is a free app measurement solution providing insights on user engagement for websites and Android apps (user actions, system events, errors and more). Google Analytics for Firebase can provide insights into different user behaviors with App Actions, notably, the most popular built-in intents (BIIs) used in your app. Further, Google Analytics for Firebase data can be exported to downstream systems like Big Query for further analysis.

Requirements

Before using the sample, you must add Firebase to your App:

  • Add a Firebase configuration file
  • Enable Firebase products in your root-level build.gradle file
  • Apply the Google Service Gradle plugin
  • Add the Google Analytics for Firebase SDK

Once these steps are completed your app will be registered and ready to use Google Analytics for Firebase.

How to use this sample


NOTE

This sample app has already been set up with Google Analytics for Firebase. To set up analytics for your own app, see the "Add Analytics to your app" section below.


Clone or download this project to your preferred location. Then, import and update it to suit your environment:

  1. Switch to get-analytics-with-firebase branch.

    $ git checkout get-analytics-with-firebase
  2. In Android Studio, select “Open an existing Android Studio project” from the initial screen or File > Open.

    Change applicationId in app/build.gradle to the applicationId of a draft or published app in Google Play Console.

    android {
    defaultConfig {
        // This ID uniquely identifies your app on device and in Google Play
        applicationId "com.example.myapp"
    }
  3. In Android Studio, find the root directory of the sample.

  4. Select the build.gradle file.

  5. Follow the instructions in the IDE.

Next, test the App Actions integration:

  1. Build and run the sample on your physical test device (Run "app").

  2. Open the App Actions test tool Tools > App Actions > App Actions Test Tool.

  3. Define an invocation name for App Actions (like "my test app"). This name is only used for testing so it can be different from what is deployed to production.

  4. Click Create Preview. Once your preview is created, the test tool window will update to reflect BIIs found in the actions.xml file.

    Test tool after preview creation

    After creating a preview you can use voice or written commands directly with Assistant on your test device.

    If you run into any issues, check out the troubleshooting issues in our developer documentation.

  5. To view aggregated statistics about your events, navigate to the Google Analytics for Firebase dashboards under Analytics > Streamview. These dashboards update periodically throughout the day. For immediate testing, use the logcat output as described in the previous section.

    You can access events data from the Events dashboard in the Firebase console. This dashboard shows the event reports that are automatically created for each distinct type of event logged by your app.

    Firebase Streamview showing App Actions traffic
  6. View events in the Android Studio debug log.

    You may enable verbose logging of events by the SDK to verify events are being logged properly. This includes both automatic and manually logged events.

    To enable verbose logging, enter this sequence of adb commands from your terminal:

    $ adb shell setprop log.tag.FA VERBOSE
    $ adb shell setprop log.tag.FA-SVC VERBOSE
    $ adb logcat -v time -s FA FA-SVC

    You should now be able to view events in the Android Studio logcat and verify events are being sent.


    NOTE

    You can also verify Firebase logs with minimal delay in the debug view report by enabling debug mode.


Add Analytics to your app

This section describes how to enable Google Analytics for Firebase in the context of App Actions. Refer to the Firebase Analytics guide for more information.

  1. Add the dependency for the Google Analytics Android library to your module (app-level) Gradle file (usually app/build.gradle):

    implementation 'com.google.firebase:firebase-analytics-ktx:17.5.0'
  2. Declare the com.google.firebase.analytics.FirebaseAnalytics object at the top of TaskActivity.kt activity:

    private lateinit var firebaseAnalytics: FirebaseAnalytics
  3. Initialize FirebaseAnalytics in the onCreate() method:

    // Obtain the FirebaseAnalytics instance.
    firebaseAnalytics = Firebase.analytics
  4. Log events from TasksActivity class. Refer to event logging events documentation for more information:

    override fun onCreate(savedInstanceState: Bundle?) {
      logEventToFirebase(intent.data);
    }
    
    
    /**
    * This method will record the event to Google Analytics for Firebase
    */
    private fun logEventToFirebase(data: Uri?) {
      val utmCampaign: String = data?.getQueryParameter("utm_campaign") ?: N_A
      val path: String = data?.path ?: N_A
      mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.APP_OPEN) {
        param(FirebaseAnalytics.Param.CAMPAIGN, utmCampaign)
        param("app_action_path", path) // logging a custom parameter
      }
    }

    NOTE

    You may include custom params, for instance app_action_path for logging other types of data.


  5. actions.xml needs additional information to indicate the source of the request. Add a url query parameter with the key: utm_campaign and value: appactions:

    <fulfillment urlTemplate="https://todo.androidappactions.com/all-tasks?utm_campaign=appactions"/>

Contribution guidelines

If you want to contribute to this project, please review the contribution guidelines.

We use GitHub issues for tracking requests and bugs. For technical questions, please use Stack Overflow.

Report general issues with App Actions features or make suggestions for additional built-in intents through our public issue tracker.

References

Releases

No releases published

Packages

No packages published

Languages