2016-10-04 91 views
0

我是Google Analytics API的「新手」,我試圖讓「Hello Analytics」示例正常工作。Hello Analytics示例生成403錯誤

我跟着their steps 來創建測試網頁,但我沒有結果。

當我進入我的網頁時,我看到標題,「登錄」按鈕和下面顯示結果的文本區域。當我按「登錄」時,這似乎工作;該按鈕變爲「已登錄」。但是,沒有結果顯示。

我使用的是FireFox,所以我右鍵單擊文本區域並選擇「檢查元素」;這表明此錯誤消息:

對象{結果:對象,身體: 「{ 」錯誤「:{ 」代碼「:403,...」, 頭:對象,狀態:403,狀態文本: 「紫禁城」 }

我認爲這是一個權限錯誤,但不知道如何糾正它。

這裏是我的文件HelloAnalytics.html:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Hello Analytics Reporting API V4</title> 
    <meta name="google-signin-client_id" content="959582115031-ardmn5vsir7kbcp0dme4d4n1p45bd649.apps.googleusercontent.com"> 
    <meta name="google-signin-scope" content="https://www.googleapis.com/auth/analytics.readonly"> 
</head> 
<body> 

<h1>Hello Analytics Reporting API V4</h1> 

<p> 
    <!-- The Sign-in button. This will run `queryReports()` on success. --> 
    <div class="g-signin2" data-onsuccess="queryReports"></div> 
</p> 

<!-- The API response will be printed here. --> 
<textarea cols="80" rows="20" id="query-output"></textarea> 

<script> 
    // Replace with your view ID. 
    var VIEW_ID = '92320289'; 

    // Query the API and print the results to the page. 
    function queryReports() { 
    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(displayResults, console.error.bind(console)); 
    } 

    function displayResults(response) { 
    var formattedJson = JSON.stringify(response.result, null, 2); 
    document.getElementById('query-output').value = formattedJson; 
    } 
</script> 

<!-- Load the JavaScript API client and Sign-in library. --> 
<script src="https://apis.google.com/js/client:platform.js"></script> 

</body> 
</html> 

我需要了解如何使用谷歌Analytics(分析)API做簡單的查詢。

+0

你是在本地運行還是在服務器上運行? – sideroxylon

+0

如何在谷歌分析API中進行分頁? –

回答

1

最後得到它的工作!

我試圖使用API​​來獲取Google樣本網站(「Google Merchandise Store」)的數據。我試圖考慮許可錯誤的可能原因,並且我想到Google可能不允許該網站的Google Analytics API調用。

因此,我通過設置自己的測試網站,並使用Google的Javascript跟蹤代碼,然後定義了一個新項目,爲其啓用API,創建憑據(客戶端ID)以及獲取視圖ID爲我的網站。通過我的HelloAnalytics.html頁面中的新客戶端ID和視圖ID,它可以很好地工作。

所以我的猜測是正確的:Google不允許API調用獲取其「Google Merchandise Store」示例網站的數據。他們真的應該在their "quickstart" page上提到這個,因爲一個「新手」(像我)不會知道這一點!

我以爲我應該發佈我的解決方案,因爲我確信有人會在未來遇到這個問題。

僅供參考 - 我有HelloAnalytics.html運行在Web服務器上。我正在使用我的ISP提供的免費網絡空間。

+0

是的,您無法對Google Merchandise Store進行API調用,該調用位於本幫助文章的底部https://support.google.com/analytics/answer/6367342?hl=zh-CN – MarkeD

+0

哦,是的,他們的確在說那裏;從未看過那​​個頁面。謝謝 –

+0

在快速入門指南中,他們也在最下方註明「注意:要成功運行此示例,您至少需要擁有一個Google Analytics媒體資源和視圖。」人們可以閱讀的是,你必須擁有屬於你的財產和觀點。 – Matt