Created
September 11, 2011 06:05
-
-
Save jkugler/1209230 to your computer and use it in GitHub Desktop.
Google analytics examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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