|  | @@ -8,9 +8,9 @@ import pandas as pd
 | 
	
		
			
				|  |  |  import random
 | 
	
		
			
				|  |  |  import urllib
 | 
	
		
			
				|  |  |  import json
 | 
	
		
			
				|  |  | -import gspread
 | 
	
		
			
				|  |  | +# import gspread
 | 
	
		
			
				|  |  |  import datetime
 | 
	
		
			
				|  |  | -from gspread_pandas import Spread, Client
 | 
	
		
			
				|  |  | +# from gspread_pandas import Spread, Client
 | 
	
		
			
				|  |  |  from oauth2client.service_account import ServiceAccountCredentials
 | 
	
		
			
				|  |  |  import os
 | 
	
		
			
				|  |  |  import threading
 | 
	
	
		
			
				|  | @@ -27,14 +27,12 @@ db.query('delete from ga_pagepath')
 | 
	
		
			
				|  |  |  db.begin()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  table=db['ga_pagepath']
 | 
	
		
			
				|  |  | +property_id= 'properties/375530887'
 | 
	
		
			
				|  |  |  SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
 | 
	
		
			
				|  |  |  platform_now = platform.system().lower()
 | 
	
		
			
				|  |  | -if platform_now == 'linux':
 | 
	
		
			
				|  |  | -  KEY_FILE_LOCATION = '/root/src/kw_tools/monitor/choozmo-ga-beee24b7a4c1.json' #'c:\\keys\\choozmo-ga-beee24b7a4c1.json'
 | 
	
		
			
				|  |  | -elif platform_now == 'windows':
 | 
	
		
			
				|  |  | -  KEY_FILE_LOCATION = 'c:\gitlab\kw_tools\monitor\corded-velocity-301807-9262189e4dd5.json'
 | 
	
		
			
				|  |  | -else:
 | 
	
		
			
				|  |  | -  KEY_FILE_LOCATION = '/Users/zooeytsai/Downloads/corded-velocity-301807-9262189e4dd5.json'
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +KEY_FILE_LOCATION = "C:\/Users\/s1301\/Downloads\/corded-velocity-301807-9262189e4dd5.json"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  VIEW_ID = '213054263'
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -75,17 +73,18 @@ def send_msg_pg(pv):
 | 
	
		
			
				|  |  |      # print(r.text)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  def initialize_analyticsreporting():
 | 
	
		
			
				|  |  |    """Initializes an Analytics Reporting API V4 service object.
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    Returns:
 | 
	
		
			
				|  |  |      An authorized Analytics Reporting API V4 service object.
 | 
	
		
			
				|  |  |    """
 | 
	
		
			
				|  |  | -  credentials = ServiceAccountCredentials.from_json_keyfile_name(
 | 
	
		
			
				|  |  | -      KEY_FILE_LOCATION, SCOPES)
 | 
	
		
			
				|  |  | +  credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    # Build the service object.
 | 
	
		
			
				|  |  | -  analytics = build('analyticsreporting', 'v4', credentials=credentials)
 | 
	
		
			
				|  |  | +  analytics = build('analyticsdata', 'v1beta', credentials=credentials)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    return analytics
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -98,12 +97,12 @@ def get_report(analytics,body):
 | 
	
		
			
				|  |  |    Returns:
 | 
	
		
			
				|  |  |      The Analytics Reporting API V4 response.
 | 
	
		
			
				|  |  |    """
 | 
	
		
			
				|  |  | -  return analytics.reports().batchGet(
 | 
	
		
			
				|  |  | -      body={
 | 
	
		
			
				|  |  | -        'reportRequests':body
 | 
	
		
			
				|  |  | -      }
 | 
	
		
			
				|  |  | -  ).execute()
 | 
	
		
			
				|  |  | +  response = analytics.properties().batchRunReports(property=property_id, body=body).execute()
 | 
	
		
			
				|  |  | +  print(response)
 | 
	
		
			
				|  |  | +  report_data = response.get('reports')
 | 
	
		
			
				|  |  | +  pv = report_data[0].get('rows')[0].get('metricValues')[0].get('value')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +  return pv
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  def print_response(response):
 | 
	
		
			
				|  |  |    """Parses and prints the Analytics Reporting API V4 response.
 | 
	
	
		
			
				|  | @@ -136,31 +135,41 @@ def print_response(response):
 | 
	
		
			
				|  |  |  #        print(ga_dict)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  def main():
 | 
	
		
			
				|  |  | -  creat_table()
 | 
	
		
			
				|  |  | +  # creat_table()
 | 
	
		
			
				|  |  |    analytics = initialize_analyticsreporting()
 | 
	
		
			
				|  |  | +  print(analytics)
 | 
	
		
			
				|  |  |  #(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)
 | 
	
		
			
				|  |  |    current_time = datetime.now().strftime('%Y-%m-%d') #現在時間
 | 
	
		
			
				|  |  | -  body=[{ 'viewId': VIEW_ID,
 | 
	
		
			
				|  |  | -  'dateRanges': [{'startDate': current_time, 'endDate': current_time}],
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -  'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
 | 
	
		
			
				|  |  | -#  'dimensions': [{'name': 'ga:pagePath'}],
 | 
	
		
			
				|  |  | -#  'orderBys':[{"fieldName": "ga:pageviews", "sortOrder": "DESCENDING"}],
 | 
	
		
			
				|  |  | -  'pageSize': '100'
 | 
	
		
			
				|  |  | -  }]
 | 
	
		
			
				|  |  | +  body={
 | 
	
		
			
				|  |  | +  "requests": [
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +      "dateRanges": [
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +          "startDate": "2023-08-08",
 | 
	
		
			
				|  |  | +          "endDate": "2023-08-08"
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +      ],
 | 
	
		
			
				|  |  | +      "metrics": [
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +          "name": "screenPageViews"
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +      ],
 | 
	
		
			
				|  |  | +      "limit": "100"
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +  ]
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    response = get_report(analytics,body)
 | 
	
		
			
				|  |  | -  ga_dict=print_response(response)
 | 
	
		
			
				|  |  | +  print(response)
 | 
	
		
			
				|  |  | +  # ga_dict=print_response(response)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  result=[]
 | 
	
		
			
				|  |  | -  for elmt in ga_dict:
 | 
	
		
			
				|  |  | -    print(elmt)
 | 
	
		
			
				|  |  | -    hour = datetime.now().strftime('%H')
 | 
	
		
			
				|  |  | -    if int(hour)+1 > 8 :
 | 
	
		
			
				|  |  | -      message = send_msg_pg(elmt['ga:pageviews'])
 | 
	
		
			
				|  |  | -#      result.append(elmt)
 | 
	
		
			
				|  |  | -  print('inserting.....')
 | 
	
		
			
				|  |  | -  return message
 | 
	
		
			
				|  |  | +  # for elmt in ga_dict:
 | 
	
		
			
				|  |  | +  #   print(elmt)
 | 
	
		
			
				|  |  | +  #   hour = datetime.now().strftime('%H')
 | 
	
		
			
				|  |  | +  #   if int(hour)+1 > 8 :
 | 
	
		
			
				|  |  | +  #     message = send_msg_pg(elmt['ga:pageviews'])
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  # return message
 | 
	
		
			
				|  |  |  if __name__ == '__main__':
 | 
	
		
			
				|  |  | -  creat_table()
 | 
	
		
			
				|  |  | +  # creat_table()
 | 
	
		
			
				|  |  |    main()
 |