gatest.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import sys
  2. import codecs
  3. import traceback
  4. import requests
  5. import re
  6. import pandas as pd
  7. import random
  8. import urllib
  9. import dataset
  10. import json
  11. import gspread
  12. import datetime
  13. from gspread_pandas import Spread, Client
  14. from oauth2client.service_account import ServiceAccountCredentials
  15. import os
  16. import threading
  17. from apiclient.discovery import build
  18. from oauth2client.service_account import ServiceAccountCredentials
  19. import dataset
  20. # GA: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet?apix_params=%7B%22resource%22%3A%7B%7D%7D#%E8%AF%B7%E6%B1%82%E6%AD%A3%E6%96%87
  21. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  22. db.query('delete from hhh_weekly_report') # delete DB content
  23. table = db['hhh_weekly_report'] # DB table
  24. SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'] # Oauth範圍
  25. KEY_FILE_LOCATION = 'choozmo-ga-beee24b7a4c1.json'
  26. VIEW_ID = '188916214'
  27. def initialize_analyticsreporting():
  28. """Initializes an Analytics Reporting API V4 service object.
  29. Returns:
  30. An authorized Analytics Reporting API V4 service object.
  31. """
  32. # GA金鑰
  33. credentials = ServiceAccountCredentials.from_json_keyfile_name(
  34. KEY_FILE_LOCATION, SCOPES)
  35. # Build the service object. 建立GA物件
  36. analytics = build('analyticsreporting', 'v4', credentials=credentials)
  37. return analytics
  38. def get_report(analytics,body):
  39. """Queries the Analytics Reporting API V4.
  40. Args:
  41. analytics: An authorized Analytics Reporting API V4 service object.
  42. Returns:
  43. The Analytics Reporting API V4 response.
  44. """
  45. return analytics.reports().batchGet( #返回 Google Analytics(分析)数据。
  46. body={
  47. 'reportRequests':body
  48. # [
  49. #{
  50. # 'viewId': VIEW_ID,
  51. # 'dateRanges': [{'startDate': '14daysAgo', 'endDate': 'today'}],
  52. # 'dateRanges': [{'startDate': '2021-05-30', 'endDate': '2021-06-05'}],
  53. # 'metrics': [{'expression': 'ga:users'}],
  54. # 'dimensions': [{'name': 'ga:sourceMedium'}]
  55. # 'dimensions': [{'name': 'ga:date'},{'name': 'ga:sourceMedium'}]
  56. # }]
  57. }
  58. ).execute()
  59. def print_response(response):
  60. """Parses and prints the Analytics Reporting API V4 response.
  61. Args:
  62. response: An Analytics Reporting API V4 response.
  63. """
  64. result=[]
  65. for report in response.get('reports', []): # 取得報表s
  66. columnHeader = report.get('columnHeader', {})
  67. dimensionHeaders = columnHeader.get('dimensions', [])
  68. metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
  69. for row in report.get('data', {}).get('rows', []):
  70. dimensions = row.get('dimensions', [])
  71. dateRangeValues = row.get('metrics', [])
  72. ga_dict={}
  73. for header, dimension in zip(dimensionHeaders, dimensions):
  74. print(header + ': ', dimension)
  75. ga_dict[header]=dimension
  76. for i, values in enumerate(dateRangeValues):
  77. print('Date range:', str(i))
  78. for metricHeader, value in zip(metricHeaders, values.get('values')):
  79. ga_dict[metricHeader.get('name')]=value
  80. # print(metricHeader.get('name') + ':', value)
  81. result.append(ga_dict)
  82. return result
  83. # print(ga_dict)
  84. def main():
  85. analytics = initialize_analyticsreporting()
  86. #(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)
  87. body=[{ 'viewId': VIEW_ID, # 从中提取数据的 Google Analytics(分析)数据视图 ID。
  88. # 'dateRanges': [{'startDate': '2021-05-31', 'endDate': '2021-06-18'}], # 请求中的日期范围。请求最多可以有 2 个日期范围。
  89. 'dateRanges': [{'startDate': '2021-05-31', 'endDate': '2021-08-05'}],
  90. # 'filtersExpression': 'ga:sourceMedium=~(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)',
  91. 'filtersExpression': 'ga:sourceMedium=~(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)', # 限制针对您的请求所返回数据的维度或指标过滤条件。
  92. # 请求的指标。请求必须指定至少一个指标。请求总共可有 10 个指标。
  93. 'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
  94. # 请求的维度。请求总共可有 7 个维度。
  95. 'dimensions': [{'name': 'ga:date'},{'name': 'ga:isoWeek'}]
  96. }]
  97. response = get_report(analytics,body)
  98. ga_dict = print_response(response)
  99. for elmt in ga_dict:
  100. elmt['category']='social'
  101. table.insert(elmt)
  102. body=[{ 'viewId': VIEW_ID,
  103. # 'dateRanges': [{'startDate': '2021-05-24', 'endDate': '2021-06-10'}],
  104. # 'dateRanges': [{'startDate': '2021-05-31', 'endDate': '2021-06-18'}],
  105. # 'dateRanges': [{'startDate': '2021-05-31', 'endDate': '2021-06-25'}],
  106. 'dateRanges': [{'startDate': '2021-05-31', 'endDate': '2021-08-05'}],
  107. # 'filtersExpression': 'ga:sourceMedium=~(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)',
  108. 'filtersExpression': 'ga:sourceMedium!~(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)',
  109. 'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
  110. 'dimensions': [{'name': 'ga:date'},{'name': 'ga:isoWeek'}]
  111. }]
  112. response = get_report(analytics,body)
  113. ga_dict=print_response(response)
  114. for elmt in ga_dict:
  115. elmt['category']='organic'
  116. table.insert(elmt)
  117. if __name__ == '__main__':
  118. main()