11
11
from google .analytics .data_v1beta import BetaAnalyticsDataClient
12
12
from google .analytics .data_v1beta .types import DateRange , Dimension , Metric , RunReportRequest
13
13
14
- PORTAL_ID = os . environ [ 'PORTAL_ID' ]
15
- FOUNDATIONS_ID = os . environ [ 'FOUNDATIONS_ID' ]
16
- COOKBOOKS_ID = os . environ [ 'COOKBOOKS_ID' ]
14
+ PORTAL_ID = '266784902'
15
+ FOUNDATIONS_ID = '281776420'
16
+ COOKBOOKS_ID = '324070631'
17
17
18
18
PRIVATE_KEY_ID = os .environ .get ('PRIVATE_KEY_ID' )
19
19
PRIVATE_KEY = os .environ .get ('PRIVATE_KEY' )
34
34
35
35
client = BetaAnalyticsDataClient .from_service_account_info (credentials_dict )
36
36
37
+ pre_project_date = '2020-03-31' # random date before project start
38
+
37
39
38
40
def _format_rounding (value ):
39
41
return f'{ round (value / 1000 , 1 ):.1f} K'
@@ -44,7 +46,7 @@ def _run_total_users_report(property_id):
44
46
property = f'properties/{ property_id } ' ,
45
47
dimensions = [],
46
48
metrics = [Metric (name = 'activeUsers' )],
47
- date_ranges = [DateRange (start_date = '2020-03-31' , end_date = 'today' )],
49
+ date_ranges = [DateRange (start_date = pre_project_date , end_date = 'today' )],
48
50
)
49
51
response = client .run_report (request )
50
52
@@ -110,7 +112,7 @@ def _run_top_pages_report(property_id):
110
112
request = RunReportRequest (
111
113
property = f'properties/{ property_id } ' ,
112
114
dimensions = [Dimension (name = 'pageTitle' )],
113
- date_ranges = [DateRange (start_date = '2020-03-31' , end_date = 'today' )],
115
+ date_ranges = [DateRange (start_date = pre_project_date , end_date = 'today' )],
114
116
metrics = [Metric (name = 'screenPageViews' )],
115
117
)
116
118
response = client .run_report (request )
@@ -167,7 +169,7 @@ def _run_usersXcountry_report(property_id):
167
169
property = f'properties/{ property_id } ' ,
168
170
dimensions = [Dimension (name = 'country' )],
169
171
metrics = [Metric (name = 'activeUsers' )],
170
- date_ranges = [DateRange (start_date = '2020-03-31' , end_date = 'today' )],
172
+ date_ranges = [DateRange (start_date = pre_project_date , end_date = 'today' )],
171
173
)
172
174
response = client .run_report (request )
173
175
@@ -183,6 +185,7 @@ def _run_usersXcountry_report(property_id):
183
185
def plot_usersXcountry (FOUNDATIONS_ID ):
184
186
users_by_country = _run_usersXcountry_report (FOUNDATIONS_ID )
185
187
188
+ # Google API Country names do not match Cartopy Country Shapefile names
186
189
dict_api2cartopy = {
187
190
'Tanzania' : 'United Republic of Tanzania' ,
188
191
'United States' : 'United States of America' ,
0 commit comments