如何构建 google 分析查询以避免配额限制?
How to construct google analytics query to avoid quota limits?
这是我请求的一部分。在 2 年的时间里,我每天都有 725 个此类请求。
我正在获取我正在创建的特定数据集的 30 天流量分析。
当我尝试查询所有 725 个数据集的分析时,我收到配额错误 Requests per user per 100 seconds
,即使我在每个请求之前放置了 time.pause(2)
。
我还能做些什么来避免达到 API 配额?
{
"reportRequests":[
{
"viewId":"104649158",
"dateRanges":[
{
"startDate":"2017-12-01",
"endDate":"2017-12-31"
}
],
"metrics":[
{
"expression":"ga:pageviews"
},
{
"expression":"ga:uniquePageviews"
},
{
"expression":"ga:pageviewsPerSession"
},
{
"expression":"ga:timeOnPage"
},
{
"expression":"ga:avgTimeOnPage"
},
{
"expression":"ga:entrances"
},
{
"expression":"ga:entranceRate"
},
{
"expression":"ga:exitRate"
},
{
"expression":"ga:exits"
}
],
"dimensions":[
{
"name":"ga:pagePathLevel2"
}
],
"dimensionFilterClauses":[
{
"filters":[
{
"dimensionName":"ga:pagePathLevel2",
"operator":"REGEXP",
"expressions":[
"23708|23707|23706|23705|23704|23703|23702|23701|23700|23699|23698|23697|23696|23695|23694|23693|23692"
]
}
]
}
]
}
]
}
1) 您应该将用户配额增加到 1000 个请求(如果尚未完成),方法是进入您的 Coogle 云控制台 -> 左上角菜单 -> API 服务与服务 -> 分析报告 API -> 配额:
https://console.cloud.google.com/apis/api/analyticsreporting.googleapis.com/quotas
2) 您可以增加时间范围并使用ga:yearMonth
dimension to still get your monthly breakdown. However you might face sampling issues: since your query is "custom" (you use a filter + dimension), sampling will apply if for the given time range the total number of sessions at property level exceeds 500K (regardless of how many are actually included in the response). In this case there is no absolute answer, you have to find the time ranges that suit you best. samplesReadCounts / samplingSpaceSizes will help you detect sampling, and if required you will need to handle pagination。
虽然您可以请求增加配额是正确的。这将增加您可以发出的请求总数仍然有限。
In the API Console, there is a similar quota referred to as Requests per 100 seconds per user. By default, it is set to 100 requests per 100 seconds per user and can be adjusted to a maximum value of 1,000. But the number of requests to the API is restricted to a maximum of 10 requests per second per user.
每个用户每 100 秒的请求数
是基于用户的配额,它链接到每个用户配额每秒最多 10 个请求(每个 IP 地址每秒 10 个查询 (QPS))。这基本上是防洪。它可以防止单个用户针对 api 发出许多请求,从而使其余用户难以使用 api.
您首先需要了解的是,每个用户每秒 100 个请求是非常主观的。当你 运行 你的请求时,你真的没有办法知道你的请求将 运行 在哪个服务器上,如果你的唯一一个 运行 在那个服务器上,那么你可能会开始10 秒内 100 个请求,然后在接下来的 90 秒内被阻止。
配额用户
你需要知道的第二件事是,基于用户通常意味着基于 ip,所以如果这些请求可能违背不同的观点,但如果所有 运行ning 来自同一个 IP 地址,这可能会导致一些混乱并且它假定您是同一个用户。为了解决这个问题,你可以使用一个名为 quota User 的备用参数,你可以在每个请求中向它发送一个随机字符串,它可以帮助它不会完全减少它 google 最终会捕捉到你正在做的事情。
quotaUser An arbitrary string that uniquely identifies a user.
Lets you enforce per-user quotas from a server-side application even in cases when the user's IP address is unknown. This can occur, for example, with applications that run cron jobs on App Engine on a user's behalf.
You can choose any arbitrary string that uniquely identifies a user, but it is limited to 40 characters.
Learn more about Capping API usage.
实施指数退避
Google 通常建议您实施一种称为指数退避的方法,这基本上意味着您尝试一个请求,如果它失败,然后您等待几秒钟,如果失败再试一次,那么您等待的时间是您的两倍之前等待然后再试一次你这样做大约 10 次,通常你可以通过。
如果您使用的是官方 google 客户端库之一,其中大多数已经实现了指数退避
洪水克星
我不久前写了一篇关于我称为 flood buster 的文章,它是一种跟踪我将尝试多快并防止用户配额错误的方法代码在 C# 中,你可能会发现它很有用 flood buster
不是真正的问题
虽然收到这些错误可能很丑陋,但您应该再次发出请求并不重要。 Google 除非您一次持续数小时,否则不会将此错误计入您的错误。
每个项目 2000 个请求 100 秒
您需要记住,您的项目每 100 秒总共可以发出的请求数是 2000。这个不能增加。
因此,如果您有两个用户,每个用户每 100 秒消耗 1000 个请求,您将达到基于项目的配额,您对此无能为力。允许一个用户吃掉你所有的配额在我看来不是一个好主意,除非这是一个单用户应用程序。
这是我请求的一部分。在 2 年的时间里,我每天都有 725 个此类请求。
我正在获取我正在创建的特定数据集的 30 天流量分析。
当我尝试查询所有 725 个数据集的分析时,我收到配额错误 Requests per user per 100 seconds
,即使我在每个请求之前放置了 time.pause(2)
。
我还能做些什么来避免达到 API 配额?
{
"reportRequests":[
{
"viewId":"104649158",
"dateRanges":[
{
"startDate":"2017-12-01",
"endDate":"2017-12-31"
}
],
"metrics":[
{
"expression":"ga:pageviews"
},
{
"expression":"ga:uniquePageviews"
},
{
"expression":"ga:pageviewsPerSession"
},
{
"expression":"ga:timeOnPage"
},
{
"expression":"ga:avgTimeOnPage"
},
{
"expression":"ga:entrances"
},
{
"expression":"ga:entranceRate"
},
{
"expression":"ga:exitRate"
},
{
"expression":"ga:exits"
}
],
"dimensions":[
{
"name":"ga:pagePathLevel2"
}
],
"dimensionFilterClauses":[
{
"filters":[
{
"dimensionName":"ga:pagePathLevel2",
"operator":"REGEXP",
"expressions":[
"23708|23707|23706|23705|23704|23703|23702|23701|23700|23699|23698|23697|23696|23695|23694|23693|23692"
]
}
]
}
]
}
]
}
1) 您应该将用户配额增加到 1000 个请求(如果尚未完成),方法是进入您的 Coogle 云控制台 -> 左上角菜单 -> API 服务与服务 -> 分析报告 API -> 配额:
https://console.cloud.google.com/apis/api/analyticsreporting.googleapis.com/quotas
2) 您可以增加时间范围并使用ga:yearMonth
dimension to still get your monthly breakdown. However you might face sampling issues: since your query is "custom" (you use a filter + dimension), sampling will apply if for the given time range the total number of sessions at property level exceeds 500K (regardless of how many are actually included in the response). In this case there is no absolute answer, you have to find the time ranges that suit you best. samplesReadCounts / samplingSpaceSizes will help you detect sampling, and if required you will need to handle pagination。
虽然您可以请求增加配额是正确的。这将增加您可以发出的请求总数仍然有限。
In the API Console, there is a similar quota referred to as Requests per 100 seconds per user. By default, it is set to 100 requests per 100 seconds per user and can be adjusted to a maximum value of 1,000. But the number of requests to the API is restricted to a maximum of 10 requests per second per user.
每个用户每 100 秒的请求数
是基于用户的配额,它链接到每个用户配额每秒最多 10 个请求(每个 IP 地址每秒 10 个查询 (QPS))。这基本上是防洪。它可以防止单个用户针对 api 发出许多请求,从而使其余用户难以使用 api.
您首先需要了解的是,每个用户每秒 100 个请求是非常主观的。当你 运行 你的请求时,你真的没有办法知道你的请求将 运行 在哪个服务器上,如果你的唯一一个 运行 在那个服务器上,那么你可能会开始10 秒内 100 个请求,然后在接下来的 90 秒内被阻止。
配额用户
你需要知道的第二件事是,基于用户通常意味着基于 ip,所以如果这些请求可能违背不同的观点,但如果所有 运行ning 来自同一个 IP 地址,这可能会导致一些混乱并且它假定您是同一个用户。为了解决这个问题,你可以使用一个名为 quota User 的备用参数,你可以在每个请求中向它发送一个随机字符串,它可以帮助它不会完全减少它 google 最终会捕捉到你正在做的事情。
quotaUser An arbitrary string that uniquely identifies a user.
Lets you enforce per-user quotas from a server-side application even in cases when the user's IP address is unknown. This can occur, for example, with applications that run cron jobs on App Engine on a user's behalf. You can choose any arbitrary string that uniquely identifies a user, but it is limited to 40 characters. Learn more about Capping API usage.
实施指数退避
Google 通常建议您实施一种称为指数退避的方法,这基本上意味着您尝试一个请求,如果它失败,然后您等待几秒钟,如果失败再试一次,那么您等待的时间是您的两倍之前等待然后再试一次你这样做大约 10 次,通常你可以通过。
如果您使用的是官方 google 客户端库之一,其中大多数已经实现了指数退避
洪水克星
我不久前写了一篇关于我称为 flood buster 的文章,它是一种跟踪我将尝试多快并防止用户配额错误的方法代码在 C# 中,你可能会发现它很有用 flood buster
不是真正的问题
虽然收到这些错误可能很丑陋,但您应该再次发出请求并不重要。 Google 除非您一次持续数小时,否则不会将此错误计入您的错误。
每个项目 2000 个请求 100 秒
您需要记住,您的项目每 100 秒总共可以发出的请求数是 2000。这个不能增加。
因此,如果您有两个用户,每个用户每 100 秒消耗 1000 个请求,您将达到基于项目的配额,您对此无能为力。允许一个用户吃掉你所有的配额在我看来不是一个好主意,除非这是一个单用户应用程序。