[英]How to get Top 50 Sessions from Google Analytics Reporting API v4 Python
I've got an analytics reporting api v4 script in Python which returns to me the sessions for certain landing pages and which city the sessions have come from. 我有一个使用Python进行分析报告的API v4脚本,该脚本向我返回了某些登陆页面的会话以及会话来自哪个城市。
Is there a way to limit this to only the top 50 city locations per landing page for sessions so I don't get returned the city's that are landing on them which have like 1 session for example. 有没有一种方法可以将其限制为每个登陆页面在会话中仅排名前50位的城市,这样我就不会返回正在登陆的城市(例如,有1个会话)。
Here is the base code for the analytics request: 这是分析请求的基本代码:
api_client = google_build(serviceName=api_name, version=api_version,
http=authorized)
for i in developments:
sample_request = {
'viewId': '6690350',
'pageSize': 100000,
'dateRanges': {
'startDate': datetime.strftime(datetime.now() - timedelta(days = 30),'%Y-%m-%d'),
'endDate': datetime.strftime(datetime.now(),'%Y-%m-%d')
},
'metrics': [{'expression': 'ga:sessions'}],
'orderBys':
[
{'fieldName': 'ga:pagePath', 'sortOrder': 'ASCENDING'},
{'fieldName': 'ga:sessions', 'sortOrder': 'DESCENDING'}
],
'dimensions': [{'name': 'ga:pagePath'},{'name': 'ga:city'},{'name': 'ga:latitude'},{'name': 'ga:longitude'}],
'dimensionFilterClauses' : [
{
EDIT - To get top x
cities for a particular landing page is not feasible through a single query, you will have to either use Content Grouping feature or process the broader response to achieve this. 编辑-要通过单个查询获取特定登陆页面的前
x
个城市是不可行的,您将必须使用内容分组功能或处理更广泛的响应才能实现此目的。
Original response - Added to your code might not be complete, look at metricFilterClauses section added only 原始回复-添加到您的代码中的内容可能不完整, 请仅查看metricFilterClauses部分
You can get the top 50 results by using pagination and using PageSize to restrict the number of rows that you get. 您可以使用分页和使用PageSize来限制获得的行数,从而获得前50个结果。 Just make sure to use
ordering
to sort before you apply pagination. 只需在应用分页之前确保使用
ordering
进行排序。
You will have to use batchGet request
to execute this - link to official guide of batchquest 您将必须使用
batchGet request
来执行此操作- 链接到batchquest的官方指南
api_client = google_build(serviceName=api_name, version=api_version,
http=authorized)
for i in developments:
sample_request = {
'viewId': '6690350',
'pageSize': 100000,
'dateRanges': {
'startDate': datetime.strftime(datetime.now() - timedelta(days = 30),'%Y-%m-%d'),
'endDate': datetime.strftime(datetime.now(),'%Y-%m-%d')
},
'metrics': [{'expression': 'ga:sessions'}],
#add this metric filter to filter out with session more than 50
'metricFilterClauses': [{
"filters": [{
"metricName": "ga:sessions",
"operator": "GREATER_THAN",
"comparisonValue": "50"
}]
}]
'orderBys':
[
{'fieldName': 'ga:pagePath', 'sortOrder': 'ASCENDING'},
{'fieldName': 'ga:sessions', 'sortOrder': 'DESCENDING'}
],
'dimensions': [{'name': 'ga:pagePath'},{'name': 'ga:city'},{'name': 'ga:latitude'},{'name': 'ga:longitude'}]
#top 50 results
'pageSize': 50
You can use MetricFilterClauses to filter out particular metric that you need. 您可以使用MetricFilterClauses筛选出所需的特定指标。 You can use different operators available to you to filter out based on conditions you want to use.
您可以使用各种可用的运算符来根据要使用的条件进行过滤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.