我正在创建一个网站仪表板供公司内部使用。我们办公室里有电视屏幕,我想显示我们8个最受欢迎网站的统计信息。每个站点应包含8张最近几周的用户/会话图表。我还想显示每个站点的活动用户。
我设法提出了一个脚本并编写了一个函数(在一些帮助下),因此我可以通过仅更改相应属性的视图ID而不是复制相同的代码八次来轻松地调用不同的站点。
我面临的问题是“配额错误:超出用户速率限制”。控制台中的错误,因为有8个站点。 2或3可以正常工作,但8似乎太多了。我已经提高了每用户每100秒的请求。也是1000。
有什么办法可以通过重写一些代码来减少API调用的次数?还是有另一种方式来实现这种仪表板的东西?
`
<div class="container-fluid">
<div class="row">
<div id="auth-button"></div>
<div class="col-lg-4 dashitem">
<h1>name</h1>
<figure class="Chartjs-figure" id="-container"></figure>
<ol class="Chartjs-legend" id="legend-1-container"></ol>
<div id="active-users-container1"></div>
</div>
<div class="col-lg-4 dashitem">
<h1>name</h1>
<figure class="Chartjs-figure" id="[name]-container"></figure>
<ol class="Chartjs-legend" id="legend-2-container"></ol>
<div id="active-users-container2"></div>
</div>
<div class="col-lg-4 dashitem">
<h1>name</h1>
<figure class="Chartjs-figure" id="[name]-container"></figure>
<ol class="Chartjs-legend" id="legend-3-container"></ol>
<div id="active-users-container3"></div>
</div>
<div class="col-lg-4 dashitem">
<h1>name</h1>
<figure class="Chartjs-figure" id="[name]-container"></figure>
<ol class="Chartjs-legend" id="legend-4-container"></ol>
<div id="active-users-container4"></div>
</div>
<div class="col-lg-4 dashitem">
<h1>name</h1>
<figure class="Chartjs-figure" id="[name]-container"></figure>
<ol class="Chartjs-legend" id="legend-5-container"></ol>
<div id="active-users-container5"></div>
</div>
<div class="col-lg-4 dashitem">
<h1>name</h1>
<figure class="Chartjs-figure" id="[name]-container"></figure>
<ol class="Chartjs-legend" id="legend-6-container"></ol>
<div id="active-users-container6"></div>
</div>
<div class="col-lg-4 dashitem">
<h1>name</h1>
<figure class="Chartjs-figure" id="[name]-container"></figure>
<ol class="Chartjs-legend" id="legend-7-container"></ol>
<div id="active-users-container7"></div>
</div>
<div class="col-lg-4 dashitem">
<h1>name</h1>
<figure class="Chartjs-figure" id="[name]-container"></figure>
<ol class="Chartjs-legend" id="legend-8-container"></ol>
<div id="active-users-container8"></div>
</div>
</div>
</div>
{literal}
<script>
gapi.analytics.ready(function() {
var CLIENT_ID = '[client-id]';
gapi.analytics.auth.authorize({
container: 'auth-button',
clientid: CLIENT_ID,
userInfoLabel:""
});
gapi.analytics.auth.on('success', function(response) {
//hide the auth-button
document.querySelector("#auth-button").style.display='none';
});
function renderIndividualChart(id, chartId, legendId, activeUsersId) {
// Adjust `now` to experiment with different days, for testing only...
var now = moment(); // .subtract(3, 'day');
var thisWeek = query({
'ids': "ga:" + id,
'dimensions': 'ga:date,ga:nthDay',
'metrics': 'ga:users',
'start-date': moment(now).subtract(1, 'day').day(0).format('YYYY-MM-DD'),
'end-date': moment(now).format('YYYY-MM-DD')
});
var lastWeek = query({
'ids': "ga:" + id,
'dimensions': 'ga:date,ga:nthDay',
'metrics': 'ga:users',
'start-date': moment(now).subtract(1, 'day').day(0).subtract(1, 'week')
.format('YYYY-MM-DD'),
'end-date': moment(now).subtract(1, 'day').day(6).subtract(1, 'week')
.format('YYYY-MM-DD')
});
Promise.all([thisWeek, lastWeek]).then(function(results) {
var data1 = results[0].rows.map(function(row) { return +row[2]; });
var data2 = results[1].rows.map(function(row) { return +row[2]; });
var labels = results[1].rows.map(function(row) { return +row[0]; });
labels = labels.map(function(label) {
return moment(label, 'YYYYMMDD').format('ddd');
});
var data = {
labels : labels,
datasets : [
{
label: 'Vorige Week',
fillColor : 'rgba(220,220,220,0.5)',
strokeColor : 'rgba(220,220,220,1)',
pointColor : 'rgba(220,220,220,1)',
pointStrokeColor : '#fff',
data : data2
},
{
label: 'Deze Week',
fillColor : 'rgba(151,187,205,0.5)',
strokeColor : 'rgba(151,187,205,1)',
pointColor : 'rgba(151,187,205,1)',
pointStrokeColor : '#fff',
data : data1
}
]
};
new Chart(makeCanvas(chartId)).Line(data);
generateLegend(legendId, data.datasets);
});
var activeUsers = new gapi.analytics.ext.ActiveUsers({
ids: "ga:" + id,
container: activeUsersId,
pollingInterval: 5
});
//activeUsers.execute();
}
function query(params) {
return new Promise(function(resolve, reject) {
var data = new gapi.analytics.report.Data({query: params});
data.once('success', function(response) { resolve(response); })
.once('error', function(response) { reject(response); })
.execute();
});
}
function makeCanvas(id) {
var container = document.getElementById(id);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
container.innerHTML = '';
canvas.width = container.offsetWidth;
canvas.height = container.offsetHeight;
container.appendChild(canvas);
return ctx;
}
function generateLegend(id, items) {
var legend = document.getElementById(id);
legend.innerHTML = items.map(function(item) {
var color = item.color || item.fillColor;
var label = item.label;
return '<li><i style="background:' + color + '"></i>' +
escapeHtml(label) + '</li>';
}).join('');
}
// Set some global Chart.js defaults.
Chart.defaults.global.animationSteps = 60;
Chart.defaults.global.animationEasing = 'easeInOutQuart';
Chart.defaults.global.responsive = true;
Chart.defaults.global.maintainAspectRatio = false;
function escapeHtml(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
renderIndividualChart(xxx9412, '[name]-container', 'legend-1-container', 'active-users-container1');
renderIndividualChart(xxx11510, '[name]-container', 'legend-2-container', 'active-users-container2');
renderIndividualChart(xxx8011, '[name]-container', 'legend-3-container', 'active-users-container3');
renderIndividualChart(xxx4711, '[name]-container', 'legend-4-container', 'active-users-container4');
renderIndividualChart(xxx4009, '[name]-container', 'legend-5-container', 'active-users-container5');
renderIndividualChart(xxx61037, '[name]-container', 'legend-6-container', 'active-users-container6');
renderIndividualChart(xxx0969, '[name]-container', 'legend-7-container', 'active-users-container7');
renderIndividualChart(xxx5362, '[name]-container', 'legend-8-container', 'active-users-container8');
});
</script>
`
答案 0 :(得分:0)
我猜您超出每个IP地址限制的10QPS。由于您一次要发送大约16(8 * 2)个呼叫,因此请参见General Quota limits。
请注意,每天的呼叫次数(50,000)和每个Analytics视图的呼叫次数(10,000)也受到限制,每个视图的并发请求数(10)也受到限制。
我认为最简单的方法是将通话一个接一个地链接,而不是一次触发所有通话。根据RPC的速度,您可能仍会遇到qps限制。更好的方法是在GA数据调用上添加速率限制,并在配额错误时使用exponential backoff添加一些重试逻辑。