Answer the question
In order to leave comments, you need to log in
How to filter by dimensions (ga api)?
Hello, tell me please. There is a query with a filter by metrics. He works and gives everything.
DIMENSIONS = ['ga:source','ga:medium']
METRICS = ['ga:users','ga:sessions','ga:adCost']
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":
[
{
"metricName": "ga:adCost",
"operator": "GREATER_THAN", # Значение показателя должно быть больше comparisonValue.
"comparisonValue": "0"
}
]
}
]
}]
}
).execute()
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()
Answer the question
In order to leave comments, you need to log in
check through the test if anything is returned at all
, you need to understand that Google does not provide data that threatens its business model
, for example, you cannot get the cost per click of a specific user from a specific country for a specific request
by the amount - as much as you like: all clicks from the USA, all clicks on request, all clicks from EN, sum up - divide as you like ...
so not all desired filters work, even if it seems that they should
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question