2017-04-13 57 views
0

我想「嵌入」谷歌分析報告到我的網頁,到目前爲止,我正在關注這個ga-dev-tools.appspot.com教程/文件。這是我的結果現在... enter image description here如何允許選擇只有一個屬性與谷歌分析API

的問題是,我不wan't顯示所有屬性(圖像三振選項),我wan't顯示當前頁面的唯一屬性,所以我需要找出如何允許只顯示一個屬性。

所以這是我到目前爲止的代碼

<h2>Sessions</h2> 
<p>Last 30 days</p> 
<div id="chart-container"></div> 
<div id="view-selector-container"></div> 

<script> 
    gapi.analytics.ready(function() { 
     /** 
     * Authorize the user immediately if the user has already granted access. 
     * If no access has been created, render an authorize button inside the 
     * element with the ID "embed-api-auth-container". 
     */ 
     gapi.analytics.auth.authorize({ 
      container: 'embed-api-auth-container', 
      clientid: '<MyClientId>' 
     }); 

     /** 
     * Create a new ViewSelector instance to be rendered inside of an 
     * element with the id "view-selector-container". 
     */ 
     var viewSelector = new gapi.analytics.ViewSelector({ 
      container: 'view-selector-container', 
     }); 

     // Render the view selector to the page. 
     viewSelector.execute(); 

     /** 
     * Create a new DataChart instance with the given query parameters 
     * and Google chart options. It will be rendered inside an element 
     * with the id "chart-container". 
     */ 
     var dataChart = new gapi.analytics.googleCharts.DataChart({ 
      query: { 
       metrics: 'ga:sessions', 
       dimensions: 'ga:date', 
       'start-date': '30daysAgo', 
       'end-date': 'yesterday' 
      }, 
      chart: { 
       container: 'chart-container', 
       type: 'LINE', 
       options: { 
        width: '100%' 
       } 
      } 
     }); 

     /** 
     * Render the dataChart on the page whenever a new view is selected. 
     */ 
     viewSelector.on('change', function(ids) { 
      dataChart.set({query: {ids: ids}}).execute(); 
     }); 
    }); 
</script> 

所以傢伙有可能增加一些PARAM處,顯示/獲取數據,只有一個屬性? 如果您需要任何其他信息,請讓我知道,我會提供。謝謝

回答

1

取決於你的用例。如果您的意思是爲了方便起見,以便用戶沒有通過列表查找相關視圖,則可以手動構建一個只包含相關ID硬編碼的選擇字段組合(然後將它們設置爲dataChart set方法中的參數在執行查詢之前就像使用viewSelector的輸出一樣)。

如果你的意思是某種安全機制,那麼不需要(因爲無論如何用戶都可以訪問它們的屬性,再加上這個是你可以在瀏覽器中操縱調用的Javascript)。

+0

超級! Thx的小費! –