【行いたいこと】
google analyticsから自動でほしいデータを収集する。
【現状】
・google APIを作成し、そのサービスアカウントIDをgoogle analyticsの管理者として「表示と収集」のみを付与
・コマンドプロンプトにて以下を書き込み
pip install --upgrade google-api-python-client
・googleが提供しているHelloAnalytics.pyを追加(KEY_FILE_LOCATION,VIEW_IDにはgoogle APIのカギ、google analyticsのview ID)
php
1"""Hello Analytics Reporting API V4.""" 2 3from apiclient.discovery import build 4from oauth2client.service_account import ServiceAccountCredentials 5 6 7SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'] 8KEY_FILE_LOCATION = 'path' 9VIEW_ID = 'path' 10 11 12def initialize_analyticsreporting(): 13 """Initializes an Analytics Reporting API V4 service object. 14 15 Returns: 16 An authorized Analytics Reporting API V4 service object. 17 """ 18 credentials = ServiceAccountCredentials.from_json_keyfile_name( 19 KEY_FILE_LOCATION, SCOPES) 20 21 # Build the service object. 22 analytics = build('analyticsreporting', 'v4', credentials=credentials) 23 24 return analytics 25 26 27def get_report(analytics): 28 """Queries the Analytics Reporting API V4. 29 30 Args: 31 analytics: An authorized Analytics Reporting API V4 service object. 32 Returns: 33 The Analytics Reporting API V4 response. 34 """ 35 return analytics.reports().batchGet( 36 body={ 37 'reportRequests': [ 38 { 39 'viewId': VIEW_ID, 40 'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}], 41 'metrics': [{'expression': 'ga:sessions'}], 42 'dimensions': [{'name': 'ga:country'}] 43 }] 44 } 45 ).execute() 46 47 48def print_response(response): 49 """Parses and prints the Analytics Reporting API V4 response. 50 51 Args: 52 response: An Analytics Reporting API V4 response. 53 """ 54 for report in response.get('reports', []): 55 columnHeader = report.get('columnHeader', {}) 56 dimensionHeaders = columnHeader.get('dimensions', []) 57 metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', []) 58 59 for row in report.get('data', {}).get('rows', []): 60 dimensions = row.get('dimensions', []) 61 dateRangeValues = row.get('metrics', []) 62 63 for header, dimension in zip(dimensionHeaders, dimensions): 64 print (header + ': ' + dimension) 65 66 for i, values in enumerate(dateRangeValues): 67 print ('Date range: ' + str(i)) 68 for metricHeader, value in zip(metricHeaders, values.get('values')): 69 print (metricHeader.get('name') + ': ' + value) 70 71 72def main(): 73 analytics = initialize_analyticsreporting() 74 response = get_report(analytics) 75 print_response(response) 76 77if __name__ == '__main__': 78 main() 79
しかし、これを行った場合以下のようなエラーが出ます。。。(filepathについては省かしていただきます)
Traceback (most recent call last): File "HelloAnalytics.py", line 78, in <module> main() File "HelloAnalytics.py", line 73, in main analytics = initialize_analyticsreporting() File "HelloAnalytics.py", line 19, in initialize_analyticsreporting KEY_FILE_LOCATION, SCOPES) File "service_account.py", line 219, in from_json_keyfile_name with open(filename, 'r') as file_obj: FileNotFoundError: [Errno 2] No such file or directory:
ご助力をいただけたらと思います。。。
回答1件
あなたの回答
tips
プレビュー