简体   繁体   English

如何从Google Analytics Reporting API v4 Python获取前50个会话

[英]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.

相关问题 将 pageToken 与 Google Analytics Reporting API v4 和 Python 结合使用 - Use of pageToken with Google Analytics Reporting API v4 and Python 报告 API v4 谷歌分析 engagemntRate - Reporting API v4 Google analytics engagemntRate 分析报告 API V4 - Analytics Reporting API V4 googleapiclient batch获取Google Analytics Reporting API V4的序列 - googleapiclient batchGet sequences Google Analytics Reporting API V4 Google Analytics Reporting API v4 排序结果 - Google Analytics Reporting API v4 sort results 使用Python将Google Analytics(分析)Reporting API V4请求结果转换为csv - Converting Google Analytics Reporting API V4 request results to csv with Python 将变量传递给 batchGet() 请求 - 谷歌分析报告 API v4 / Python / 循环 - Passing a variable to a batchGet() request - Google Analytics Reporting API v4 / Python / loop Google Analytics(分析)报告 API v4:nextPagetoken 不能 go 超过 10,000 行(Python) - Google Analytics Reporting API v4: nextPagetoken cannot go beyond 10,000 rows (Python) 在 Google Analytics(分析)报告 API v4 中查询超过 10k 行限制 - Querying past the 10k row limit in Google Analytics Reporting API v4 AttributeError:“ dict”对象没有属性“ to_csv” Google Analytics API报告V4 - AttributeError: 'dict' object has no attribute 'to_csv' Google Analytics API Reporting V4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM