import os from datetime import datetime import requests from google.analytics.data_v1beta import BetaAnalyticsDataClient from google.analytics.data_v1beta.types import ( DateRange, Dimension, Metric, MetricType, RunReportRequest, ) # IMPORTANT: Please set up GOOGLE_APPLICATION_CREDENTIALS through export (Bash) OR Environment Variables (Windows). # GOOGLE_APPLICATION_CREDENTIALS = "[path to credentials.json file]" # 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-879b27e60620.json'+os.pathsep+'C:\/Users\/s1301\/Downloads\/corded-velocity-301807-c1a87c5dfc8d.json' property_id = [ ["290055766","Simpleprotein - GA4",{ "Authorization": "Bearer " + "DC7eiXkGoKzXcuCTuqCGmuNnDC15D3iv6E7tu5BAsUZ", "Content-Type": "application/x-www-form-urlencoded"},"C:\/Users\/s1301\/Downloads\/corded-velocity-301807-879b27e60620.json"], ["349445560","www.lisinbeauty.com",{"Authorization": "Bearer " + "4RO6ZOS4b7TkuqknajBPibTnOYnumHfwJvRzdZ3Bmq6", "Content-Type": "application/x-www-form-urlencoded"},"C:\/Users\/s1301\/Downloads\/corded-velocity-301807-c1a87c5dfc8d.json"]] # CHANGE HERE # Insert PageView target here. pv_target = 0 # Show whether target is achieved or not. Set to 1 to enable. show_target = 0 # Enable/disable send to LINE Notify. Set to 1 to enable. send = 1 # Insert destination LINE Group ID here. def send_msg(msg,lineid): 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=lineid, params=params) def run_report(): total = 0 message = "" for p in property_id: os.environ['GOOGLE_APPLICATION_CREDENTIALS']=p[3] client = BetaAnalyticsDataClient() request = RunReportRequest( property=f"properties/{p[0]}", metrics=[Metric(name="screenPageViews")], date_ranges=[DateRange(start_date="2023-02-19", end_date="2023-02-19")], ) 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") else: message = message + ("Target not reached\n") print(message) send_msg(message,p[2]) run_report()