gpt4 book ai didi

javascript - 在 Google API 请求中使用 "filters"参数总是导致错误?

转载 作者:行者123 更新时间:2023-11-30 16:43:03 26 4
gpt4 key购买 nike

我正在尝试查询 Google Analaytics Core Reporting API。但是我遇到了一个我无法弄清楚的错误。我处于早期阶段,所以我从 quickstart guide 复制了一个示例 JavaScript 实现. 跑这个,它的工作原理,可爱。

所以我开始调整参数,一切似乎都很好,直到我输入过滤器,所以

成功了

// Query the Core Reporting API for the number sessions for
// the past seven days.
gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profileId,
'start-date': '7daysAgo',
'end-date': 'today',
'dimensions':'ga:browser',
'metrics': 'ga:pageviews'
})

不起作用

gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profileId,
'start-date': '7daysAgo',
'end-date': 'today',
'filters':'ga:city%3D%3DIrvine',
'dimensions':'ga:browser',
'metrics': 'ga:pageviews'
})

错误结果:

{
"error": {
"errors": [{
"domain": "global",
"reason": "invalidParameter",
"message": "Invalidvalue'ga: city%3D%3DIrvine'forfiltersparameter."
}],
"code": 400,
"message": "Invalidvalue'ga: city%3D%3DIrvine'forfiltersparameter."
}
}

我重读了一篇 the docs而且我看不出我的过滤器有问题。

那么我在这里缺少什么?为什么像我这样进入过滤器会失败?


完整代码

我认为如果没有 ClientID 等,您将无法运行此程序,但这里(为了完整性)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Analytics - A quickstart guide for JavaScript</title>
</head>
<body>

<button id="auth-button" hidden>Authorize</button>

<h1>Hello Analytics</h1>

<textarea cols="80" rows="20" id="query-output"></textarea>

<script>

// Replace with your client ID from the developer console.
var CLIENT_ID = 'MyClientID';

// Set authorized scope.
var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'];


function authorize(event) {
// Handles the authorization flow.
// `immediate` should be false when invoked from the button click.
var useImmdiate = event ? false : true;
var authData = {
client_id: CLIENT_ID,
scope: SCOPES,
immediate: useImmdiate
};

gapi.auth.authorize(authData, function(response) {
var authButton = document.getElementById('auth-button');
if (response.error) {
authButton.hidden = false;
}
else {
authButton.hidden = true;
queryAccounts();
}
});
}


function queryAccounts() {
// Load the Google Analytics client library.
gapi.client.load('analytics', 'v3').then(function() {

// Get a list of all Google Analytics accounts for this user
gapi.client.analytics.management.accounts.list().then(handleAccounts);
});
}


function handleAccounts(response) {
// Handles the response from the accounts list method.
if (response.result.items && response.result.items.length) {
// Get the first Google Analytics account.
var firstAccountId = response.result.items[0].id;

// Query for properties.
queryProperties(firstAccountId);
} else {
console.log('No accounts found for this user.');
}
}


function queryProperties(accountId) {
// Get a list of all the properties for the account.
gapi.client.analytics.management.webproperties.list(
{'accountId': accountId})
.then(handleProperties)
.then(null, function(err) {
// Log any errors.
console.log(err);
});
}


function handleProperties(response) {
// Handles the response from the webproperties list method.
if (response.result.items && response.result.items.length) {

// Get the first Google Analytics account
var firstAccountId = response.result.items[0].accountId;

// Get the first property ID
var firstPropertyId = response.result.items[0].id;

// Query for Views (Profiles).
queryProfiles(firstAccountId, firstPropertyId);
} else {
console.log('No properties found for this user.');
}
}


function queryProfiles(accountId, propertyId) {
// Get a list of all Views (Profiles) for the first property
// of the first Account.
gapi.client.analytics.management.profiles.list({
'accountId': accountId,
'webPropertyId': propertyId
})
.then(handleProfiles)
.then(null, function(err) {
// Log any errors.
console.log(err);
});
}


function handleProfiles(response) {
// Handles the response from the profiles list method.
if (response.result.items && response.result.items.length) {
// Get the first View (Profile) ID.
var firstProfileId = response.result.items[0].id;

// Query the Core Reporting API.
queryCoreReportingApi(firstProfileId);
} else {
console.log('No views (profiles) found for this user.');
}
}


function queryCoreReportingApi(profileId) {
// Query the Core Reporting API for the number sessions for
// the past seven days.
gapi.client.analytics.data.ga.get({
'ids': 'ga:' + profileId,
'start-date': '7daysAgo',
'end-date': 'today',
'filters':'ga:city%3D%3DIrvine',
'dimensions':'ga:browser',
'metrics': 'ga:pageviews'
})
.then(function(response) {
var formattedJson = JSON.stringify(response.result, null, 2);
document.getElementById('query-output').value = formattedJson;
})
.then(null, function(err) {
// Log any errors.
console.log(err);
});
}

// Add an event listener to the 'auth-button'.
document.getElementById('auth-button').addEventListener('click', authorize);
</script>

<script src="https://apis.google.com/js/client.js?onload=authorize"></script>

</body>
</html>

最佳答案

doc 中所述,过滤器编码示例是关于最终的 HTTP 请求

https://www.googleapis.com/analytics/v3/data/ga
?ids=ga:12134
&dimensions=ga:browser
&metrics=ga:pageviews
&filters=ga:browser%3D~%5EFirefox
&start-date=2007-01-01
&end-date=2007-12-31

但是 Api 已经在进行编码了。

因此替换 %3D%3D==在你的过滤器中 ga:city%3D%3DIrvine会完成这项工作。

关于javascript - 在 Google API 请求中使用 "filters"参数总是导致错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31652230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com