核心报告 API - 如何使用多个 dimensionFilterClauses 过滤器?

Core Reporting API - How to use multiple dimensionFilterClauses filters?

我正在尝试将多个 dimensionFilterClauses 用于 Core Reporting API V4 查询。如果我只在 ga:adwordsCustomerID 维度上使用一个过滤器,一切正常,但是当我在 ga:adTargetingType 维度上添加第二个过滤器时,它会抛出一个 "Status 400: Bad request" 错误。

这是我写的查询:

    return analytics.reports().batchGet(
        body={"reportRequests": [{
            "pageSize": 10000,
            "viewId": VIEW_ID,
            "dateRanges": [
                {"startDate": "31daysAgo", "endDate": "yesterday"}
            ],
            "dimensions": [
                {"name": "ga:adwordsCampaignID"},
                {"name": "ga:adwordsAdGroupID"},
                {"name": "ga:adwordsCriteriaID"}
            ],
            "metrics": [
                {"expression": "ga:adClicks"},
                {"expression": "ga:adCost"},
                {"expression": "ga:uniquePurchases"},
                {"expression": "ga:itemRevenue"},
                {"expression": "ga:CPC"},
                {"expression": "ga:ROAS"}
            ],
            "dimensionFilterClauses": [
                {"filters": [
                    {"dimensionName": "ga:adwordsCustomerID",
                     "operator": "EXACT",
                     "expressions": ["2096809090"]},
                    {"dimensionName": "ga:adTargetingType",
                     "operator": "EXACT",
                     "expressions": ["Keyword"]}
                ]}
            ],
            "metricFilterClauses": [
                {"filters": [
                    {"metricName": "ga:adCost",
                     "operator": "GREATER_THAN",
                     "comparisonValue": "0"}
                ]}
            ],
            "orderBys": [
                {"fieldName": "ga:adClicks",
                 "sortOrder": "DESCENDING"}
            ]}
        ]}
    ).execute()

你知道上面的查询正文有什么问题吗?

分析报告 API V4 Filtering

ReportRequest 接受一个 DimensionFilterClauses 的数组。这些子句与逻辑运算符 AND 组合在一起。 IE。如果你有两个 DimensionFilterClause 对象:AB; API 只会 return 满足 A AND B.

两个条件的值

每个 DimensionFilterClause 接受一个 DimensionFilters 的数组(称为 filters)。这些过滤器与逻辑运算符 OR 相结合。 IE。如果在 DimensionFilterClause 中有两个 DimensionFilter 对象 CD; API 会 return 结果满足 C 或 D.

例子

下面是一个包含两个 DimensionFilterClauses 的请求示例; ga:adWordsCampaignID==8675309 AND ga:adwordsAdGroupID==12345 API Explorer example:

{
  "reportRequests": 
  [
    {
      "viewId": "XXXX",
      "dimensions": 
      [
        {"name": "ga:adwordsCampaignID"},
        {"name": "ga:adwordsAdGroupID"},
        {"name": "ga:adwordsCriteriaID"}
      ],
      "metrics": 
      [
        {"expression": "ga:adClicks"},
        {"expression": "ga:adCost"}
      ],
      "metricFilterClauses": 
      [
        {
          "filters": 
          [
            {
              "metricName": "ga:adCost",
              "operator": "GREATER_THAN",
              "comparisonValue": "0"
            }
          ]
        }
      ],
      "dimensionFilterClauses": 
      [
        {
          "filters": 
          [
            {
              "dimensionName": "ga:adwordsCampaignID",
              "operator": "EXACT",
              "expressions": ["8675309"]
            }
          ]
        },
        {
          "filters": 
          [
            {
              "dimensionName": "ga:adwordsAdGroupID",
              "operator": "EXACT",
              "expressions": 
              ["12345"],
            }
          ]
        }
      ]
    }
  ]
}

结论

从一开始,您的 API 请求正文似乎没有任何问题,如果有,您会收到一条错误消息。但更有可能的是您不打算请求:

        "dimensionFilterClauses": [
            {"filters": [
                {"dimensionName": "ga:adwordsCustomerID",
                 "operator": "EXACT",
                 "expressions": ["2096809090"]},
                {"dimensionName": "ga:adTargetingType",
                 "operator": "EXACT",
                 "expressions": ["Keyword"]}
            ]}
        ],

但您打算请求:

        "dimensionFilterClauses": [
            {"filters": [
                {"dimensionName": "ga:adwordsCustomerID",
                 "operator": "EXACT",
                 "expressions": ["2096809090"]},
            ]},{"filters": [
                {"dimensionName": "ga:adTargetingType",
                 "operator": "EXACT",
                 "expressions": ["Keyword"]}
            ]}
        ],

当没有数据出现时,我的建议是先删除过滤器,然后验证您是否拥有要搜索的确切字符串。此外,如果您没有完整的字符串,您可以使用不同的 filter.operator,例如 PARTIALBEGINS_WITH.

错误处理

使用正确的错误处理也很聪明,尤其是在调试时:

try:
  response = analyticsreporting.reports().batchGet(
    body=requestBody
  ).execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print 'There was an error in constructing your query : %s' % error

except HttpError, error:
  # Handle API errors.
  print ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))

已更新:ga:adTargetingType==Keyword 示例

从下面的评论中,要求给出一个带有 ga:adTargetingType==Keyword 的维度过滤器的例子。使用 API Example here 向自己证明它有效(只需将 VIEW_ID 更改为您的视图视图 ID 并点击 "Authorize and Execute")。 JSON正文如下:

{
  "reportRequests": 
  [
    {
      "viewId": "VIEW_ID",
      "metrics": [{"expression": "ga:sessions"}],
      "dimensions": [{"name": "ga:adTargetingType"}],
      "dimensionFilterClauses": 
      [
        {
          "filters": 
          [
            {
              "dimensionName": "ga:adTargetingType",
              "operator": "EXACT",
              "expressions": ["Keyword"]
            }
          ]
        }
      ]
    }
  ]
}

我总是喜欢从小事做起,逐步发展。通过删除其他参数和字段,我可以向自己证明什么有效,什么无效。此示例是过滤 ga:keyword==Keyword.

的最低要求请求

第二次更新:

您收到的实际错误信息如下:

"Selected dimensions and metrics cannot be queried together."

无法使用以下指标查询维度 ga:adTargetingType:

  • ga:impressions
  • ga:adClicks
  • ga:adCost
  • ga:CPM
  • ga:CPC
  • ga:CTR
  • ga:costPerTransaction
  • ga:costPerGoalConversion
  • ga:costPerConversion
  • ga:RPC
  • ga:ROAS
"dimensions": 
      [
        {"name": "ga:adwordsCampaignID"},
        {"name": "ga:adwordsAdGroupID"},
        {"name": "ga:adwordsCriteriaID"}
      ],
      "metrics": 
      [
        {"expression": "ga:adClicks"},
        {"expression": "ga:adCost"}
      ],

代码将能够调用多个维度而无需 filter.If 你想知道哪些维度和指标,请使用 link: GA Dimensions and Metrics

此页面中缺少的一条重要信息是 FilterLogicalOperator 的使用。这是用于组合多个维度过滤器的运算符。如果未指定,则将其视为 OR。所以,上面说默认是 AND 的答案是不正确的。

为了应用两个过滤器,我们需要在代码中添加 FilterLogicalOperator:

"dimensionFilterClauses": [
            {"operator": "AND",
             "filters": [
                {"dimensionName": "ga:adwordsCustomerID",
                 "operator": "EXACT",
                 "expressions": ["2096809090"]},
                {"dimensionName": "ga:adTargetingType",
                 "operator": "EXACT",
                 "expressions": ["Keyword"]}
            ]}
        ],