Commit 420cee4cc0ec1f0915c1484f5c428b2cc4003049

Authored by Noah ago
0 parents
Exists in master

first commit

Showing 4 changed files with 98 additions and 0 deletions Side-by-side Diff

analyticsreporting.dat View file @ 420cee4
... ... @@ -0,0 +1 @@
  1 +{"_module": "oauth2client.client", "scopes": ["https://www.googleapis.com/auth/analytics.readonly"], "token_expiry": "2016-06-30T09:06:40Z", "id_token": null, "access_token": "ya29.Ci8RA5d1vZMq-ZtmEnvWRjxp5CpIOsyjfv7y_p90fNj8OjVbKq-F8Lf5n_iqBbHAXQ", "token_uri": "https://accounts.google.com/o/oauth2/token", "invalid": false, "token_response": {"access_token": "ya29.Ci8RA5d1vZMq-ZtmEnvWRjxp5CpIOsyjfv7y_p90fNj8OjVbKq-F8Lf5n_iqBbHAXQ", "token_type": "Bearer", "expires_in": 3600}, "client_id": "884418547933-e08agllr5v2kp2r3i68ld31ptl3bj4rt.apps.googleusercontent.com", "token_info_uri": "https://www.googleapis.com/oauth2/v3/tokeninfo", "client_secret": "yHgeC50DdYVLTjdHwG00eRjA", "revoke_uri": "https://accounts.google.com/o/oauth2/revoke", "_class": "OAuth2Credentials", "refresh_token": "1/XLGetj18dRnqooCzqSFfNELsMJ2yp6umjkPm8RUxnxM", "user_agent": null}
0 2 \ No newline at end of file
... ... @@ -0,0 +1,10 @@
  1 +{"web":
  2 + {"client_id":"884418547933-e08agllr5v2kp2r3i68ld31ptl3bj4rt.apps.googleusercontent.com",
  3 + "project_id":"pikicast.com:api-project-884418547933",
  4 + "auth_uri":"https://accounts.google.com/o/oauth2/auth",
  5 + "token_uri":"https://accounts.google.com/o/oauth2/token",
  6 + "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
  7 + "client_secret":"yHgeC50DdYVLTjdHwG00eRjA",
  8 + "redirect_uris":["http://localhost:8080/"]
  9 + }
  10 +}
0 11 \ No newline at end of file
... ... @@ -0,0 +1,87 @@
  1 +import argparse
  2 +
  3 +from googleapiclient.discovery import build
  4 +import httplib2
  5 +from oauth2client import client
  6 +from oauth2client import file
  7 +from oauth2client import tools
  8 +
  9 +SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
  10 +DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')
  11 +CLIENT_SECRETS_PATH = '/var/root/PycharmProjects/untitled1/pong/cli.json' # Path to client_secrets.json file.
  12 +VIEW_ID = '84815165'
  13 +
  14 +
  15 +def initialize_analyticsreporting():
  16 +
  17 + # Parse command-line arguments.
  18 + parser = argparse.ArgumentParser(
  19 + formatter_class=argparse.RawDescriptionHelpFormatter,
  20 + parents=[tools.argparser])
  21 + flags = parser.parse_args([])
  22 +
  23 + # Set up a Flow object to be used if we need to authenticate.
  24 + flow = client.flow_from_clientsecrets(
  25 + CLIENT_SECRETS_PATH, scope=SCOPES,
  26 + message=tools.message_if_missing(CLIENT_SECRETS_PATH))
  27 +
  28 + # Prepare credentials, and authorize HTTP object with them.
  29 + # If the credentials don't exist or are invalid run through the native client
  30 + # flow. The Storage object will ensure that if successful the good
  31 + # credentials will get written back to a file.
  32 +
  33 + storage = file.Storage('analyticsreporting.dat')
  34 + credentials = storage.get()
  35 + if credentials is None or credentials.invalid:
  36 + credentials = tools.run_flow(flow, storage, flags)
  37 + http = credentials.authorize(http=httplib2.Http())
  38 +
  39 + # Build the service object.
  40 + analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)
  41 +
  42 + return analytics
  43 +
  44 +def get_report(analytics):
  45 + # Use the Analytics Service Object to query the Analytics Reporting API V4.
  46 + return analytics.reports().batchGet(
  47 + body={
  48 + 'reportRequests': [
  49 + {
  50 + 'viewId': VIEW_ID,
  51 + 'dateRanges': [{'startDate': '1daysAgo', 'endDate': 'today'}],
  52 + 'metrics': [{'expression': 'ga:Users'}]
  53 + }]
  54 + }
  55 + ).execute()
  56 +
  57 +
  58 +def print_response(response):
  59 +
  60 +
  61 + for report in response.get('reports', []):
  62 + columnHeader = report.get('columnHeader', {})
  63 + dimensionHeaders = columnHeader.get('dimensions', [])
  64 + metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
  65 + rows = report.get('data', {}).get('rows', [])
  66 +
  67 + for row in rows:
  68 + dimensions = row.get('dimensions', [])
  69 + dateRangeValues = row.get('metrics', [])
  70 +
  71 + for header, dimension in zip(dimensionHeaders, dimensions):
  72 + print header + ': ' + dimension
  73 +
  74 + for i, values in enumerate(dateRangeValues):
  75 + print 'Date range (' + str(i) + ')'
  76 + for metricHeader, value in zip(metricHeaders, values.get('values')):
  77 + print metricHeader.get('name') + ': ' + value
  78 +
  79 +
  80 +def main():
  81 +
  82 + analytics = initialize_analyticsreporting()
  83 + response = get_report(analytics)
  84 + print_response(response)
  85 +
  86 +if __name__ == '__main__':
  87 + main()
0 88 \ No newline at end of file