A
A
AREX2019-06-27 13:27:13
Google
AREX, 2019-06-27 13:27:13

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()

But when I try to set a filter, for example, by ga:source
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()

It gives an error:
Invalid value at 'report_requests[0].metric_filter_clauses[0].filters[0].operator' (TYPE_ENUM), "EXACT"
Invalid JSON payload received. Unknown name "expressions" at 'report_requests[0].metric_filter_clauses[0].filters[0]': Cannot find field.">
Tell me what the problem might be. As a result, the filter works for any indicators, but not for any parameters.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2019-06-27
@arrexq

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

R
Roman, 2019-06-27
@JohnnyBravo7

Well, everything is just zhezh. The API can't get you those values ​​because referral is medium :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question