我是使用这个 API 的新手,我正在努力使用Google Analytics Reporting API v4检索我的数据。我只是尝试检索网站的分析,以便我可以构建仪表板。我正在关注这里的示例:Hello Analytics Reporting API v4;Web 应用程序的 JavaScript 快速入门
我能够提出请求,但是,我不断收到400 error
可能是这样的:400:无效的凭据
`Invalid JSON payload received. Unknown name "express" at 'report_requests[0].metrics[0]': Cannot find field.
handleError @ Test.js:64
h.o0 @ cb=gapi.loaded_0:53
xs @ cb=gapi.loaded_0:56
Wq @ cb=gapi.loaded_0:56
_.C.uea @ cb=gapi.loaded_0:55
Ap @ cb=gapi.loaded_0:49`
我不知道我做错了什么,我希望有人能指出我正确的方向。
这是一个反应应用程序。我在组件安装后提出请求。
import React, { Component } from 'react';
import GoogleLogin from 'react-google-login';
import $ from 'jquery';
const VIEW_ID = '17414592';
const CLIENT_ID = "936270024581-stgn130l17v21s6vjch9p751hiqbovac.apps.googleusercontent.com";
const DISC_DOCS = 'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4';
export default class Test extends Component {
constructor(props) {
super(props)
this.printResults = this.printResults.bind(this);
this.handleRequest = this.handleRequest.bind(this);
this.handleError = this.handleError.bind(this);
}
//when component mounts, create a google sign in button.
componentDidMount() {
//load gapi
$.getScript("https://apis.google.com/js/client:platform.js")
.done(() => {
window.gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.handleRequest,
'onfailure': this.handleError
});
})
}
//on success, make a request to get google analytics data
handleRequest() {
window.gapi.client.request({
path: '/v4/reports:batchGet',
root: 'https://analyticsreporting.googleapis.com',
method: 'POST',
body: {
reportRequests: [
{
viewId: VIEW_ID,
dateRanges: [{
startDate: '7daysAgo',
endDate: 'today'
}],
metrics: [{ express: 'ga:sessions' }]
}
]
}
}).then(this.printResults, this.handleError)
}
//log the data
printResults(response) {
console.log(response)
}
//or the error if there is one
handleError(reason) {
console.error(reason)
console.error(reason.result.error.message);
}
//render it all
render() {
return (
<div>
<div id="my-signin2"></div>
</div>
)
}
}
我究竟做错了什么?如何使用 Google Analytics Reporting APIv4 正确执行 batchRequest 以检索 7 天内的所有数据?我感觉好像我正在使用所有必填字段,但是,我不知道我错过了什么。你能为我指出正确的方向吗?
我正在回答我自己的问题,以防其他人需要帮助使用带有反应的 google 分析报告 api v4。这是我需要做的,以便构建一个 gapi 按钮,然后发出基本请求。我创建的错误来自一个错字。而不是metrics:[{expression: 'ga:sessions'}]
我使用metrics:[{express: 'ga:sessions'}]
.
这是一个将创建一个简单请求的组件。请注意,您必须将 更改VIEW_ID
为您的值。
import React, { Component } from 'react';
import $ from 'jquery';
const VIEW_ID = '17414592';
export default class Test extends Component {
constructor(props) {
super(props)
this.printResults = this.printResults.bind(this);
this.handleRequest = this.handleRequest.bind(this);
this.handleError = this.handleError.bind(this);
}
//when component mounts, create a google sign in button.
componentDidMount() {
//load gapi
$.getScript("https://apis.google.com/js/client:platform.js")
.done(() => {
window.gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.handleRequest,
'onfailure': this.handleError
});
})
}
//on success, make a request to get google analytics data
handleRequest() {
window.gapi.client.request({
path: '/v4/reports:batchGet',
root: 'https://analyticsreporting.googleapis.com',
method: 'POST',
body: {
reportRequests: [
{
viewId: VIEW_ID,
dateRanges: [{
startDate: '7daysAgo',
endDate: 'today'
}],
metrics: [{ expression: 'ga:sessions' }]
}
]
}
}).then(this.printResults, this.handleError)
}
//log the data
printResults(response) {
console.log(response)
}
//or the error if there is one
handleError(reason) {
console.error(reason)
console.error(reason.result.error.message);
}
//render it all
render() {
return (
<div>
<div id="my-signin2"></div>
</div>
)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句