Last active
August 29, 2015 14:02
-
-
Save debrouwere/e0e0419c073b732767ae to your computer and use it in GitHub Desktop.
This file contains 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
# encoding: utf-8 | |
""" | |
First, run `authenticate.py` which will launch a new browser window that | |
will let you give this app permission to access your Google Analytics | |
data. The OAuth2 token will work for one hour and will be in | |
`ga-oauth2-token.json`. | |
Then, just run this file using `python analytics.py`. | |
Find out more about which metrics and dimensions are available to you | |
using the documentation at https://developers.google.com/analytics/devguides/reporting/core/v3/ | |
and the API explorer at https://ga-dev-tools.appspot.com/explorer/ | |
""" | |
import os | |
import json | |
import httplib2 | |
import textwrap | |
from datetime import date | |
from oauth2client.client import Credentials | |
from apiclient.discovery import build | |
CREDENTIALS = 'ga-oauth2-token.json' | |
def wrap(s): | |
return textwrap.dedent(s).strip().replace('\n', '') | |
if not os.path.exists(CREDENTIALS): | |
raise Exception(wrap(""" | |
Please go through the OAuth2 authentication workflow | |
by running tools/authenticate.py | |
""")) | |
credentials = Credentials.new_from_json(open(CREDENTIALS).read()) | |
http = credentials.authorize(httplib2.Http()) | |
service = build('analytics', 'v3', http=http) | |
accounts = service.management().accounts().list().execute() | |
account = accounts['items'][0] | |
webproperties = service.management().webproperties().list( | |
accountId=account['id']).execute() | |
webproperty = webproperties['items'][0] | |
profiles = service.management().profiles().list( | |
accountId=account['id'], | |
webPropertyId=webproperty['id']).execute() | |
profile = profiles['items'][0] | |
date = date(2014, 3, 3).isoformat() | |
data = service.data().ga().get( | |
ids='ga:' + profile['id'], | |
start_date=date, | |
end_date=date, | |
metrics='ga:pageviews').execute() | |
print 'accounts', [account['name'] for account in accounts['items']] | |
print '0:profiles', [profile['name'] for profile in profiles['items']] | |
print data['rows'] |
This file contains 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 argparse | |
import httplib2 | |
import sys | |
from apiclient.discovery import build | |
from oauth2client.client import flow_from_clientsecrets | |
from oauth2client.file import Storage | |
from oauth2client import tools | |
parser = argparse.ArgumentParser( | |
description=__doc__, | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
parents=[tools.argparser]) | |
flags = parser.parse_args(sys.argv[1:]) | |
CLIENT_SECRETS = 'client_secrets.json' | |
MISSING_CLIENT_SECRETS_MESSAGE = '%s is missing' % CLIENT_SECRETS | |
FLOW = flow_from_clientsecrets(CLIENT_SECRETS, | |
scope='https://www.googleapis.com/auth/analytics.readonly', | |
message=MISSING_CLIENT_SECRETS_MESSAGE) | |
TOKEN_FILE_NAME = 'ga-oauth2-token.json' | |
if __name__ == '__main__': | |
storage = Storage(TOKEN_FILE_NAME) | |
credentials = storage.get() | |
if credentials is None or credentials.invalid: | |
credentials = tools.run_flow(FLOW, storage, flags) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment