So following on from a comment to my previous post about Android Scripting Environment with Python, I thought I should update the original Google Analytics script with some of the new shiny that ASE provides.

I downloaded ase_r20.apk from the ASE website to take advantage of some of the new features.

One of the nice things is that you can now quickly input code via the API Browser menu option, which gives you a great list of all the API calls that ASE supports. It certainly helps reduce the errors and speed up the development time … especially when you’re typing on the Android’s virtual keyboard.

One thing which I’ve wanted to do for a long time is improve the dialog display and give some feedback to the user that something is indeed happening whilst the script goes off to talk to Google Analytics.

Fortunately, ASE now provides some dialog related API calls, including two types of progress indicators — an indeterminate circular spinner, and a traditional horizontal progress bar.

Progress DialogAmending the code is easy enough. Pop open the script in the editor, scroll down a few lines, select API Browser from the menu options, select dialogCreateSpinnerProgress, add some helpful text to the dialog box, select dialogShow again from the API Browser, and ta-da; you have an indicator to the user that something is happening. When the lengthy process is finished, you simply call dialogDismiss to remove the progress indicator.

Also, I used a bit of a hack to get the display of the final data last time, and I wanted to improve on this, because if you have a lot of data, the end of the dialog box gets pushed off the screen and the “Ok” button gets truncated too.

Again, ASE has a new dialogCreate API call. Inserting this into the code, followed by a dialogSetPositiveButtonText to create an “Ok” button is all you need. The dialog is dismissed automatically when you press the button.

Nice and simple, eh?

So, our final code would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Google Android Python Development example, using the Android Scripting Language (ASE).
This is an example of how to communicate with the Google Analytics Data API via Python.
Author: Howard Sandford, April 2010
Released under CC BY-SA-NC 3.0
"""


import gdata.analytics.service
import datetime
import android

# For use with the ASE
droid = android.Android()

# Let the user know we're doing something
droid.dialogCreateSpinnerProgress('Please wait ...', 'Collecting Google Analytics data', 100, False)
droid.dialogShow()

# The Google Analytics account login details
USERNAME = 'your_email_account@gmail'
PASSWORD = 'your_password'

# We want data for the last 7 days
end_date = datetime.datetime.today().strftime('%Y-%m-%d')
start_date = (datetime.datetime.today() - datetime.timedelta(7)).strftime('%Y-%m-%d')

# Authenticate ourselves against the Analytics Data API
client = gdata.analytics.service.AnalyticsDataService()
client.ClientLogin(USERNAME, PASSWORD)

# Grab the data for a particular profile_id -- in this case the
# number of visits for the keywords that people have searched for us
data = client.GetData(
    ids='ga:123456',
    dimensions='ga:keyword',
    metrics='ga:visits',
    sort='-ga:visits',
    start_date=start_date,
    end_date=end_date,
)

# print out the results
total_visits = 0
analytics_results = []
for entry in data.entry:
    analytics_results.append('%s visits for %s\n' % (entry.metric[0].value, entry.dimension[0].value))
    total_visits = total_visits + int(entry.metric[0].value)

analytics_results.append('-----\n')
analytics_results.append('Total of %s visits between %s and %s\n' % (total_visits, start_date, end_date))

# Ok, we're ready to show our results, so stop the progress indicator
droid.dialogDismiss()

# Now show the results to the user
droid.dialogCreateAlert('Analytics Data', ''.join(analytics_results))

# Add an OK button to our dialog
droid.dialogSetPositiveButtonText('  Ok  ')
droid.dialogShow()

# Finish!
droid.exit()

Feel free to play around with this code, obviously putting in your own values for username, password and Google Analytics web-site profile id. In the meantime, I’ll work on a few more examples showing some of the other useful user interface API calls.

[All files are released under Creative Commons BY-SA-NC 3.0]

3 thoughts on “Android Scripting Environment ASE Example for Python

  1. Hello Howard,

    thanks for these tutorials!
    I would like to ask you to find some plugin for your wordpress to show your code in view please.

    I am unable to see all the code on the page and if I look into source code of the page, then it is (the python code) littered with HTML tags.

    Thanks a lot
    Robert

  2. Hey Robert, sorry it took so long, but I’ve added a plugin to allow you to easily copy the code. How’s that? 🙂

    Cheers
    H

Leave a Reply

Your email address will not be published. Required fields are marked *