GAのAPIをたたいて、pythonでデータを抽出したいのですが、データが抽出できません。
バージョンは以下です。宜しくお願いします。
python3.7
windows10
python3
1 2"""Hello Analytics Reporting API V4.""" 3import os 4 5from apiclient.discovery import build 6from oauth2client.service_account import ServiceAccountCredentials 7 8import httplib2 9 10BASE_DIR = os.path.dirname(os.path.abspath(__file__)) 11KEY_FILE_LOCATION = os.path.join(BASE_DIR, 'test-5acf.p12') 12SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'] 13DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest') 14 15SERVICE_ACCOUNT_EMAIL = 'ono-xxx@xxxxxxx.com' 16VIEW_ID = 'xxxxxxx' 17 18 19def initialize_analyticsreporting(): 20 """Initializes an analyticsreporting service object. 21 22 Returns: analytics an authorized analyticsreporting service 23 object. 24 25 """ 26 27 credentials = ServiceAccountCredentials.from_p12_keyfile( 28 SERVICE_ACCOUNT_EMAIL, KEY_FILE_LOCATION, scopes=SCOPES) 29 30 http = credentials.authorize(httplib2.Http()) 31 32 # Build the service object. 33 analytics = build('analytics', 'v4', http=http, 34 discoveryServiceUrl=DISCOVERY_URI) 35 36 return analytics 37 38 39def get_report(analytics): 40 # Use the Analytics Service Object to query the Analytics Reporting API V4. 41 return analytics.reports().batchGet( 42 body={ 43 'reportRequests': [ 44 { 45 'viewId': VIEW_ID, 46 'pageSize': 10, 47 'dateRanges': [ 48 {'startDate': '7daysAgo', 'endDate': 'today'} 49 ], 50 'metrics': [ 51 {'expression': 'ga:pageviews'}, 52 ], 53 'dimensions': [ 54 {'name': 'ga:pagePath'}, {'name': 'ga:pageTitle'} 55 ], 56 'orderBys': [ 57 {'fieldName': 'ga:pageviews', 'sortOrder': 'DESCENDING'}, 58 ] 59 }] 60 } 61 ).execute() 62 63 64def print_response(response): 65 """Parses and prints the Analytics Reporting API V4 response.""" 66 67 for report in response.get('reports', []): 68 rows = report.get('data', {}).get('rows', []) 69 for row in rows: 70 print(row) 71 72 73def main(): 74 75 analytics = initialize_analyticsreporting() 76 response = get_report(analytics) 77 print_response(response) 78 79 80if __name__ == '__main__': 81 main()
> データが抽出できません。
ということですが、実行した結果は、どのような結果になるのでしょうか?
結果を具体的に記載していただけないでしょうか?
エラーが出るならエラーメッセージを記載してください。
結果は、何もでず処理おわりますが、値は何も返っこないです。
Googleアナリティクスのデータが無いと言うことなのでしょうか。
ちなみに、working.pyというフォルダにディレクトリ名のみ残ります。
あなたの回答
tips
プレビュー