DIMENSIONS = ['ga:source']
METRICS = ['ga:users']
def initialize_analyticsreporting():
credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)
# Build the service object.
analytics = build('analyticsreporting', 'v4', credentials=credentials)
return analytics
def get_report(analytics):
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '14daysAgo', 'endDate': 'today'}],
'metrics': [{'expression':i} for i in METRICS],
'dimensions': [{'name':j} for j in DIMENSIONS],
"metricFilterClauses": # Описание фильтра начинается здесь.
[
{
"filters":
[
{
"dimensionName": "ga:source",
"operator": "EXACT",
"expressions": ["google"]
}
]
}
]
}]
}
).execute()