我正在运行一个脚本来从Google Analytics中的细分中提取会话数据。我可以为给定的数据范围提取总会话数,但我希望按天计算数据。我该怎么做?
def get_service(api_name, api_version, scope, key_file_location,
service_account_email):
"""Get a service that communicates to a Google API.
Args:
api_name: The name of the api to connect to.
api_version: The api version to connect to.
scope: A list auth scopes to authorize for the application.
key_file_location: The path to a valid service account p12 key file.
service_account_email: The service account email address.
Returns:
A service that is connected to the specified API.
"""
credentials = ServiceAccountCredentials.from_p12_keyfile(
service_account_email, key_file_location, scopes=scope)
http = credentials.authorize(httplib2.Http())
# Build the service object.
service = build(api_name, api_version, http=http)
return service
def get_segment_sessions(service, profile_id, segment_id):
return service.data().ga().get(
ids='ga:' + profile_id,
segment='gaid::' + segment_id,
start_date='7daysAgo',
end_date='yesterday',
metrics='ga:sessions').execute()
def main():
# Define the auth scopes to request.
scope = ['https://www.googleapis.com/auth/analytics.readonly']
# Use the developer console and replace the values with your
# service account email and relative location of your key file.
service_account_email = '************'
key_file_location = '*********'
# Authenticate and construct service.
service = get_service('analytics', 'v3', scope, key_file_location,
service_account_email)
get_segments(service)
#print odd video sessions
print_results(get_segment_sessions(service, profile, '9rZpyfi7Rzq2cU3xvZABbw'))
#print even video sessions
print_results(get_segment_sessions(service, profile, '7T3JreTGQnuhIa0i3TZsSg'))
if __name__ == '__main__':
main()