Kivy recipe: google analytics, play services and bug reporting

Once I have submitted my in google play, I was really happy about how stable my app is. Look, this is ANR/crashes report from developers dashboard:

Cool ha? Two crashes within 6 months. Haha, how naive...

The thing is, that if some exception happens in python, android app will exit gracefully. I can't even imagine, that crash reporting allows you to
put extra information, which could be a stack trace for example in our case.

In my every day work I use sentry for monitoring the health status of my django apps. So why not to use it for kivy app? This is my most favorite part in python: There are solutions for everything.

So let's start...

Raven/Sentry integration

Let's assume that we already have working sentry installation. First we need install raven - client app for sentry:

pip install raven

Put as much code from main.py as possible in try-catch block

try:  
    from kivy.app import App

    class KognitivoApp(App):
        ...


    if __name__ == '__main__':
        app = KognitivoApp()
        app.run()

except SystemExit:  
    pass
except:  
    ...

System exit is needed for passing exit() calls as non-errors.

After that you need to initialize raven client with sentry dsn in exception block and catch exception

from raven import Client  
client = Client(dsn="http://your_sentry_dsn")  
raven.captureException()  

That's it. If you want to have more context, you can attach device and manufacturer name, version and so on:

raven.tags_context({
    "version": settings.VERSION
})

if platform == 'android':
    from jnius import autoclass

    Build = autoclass("android.os.Build")
    self.user_context({
        "model": Build.MODEL,
        "manufacturer": Build.MANUFACTURER,
    })

After I've integrated it, imagine what I saw. This is how it looked like after four days:

And some context:

Once we have a web for catching bugs, we want also know, how do users interact with the app.

Google analytics

First we need last google play services version. If you use buildozer, you will find out, that default sdk downloaded by buildozer, does not have it. You will need to download it. Call a command:

    ~/.buildozer/android/platform/android-sdk-23/tools/android

Replace 23 with your sdk version, if you use a different one. Mark Extra->Google Services for download.

Copy ~/.buildozer/android/platform/android-sdk-23/extras/google/google_play_services/libproject/google-play-services_lib to your project folder. It's a standard part.

Add reference to lib project in buildozer.spec:

android.meta_data = com.google.android.gms.version=@integer/google_play_services_version  
android.library_references = google-play-services_lib/  

Build lib project (from project folder):

~/.buildozer/android/platform/android-sdk-23/tools/android update project -t 1 -p ./google-play-services_lib

Make android build, check if it works.

Now we have google play services in our apk.

I've sketched a small tracking client for kivy, which you can use. It depends on jnius and can be found here. It could be also useful to put screens tracking in a separate mixin:

class TrackingScreenMixin(object):  
    def on_enter(self, *args):
        tracker = App.get_running_app().tracker
        tracker.send_screen(self)

    def on_pre_leave(self, *args):
        tracker = App.get_running_app().tracker

        tracker.clear_screen()

You would need to instantiate tracker in your app first. Put events tracking where you want to monitor them:

self.tracker.send_event('category', 'action', 'label', 1)  

* Like it? Want more? Support my kivy app on facebook and google play. Thank you! *

That's it. This is the basic config, you can surely extend it with more google analytics or google services functions, but this is enough for a fast start.

Happy monitoring!


comments powered by Disqus