-
Notifications
You must be signed in to change notification settings - Fork 1
/
reporting.py
142 lines (114 loc) · 4.73 KB
/
reporting.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
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
from apiclient.discovery import build
#from oauth2client.service_account import ServiceAccountCredentials
import json
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
from initialize import Initialize
class Reporting:
IN_initialize = Initialize()
VIEW_ID = IN_initialize.VIEW_ID
ACCOUNT_ID = IN_initialize.ACCOUNT_ID
WEB_PROPERTY_ID = IN_initialize.WEB_PROPERTY_ID
def get_gtm(self,analytics):
gtm = analytics['gtm']
accounts = gtm.accounts().list().execute()
return accounts
def get_management(self,analytics):
#API V4 does not have management resource so we use V3 for this
analytics_v3 = analytics['analytics_v3']
custom_dimensions = analytics_v3.management().customDimensions().list(
accountId = self.ACCOUNT_ID,
webPropertyId = self.WEB_PROPERTY_ID
).execute()
custom_metrics = analytics_v3.management().customMetrics().list(
accountId = self.ACCOUNT_ID,
webPropertyId = self.WEB_PROPERTY_ID
).execute()
adwords_linked = analytics_v3.management().webPropertyAdWordsLinks().list(
accountId = self.ACCOUNT_ID,
webPropertyId = self.WEB_PROPERTY_ID
).execute()
goals = analytics_v3.management().goals().list(
accountId = self.ACCOUNT_ID,
webPropertyId = self.WEB_PROPERTY_ID,
profileId = self.VIEW_ID
).execute()
#print json.dumps(custom_metrics,sort_keys=True,indent=2)
customMetrics_list = []
for metric in custom_metrics.get('items',[]):
metric_name = metric.get('name')
print metric_name
customMetrics_list.append(metric_name)
customDimensions_list = []
for dimension in custom_dimensions.get('items',[]):
Cdimension_name = dimension.get('name')
customDimensions_list.append(Cdimension_name)
goals_list = []
for goal in goals.get('items',[]):
goal_name = goal.get('name')
goals_list.append(goal_name)
managemnt_get = {'customDimensions_list':customDimensions_list, 'adwords_linked':adwords_linked,
'goals_list':goals_list, 'customMetrics_list':customMetrics_list}
return managemnt_get
def get_report(self,analytics):
"""Using the Analytics Service Object to query the Analytics Reporting API V4.
"""
#VIEW_ID = IN_initialize.VIEW_ID
analytics_v4 = analytics['analytics_v4']
report = analytics_v4.reports().batchGet(
body={
'reportRequests': [
{
'viewId': self.VIEW_ID,
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:searchSessions'}, {'expression': 'ga:totalEvents'},
{'expression':'ga:totalValue'}]
},
{
'viewId': self.VIEW_ID,
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'dimensions': [{'name': 'ga:fullReferrer'}, {'name': 'ga:medium'}]
}
]
}
).execute()
return report
def print_response(self,response):
"""Parses and prints the Analytics Reporting API V4 response"""
fullReferrer_list = []
medium_list = []
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
rows = report.get('data', {}).get('rows', [])
for row in rows:
dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])
for header, dimension in zip(dimensionHeaders, dimensions):
#global fullReferrer_list
if(header == 'ga:fullReferrer'):
fullReferrer_list.append(dimension)
if(header == 'ga:medium'):
#global medium_list
medium_list.append(dimension)
#print header + ': ' + dimension
for i, values in enumerate(dateRangeValues):
#print 'Date range (' + str(i) + ')'
for metricHeader, value in zip(metricHeaders, values.get('values')):
#print metricHeader.get('name') + ': ' + value
if(metricHeader.get('name') == 'ga:searchSessions'):
#global searchSessions
searchSessions = value
if(metricHeader.get('name') == 'ga:totalEvents'):
#global totalEvents
totalEvents = value
if(metricHeader.get('name') == 'ga:totalValue'):
#global totalValue
totalValue = value
useful_values = {'searchSessions':searchSessions,'totalEvents':totalEvents,
'totalValue':totalValue, 'fullReferrer_list':fullReferrer_list,
'medium_list':medium_list}
return useful_values