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.
Amending 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:
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
# 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)
# 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()
# 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(
# print out the results
total_visits = 0
analytics_results = 
for entry in data.entry:
analytics_results.append('%s visits for %s\n' % (entry.metric.value, entry.dimension.value))
total_visits = total_visits + int(entry.metric.value)
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
# Now show the results to the user
droid.dialogCreateAlert('Analytics Data', ''.join(analytics_results))
# Add an OK button to our dialog
droid.dialogSetPositiveButtonText(' Ok ')
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]