簡體   English   中英

使用Google Analytics(分析)Core Reporting API查詢中的數據繪制圖表

[英]Draw a Chart with Data from Google Analytics Core Reporting API Query

我對Google Analytics(分析)Core Reporting API以及在圖表中顯示結果數據有疑問。 我設法對Analytics(分析)進行了查詢,效果很好。 我可以在Web表格中選擇開始和結束日期,屬性以及一些維度和指標。 然后作為回報,我想以dataTable格式獲取所有數據,因為Query-Option'output':'datatable'

現在,我想在Google圖表中顯示數據(數據表)。 有問題。 屏幕上畫了一些東西,但是沒有數據被顯示。 所以我認為繪制的圖只是空的。 從邏輯上講,我試圖創建一個新的dataTable並用來自Query-Response的Data填充它,然后繪制它。 某個地方肯定有問題,也許是格式? 我已經嘗試了很多方法,但是沒有一種方法可以得出正確的圖表。

順便說一句:如果我嘗試使用給定的數據繪制圖表,例如Google圖表文檔( https://developers.google.com/chart/interactive/docs/quick_start )的示例,則繪制該圖表沒有任何問題。 所以我猜它是一個格式問題。

這是代碼(略微縮短):

 function queryCoreReportingApi(profileId) {    
      gapi.client.analytics.data.ga.get({
      'ids': 'ga:' + profileId,
      'start-date': startDate,                  //Startdate Datepicker
      'end-date': endDate,                      //Enddate Datepicker
      'metrics': 'ga:sessions',
      'dimensions': dimension,
      'filters': 'ga:landingPagePath=@'+service+';'+'ga:deviceCategory=='+device,     
      'output': 'datatable'
    })
    .then(function(response) {
      var formattedJson = JSON.stringify(response.result, null, 2);

      //Output Data in Text-Area
      document.getElementById('query-output').value = formattedJson;
      document.getElementById('query-output-obj').value = response.result.dataTable;

      //Load Chart Lib
      google.load('visualization', '1.0', {'packages':['corechart']});

      //Create Data-Table and fill up
      var data = new google.visualization.DataTable();
      data.addColumn('string', 'Browser');
      data.addColumn('number', 'Sessions');

      response.result.dataTable.rows.forEach(function pushNumericColumn(row) {
        data.addRow([row[0], parseInt(row[1])]);
      });

      var options = {
      title: 'Custom Chart',
      };

      //Draw Chart
      var chart = new google.visualization.PieChart(document.getElementById('chart_div2'));
      chart.draw(data, options);
    }

formattedJson的輸出:查詢中的所有數據|

response.result.dataTable的輸出:[對象對象] |

錯誤:控制台中沒有錯誤

身份驗證過程(代碼示例中未顯示)也可以正常工作。

我可以通過訪問行對象的“ v”屬性來繪制圖表,如下所示:

data.addRow([row.c[0].v, parseInt(row.c[1].v)]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM