Skip to content

Instantly share code, notes, and snippets.

@jkugler
Created September 11, 2011 06:05
Show Gist options
  • Save jkugler/1209230 to your computer and use it in GitHub Desktop.
Save jkugler/1209230 to your computer and use it in GitHub Desktop.
Google analytics examples
#!/usr/bin/python
import gdata.analytics.client
def main():
# source=os.path.basename(__file__) makes the "user agent" the name of the file
ga_client = gdata.analytics.client.AnalyticsClient(source=os.path.basename(__file__))
print "Getting list of GA Accounts...",
sys.stdout.flush()
try:
ga_client.client_login(<YOUR_USER_NAME>,
<YOUR_PASSWORD>,
source=os.path.basename(__file__),
service = ga_client.auth_service)
except gdata.client.BadAuthentication, ex:
print 'Invalid user credentials given.'
sys.exit(1)
except gdata.client.Error, ex:
print 'Login Error'
print str(ex)
sys.exit(1)
res = ga_client.GetAccountFeed(gdata.analytics.client.AccountFeedQuery())
ga_accounts = {}
for entry in res.entry:
d = {}
for attr in [('web_property_id', 'ga:webPropertyId'),
('account_name', 'ga:accountName'),
('account_id', 'ga:accountId'),
('profile_id', 'ga:profileId'),
('currencty', 'ga:currency'),
('timezone', 'ga:timezone')]:
d[attr[0]] = entry.GetProperty(attr[1]).value
d['profile_name'] = entry.title.text
d['table_id'] = entry.table_id.text
if d['profile_name'].startswith('www.'):
d['profile_name'] = d['profile_name'][4:]
if d['profile_name']endswith('/'):
d.['profile_name'] = d['profile_name'][0:-1]
ga_accounts[d['profile_name']] = d
print "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment