1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2014 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Reference command-line example for Google Analytics Core Reporting API v3.
This application demonstrates how to use the python client library to access
all the pieces of data returned by the Google Analytics Core Reporting API v3.
The application manages autorization by saving an OAuth2.0 token in a local
file and reusing the token for subsequent requests.
Before You Begin:
Update the client_secrets.json file
You must update the clients_secrets.json file with a client id, client
secret, and the redirect uri. You get these values by creating a new project
in the Google APIs console and registering for OAuth2.0 for installed
applications: https://code.google.com/apis/console
Learn more about registering your analytics application here:
http://developers.google.com/analytics/devguides/reporting/core/v3/gdataAuthorization
Supply your TABLE_ID
You will also need to identify from which profile to access data by
specifying the TABLE_ID constant below. This value is of the form: ga:xxxx
where xxxx is the profile ID. You can get the profile ID by either querying
the Management API or by looking it up in the account settings of the
Google Anlaytics web interface.
Sample Usage:
$ python core_reporting_v3_reference.py ga:xxxx
Where the table ID is used to identify from which Google Anlaytics profile
to retrieve data. This ID is in the format ga:xxxx where xxxx is the
profile ID.
Also you can also get help on all the command-line flags the program
understands by running:
$ python core_reporting_v3_reference.py --help
"""
from __future__ import print_function
__author__ = "api.nickm@gmail.com (Nick Mihailovski)"
import argparse
import sys
from googleapiclient.errors import HttpError
from googleapiclient import sample_tools
from oauth2client.client import AccessTokenRefreshError
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
"table_id",
type=str,
help=(
"The table ID of the profile you wish to access. "
"Format is ga:xxx where xxx is your profile ID."
),
)
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv,
"analytics",
"v3",
__doc__,
__file__,
parents=[argparser],
scope="https://www.googleapis.com/auth/analytics.readonly",
)
# Try to make a request to the API. Print the results or handle errors.
try:
results = get_api_query(service, flags.table_id).execute()
print_results(results)
except TypeError as error:
# Handle errors in constructing a query.
print(("There was an error in constructing your query : %s" % error))
except HttpError as error:
# Handle API errors.
print(
(
"Arg, there was an API error : %s : %s"
% (error.resp.status, error._get_reason())
)
)
except AccessTokenRefreshError:
# Handle Auth errors.
print(
"The credentials have been revoked or expired, please re-run "
"the application to re-authorize"
)
def get_api_query(service, table_id):
"""Returns a query object to retrieve data from the Core Reporting API.
Args:
service: The service object built by the Google API Python client library.
table_id: str The table ID form which to retrieve data.
"""
return (
service.data()
.ga()
.get(
ids=table_id,
start_date="2012-01-01",
end_date="2012-01-15",
metrics="ga:visits",
dimensions="ga:source,ga:keyword",
sort="-ga:visits",
filters="ga:medium==organic",
start_index="1",
max_results="25",
)
)
def print_results(results):
"""Prints all the results in the Core Reporting API Response.
Args:
results: The response returned from the Core Reporting API.
"""
print_report_info(results)
print_pagination_info(results)
print_profile_info(results)
print_query(results)
print_column_headers(results)
print_totals_for_all_results(results)
print_rows(results)
def print_report_info(results):
"""Prints general information about this report.
Args:
results: The response returned from the Core Reporting API.
"""
print("Report Infos:")
print("Contains Sampled Data = %s" % results.get("containsSampledData"))
print("Kind = %s" % results.get("kind"))
print("ID = %s" % results.get("id"))
print("Self Link = %s" % results.get("selfLink"))
print()
def print_pagination_info(results):
"""Prints common pagination details.
Args:
results: The response returned from the Core Reporting API.
"""
print("Pagination Infos:")
print("Items per page = %s" % results.get("itemsPerPage"))
print("Total Results = %s" % results.get("totalResults"))
# These only have values if other result pages exist.
if results.get("previousLink"):
print("Previous Link = %s" % results.get("previousLink"))
if results.get("nextLink"):
print("Next Link = %s" % results.get("nextLink"))
print()
def print_profile_info(results):
"""Prints information about the profile.
Args:
results: The response returned from the Core Reporting API.
"""
print("Profile Infos:")
info = results.get("profileInfo")
print("Account Id = %s" % info.get("accountId"))
print("Web Property Id = %s" % info.get("webPropertyId"))
print("Profile Id = %s" % info.get("profileId"))
print("Table Id = %s" % info.get("tableId"))
print("Profile Name = %s" % info.get("profileName"))
print()
def print_query(results):
"""The query returns the original report query as a dict.
Args:
results: The response returned from the Core Reporting API.
"""
print("Query Parameters:")
query = results.get("query")
for key, value in query.iteritems():
print("%s = %s" % (key, value))
print()
def print_column_headers(results):
"""Prints the information for each column.
The main data from the API is returned as rows of data. The column
headers describe the names and types of each column in rows.
Args:
results: The response returned from the Core Reporting API.
"""
print("Column Headers:")
headers = results.get("columnHeaders")
for header in headers:
# Print Dimension or Metric name.
print(
"\t%s name: = %s"
% (header.get("columnType").title(), header.get("name"))
)
print("\tColumn Type = %s" % header.get("columnType"))
print("\tData Type = %s" % header.get("dataType"))
print()
def print_totals_for_all_results(results):
"""Prints the total metric value for all pages the query matched.
Args:
results: The response returned from the Core Reporting API.
"""
print("Total Metrics For All Results:")
print("This query returned %s rows." % len(results.get("rows")))
print(("But the query matched %s total results." % results.get("totalResults")))
print("Here are the metric totals for the matched total results.")
totals = results.get("totalsForAllResults")
for metric_name, metric_total in totals.iteritems():
print("Metric Name = %s" % metric_name)
print("Metric Total = %s" % metric_total)
print()
def print_rows(results):
"""Prints all the rows of data returned by the API.
Args:
results: The response returned from the Core Reporting API.
"""
print("Rows:")
if results.get("rows", []):
for row in results.get("rows"):
print("\t".join(row))
else:
print("No Rows Found")
if __name__ == "__main__":
main(sys.argv)
|