GA4LN_base.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. from datetime import datetime
  3. import requests
  4. from google.analytics.data_v1beta import BetaAnalyticsDataClient
  5. from google.analytics.data_v1beta.types import (
  6. DateRange,
  7. Dimension,
  8. Metric,
  9. MetricType,
  10. RunReportRequest,
  11. )
  12. # IMPORTANT: Please set up GOOGLE_APPLICATION_CREDENTIALS through export (Bash) OR Environment Variables (Windows).
  13. # GOOGLE_APPLICATION_CREDENTIALS = "[path to credentials.json file]"
  14. # Insert PROPERTY IDs you want to track here.
  15. # Format: ["property_ID","property_name"]
  16. # KEEP THIS IN TUPLE FORM EVEN IF ONLY ONE DATA SOURCE IS USED !!
  17. # os.environ['GOOGLE_APPLICATION_CREDENTIALS']='C:\/Users\/s1301\/Downloads\/corded-velocity-301807-879b27e60620.json'+os.pathsep+'C:\/Users\/s1301\/Downloads\/corded-velocity-301807-c1a87c5dfc8d.json'
  18. property_id = [
  19. ["290055766","Simpleprotein - GA4",{
  20. "Authorization": "Bearer " + "DC7eiXkGoKzXcuCTuqCGmuNnDC15D3iv6E7tu5BAsUZ",
  21. "Content-Type": "application/x-www-form-urlencoded"},"C:\/Users\/s1301\/Downloads\/corded-velocity-301807-879b27e60620.json"],
  22. ["349445560","www.lisinbeauty.com",{"Authorization": "Bearer " + "4RO6ZOS4b7TkuqknajBPibTnOYnumHfwJvRzdZ3Bmq6",
  23. "Content-Type": "application/x-www-form-urlencoded"},"C:\/Users\/s1301\/Downloads\/corded-velocity-301807-c1a87c5dfc8d.json"]] # CHANGE HERE
  24. # Insert PageView target here.
  25. pv_target = 0
  26. # Show whether target is achieved or not. Set to 1 to enable.
  27. show_target = 0
  28. # Enable/disable send to LINE Notify. Set to 1 to enable.
  29. send = 1
  30. # Insert destination LINE Group ID here.
  31. def send_msg(msg,lineid):
  32. current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  33. params = {"message": "Time: " + current_time + "\n" + msg}
  34. # r = requests.post("https://notify-api.line.me/api/notify",headers=lineid, params=params)
  35. def run_report():
  36. total = 0
  37. message = ""
  38. for p in property_id:
  39. os.environ['GOOGLE_APPLICATION_CREDENTIALS']=p[3]
  40. client = BetaAnalyticsDataClient()
  41. request = RunReportRequest(
  42. property=f"properties/{p[0]}",
  43. metrics=[Metric(name="screenPageViews")],
  44. date_ranges=[DateRange(start_date="2023-02-19", end_date="2023-02-19")],
  45. )
  46. response = client.run_report(request)
  47. for rowIdx, row in enumerate(response.rows):
  48. for i, metric_value in enumerate(row.metric_values):
  49. metric_name = response.metric_headers[i].name
  50. viewcount = metric_value.value
  51. message = message + (f"{p[1]} / {metric_name}: {viewcount}\n")
  52. total = total + int(viewcount)
  53. message = message + (f"Total views: {total}\n")
  54. if show_target == 1:
  55. if total >= pv_target:
  56. message = message + ("Target reached\n")
  57. else:
  58. message = message + ("Target not reached\n")
  59. print(message)
  60. send_msg(message,p[2])
  61. run_report()