1

我剛開始使用此API,而且我正在努力使用Google Analytics Reporting API v4檢索我的數據。我只是試圖檢索網站的分析,所以我可以建立一個儀表板。在這裏,我下面的例子:Hello Analytics Reporting API v4; JavaScript quick start for web applicationsGoogle Analytics Reporting API v4缺少必需的身份驗證憑證

我可以提出一個要求,但是,我不斷收到一個400 error即可能是這樣的:400: Invalid Credentials

`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報告APIv4從7天內檢索所有數據?我感覺好像我在使用所有必填字段,但是,我不知道我錯過了什麼。你能爲我指出正確的方向嗎?

回答

1

我回答我自己的問題,以防其他人需要使用谷歌分析報告API與反應的幫助。這是我需要做的,以建立一個gapi按鈕,然後提出一個基本的要求。我創建的錯誤來自錯字。我使用的是metrics:[{express: 'ga:sessions'}]而不是metrics:[{expression: '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> 
    ) 
    } 
} 
1

401:無效的證書無效的授權標頭。 您使用的令牌訪問 已過期或無效。

這基本上意味着您需要重新登錄。

單擊登錄按鈕並授權訪問Google Analytics。

請記住訪問令牌只在一小時內有效,您必須在有效期屆滿時再次簽名。

+0

@達爾姆感謝您的回覆。我編輯了一下我的代碼來創建一個GoogleSignal2按鈕。在登錄並調用我的'onsuccess'回調後,我現在收到了一個不同的錯誤。我已經更新了我的問題,並希望能夠再次查看並確定導致新錯誤的原因。 –

+0

@達姆感謝您的回覆。我編輯了一下我的代碼來創建一個GoogleSignal2按鈕。我意識到我犯的錯誤。當我提出請求時,我有一個未定義的屬性,稱爲「express」,當它應該是'metrics'屬性中的「表達式」時。感謝您的幫助! –