-
Notifications
You must be signed in to change notification settings - Fork 1
/
top_nodes.py
92 lines (69 loc) · 2.84 KB
/
top_nodes.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import re
# import the Auth Helper class
import analytics_auth
from apiclient.errors import HttpError
from oauth2client.client import AccessTokenRefreshError
def main(argv):
# Step 1. Get an analytics service object.
service = analytics_auth.initialize_service()
try:
# Step 2. Get the user's first profile ID.
profile_id = get_aitopics_profile_id(service)
if profile_id:
# Step 3. Query the Core Reporting API.
start_date = argv[1]
end_date = argv[2]
results = get_results(service, profile_id, start_date, end_date)
# Step 4. Output the results.
print_results(results)
except TypeError, error:
# Handle errors in constructing a query.
print ('There was an error in constructing your query : %s' % error)
except HttpError, 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_aitopics_profile_id(service):
# Get a list of all Google Analytics accounts for this user
accounts = service.management().accounts().list().execute()
if accounts.get('items'):
# Get the first Google Analytics account
firstAccountId = accounts.get('items')[2].get('id')
# Get a list of all the Web Properties for the first account
webproperties = service.management().webproperties().list(accountId=firstAccountId).execute()
if webproperties.get('items'):
# Get the first Web Property ID
firstWebpropertyId = webproperties.get('items')[0].get('id')
# Get a list of all Profiles for the first Web Property of the first Account
profiles = service.management().profiles().list(
accountId=firstAccountId,
webPropertyId=firstWebpropertyId).execute()
if profiles.get('items'):
# return the first Profile ID
return profiles.get('items')[0].get('id')
return None
def get_results(service, profile_id, start_date, end_date):
# Use the Analytics Service Object to query the Core Reporting API
return service.data().ga().get(
ids='ga:' + profile_id,
start_date=start_date,
end_date=end_date,
dimensions='ga:pagePath',
metrics='ga:uniquePageViews,ga:pageViews').execute()
def print_results(results):
# Print data nicely for the user.
if results:
pages = sorted(results.get('rows'), key=lambda row: int(row[1]), reverse=True)
pages = filter(lambda row: re.match(r'/(news|video|publication|link|topic|tag|podcast|misc|classic).*', row[0]), pages)
for page in pages:
print '(%d/%d) %s' % (int(page[1]), int(page[2]), page[0])
else:
print 'No results found'
main(sys.argv)