|
@@ -1,4 +1,4 @@
|
|
|
-from datetime import datetime
|
|
|
+from datetime import datetime, timedelta
|
|
|
import os
|
|
|
import requests
|
|
|
from google.analytics.data_v1beta import BetaAnalyticsDataClient
|
|
@@ -12,11 +12,11 @@ from google.analytics.data_v1beta.types import (
|
|
|
|
|
|
# IMPORTANT: Please set up GOOGLE_APPLICATION_CREDENTIALS through export (Bash) OR Environment Variables (Windows).
|
|
|
# GOOGLE_APPLICATION_CREDENTIALS = "[path to credentials.json file]"
|
|
|
+os.environ['GOOGLE_APPLICATION_CREDENTIALS']="C:\/Users\/s1301\/Downloads\/corded-velocity-301807-b52c79741854.json"
|
|
|
|
|
|
# Insert PROPERTY IDs you want to track here.
|
|
|
# Format: ["property_ID","property_name"]
|
|
|
# KEEP THIS IN TUPLE FORM EVEN IF ONLY ONE DATA SOURCE IS USED !!
|
|
|
-os.environ['GOOGLE_APPLICATION_CREDENTIALS']="C:\/Users\/s1301\/Downloads\/corded-velocity-301807-b52c79741854.json"
|
|
|
# property_id = [["336444388","Icons_EN"],["336427321","Icons_ZH"]] # CHANGE HERE
|
|
|
property_id = [["336444388","Icons_EN"]] # CHANGE HERE
|
|
|
|
|
@@ -38,34 +38,45 @@ headers = {
|
|
|
|
|
|
def send_msg(msg):
|
|
|
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
- params = {"message": "Time: " + current_time + "\n" + msg}
|
|
|
- # r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
|
|
|
+ params = {"message": "\nTime: " + current_time + "\n" + msg}
|
|
|
+ r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
|
|
|
|
|
|
def run_report():
|
|
|
client = BetaAnalyticsDataClient()
|
|
|
- total = 0
|
|
|
message = ""
|
|
|
-
|
|
|
+
|
|
|
+ dailypvtotal = {}
|
|
|
+ dailypvtotal[(datetime.now() - timedelta(days=1)).strftime('%Y%m%d')]=0
|
|
|
+ dailypvtotal[datetime.now().strftime('%Y%m%d')]=0
|
|
|
+
|
|
|
for p in property_id:
|
|
|
+ message = message + p[1] + "\n"
|
|
|
request = RunReportRequest(
|
|
|
property=f"properties/{p[0]}",
|
|
|
+ dimensions=[Dimension(name="date")],
|
|
|
metrics=[Metric(name="screenPageViews")],
|
|
|
- date_ranges=[DateRange(start_date="today", end_date="today")],
|
|
|
+ date_ranges=[DateRange(start_date="yesterday", end_date="today")],
|
|
|
)
|
|
|
response = client.run_report(request)
|
|
|
|
|
|
for rowIdx, row in enumerate(response.rows):
|
|
|
for i, metric_value in enumerate(row.metric_values):
|
|
|
- metric_name = response.metric_headers[i].name
|
|
|
- viewcount = metric_value.value
|
|
|
- message = message + (f"{p[1]} / {metric_name}: {viewcount}\n")
|
|
|
- total = total + int(viewcount)
|
|
|
- message = message + (f"Total views: {total}\n")
|
|
|
- if show_target == 1:
|
|
|
- if total >= pv_target:
|
|
|
- message = message + ("Target reached\n")
|
|
|
+ for i, dimension_value in enumerate(row.dimension_values):
|
|
|
+ date=dimension_value.value
|
|
|
+ viewcount = metric_value.value
|
|
|
+ message = message + (f"{date}: {viewcount}\n")
|
|
|
+ dailypvtotal[date]=dailypvtotal[date]+int(viewcount)
|
|
|
+ message = message + '\n'
|
|
|
+
|
|
|
+ for d,v in dailypvtotal.items():
|
|
|
+ message = message + (f"{d} Total views: {v} ")
|
|
|
+ if show_target == 1:
|
|
|
+ if v >= pv_target:
|
|
|
+ message = message + ("(Target reached)\n")
|
|
|
+ else:
|
|
|
+ message = message + ("(Target not reached)\n")
|
|
|
else:
|
|
|
- message = message + ("Target not reached\n")
|
|
|
+ message = message + '\n'
|
|
|
print(message)
|
|
|
if send == 1:
|
|
|
send_msg(message)
|