我尝试将多个dimensionFilterClauses用于Core Reporting API V4查询。如果我只使用ga:adTargetingType
维度上的过滤器一切正常,但是当我在 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()
维度上添加第二个过滤器时,它会抛出一个"状态400:错误请求"错误。
这是我写的查询:
String regex, updatedXml;
// 1. remove all white space preceding a begin element tag:
regex = "[\\n\\s]+(\\<[^/])";
updatedXml = originalXmlStr.replaceAll( regex, "$1" );
// 2. remove all white space following an end element tag:
regex = "(\\</[a-zA-Z0-9-_\\.:]+\\>)[\\s]+";
updatedXml = updatedXml.replaceAll( regex, "$1" );
// 3. remove all white space following an empty element tag
// (<some-element xmlns:attr1="some-value".... />):
regex = "(/\\>)[\\s]+";
updatedXml = updatedXml.replaceAll( regex, "$1" );
您知道上述查询正文有什么问题吗?
答案 0 :(得分:7)
ReportRequest采用DimensionFilterClauses
数组。这些子句与逻辑 AND
运算符结合使用。 IE浏览器。如果您有两个DimensionFilterClause
个对象: A 和 B ; API将仅返回满足A AND
B中的两个条件的值。
每个DimensionFilterClause
都会使用DimensionFilters
数组(称为filters
)。这些过滤器与逻辑 OR
运算符结合使用。 IE浏览器。如果DimensionFilter
中有两个DimensionFilterClause
个对象 C 和 D ; API将返回满足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
,例如PARTIAL
或BEGINS_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,然后点击&#34;授权并执行&#34;)。 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
答案 1 :(得分:0)
"dimensions":
[
{"name": "ga:adwordsCampaignID"},
{"name": "ga:adwordsAdGroupID"},
{"name": "ga:adwordsCriteriaID"}
],
"metrics":
[
{"expression": "ga:adClicks"},
{"expression": "ga:adCost"}
],
代码无需过滤即可调用多个维度。如果您想知道哪些维度和指标,请使用链接: GA Dimensions and Metrics
答案 2 :(得分:0)
此页面中缺少的重要信息是使用FilterLogicalOperator。这是用于组合多维过滤器的运算符。如果未指定,则将其视为OR。因此,上面的答案表明默认值为AND是不正确的。
为了同时应用两个过滤器,我们需要在代码中添加FilterLogicalOperator:
"dimensionFilterClauses": [
{"operator": "AND",
"filters": [
{"dimensionName": "ga:adwordsCustomerID",
"operator": "EXACT",
"expressions": ["2096809090"]},
{"dimensionName": "ga:adTargetingType",
"operator": "EXACT",
"expressions": ["Keyword"]}
]}
],