报表数据未显示在嵌入式API中

时间:2018-10-25 15:34:22

标签: google-analytics-api

我设置了OAuth 2.0客户端ID,复制并粘贴了代码以嵌入https://ga-dev-tools.appspot.com/embed-api/basic-dashboard/中的Google Analytics(分析)API的基本信息中心

但是,在我的本地主机服务器中,我看到“访问Google Analytics(分析)”按钮,单击并获得授权后,除了“您以xxxxx@gmail.com登录”之外,什么都没有显示。 如Google Analytics(分析)Embed API Overview文档中所见,是否不应该在文本下方显示图表和/或报告数据?

google's default screenshot of embedded api

这是我的实际内容:

missing charts below the text

这是完整的代码:

<!DOCTYPE html>
<html>
<head>
  <title>Embed API Demo</title>
</head>
<body>

<!-- Step 1: Create the containing elements. -->

<div id="embed-api-auth-container"></div>
<div id="chart-container"></div>
<div id="view-selector-container"></div>

<!-- Step 2: Load the library. -->

<script>
(function(w,d,s,g,js,fs){
  g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
  js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
  js.src='https://apis.google.com/js/platform.js';
  fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
}(window,document,'script'));
</script>


<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: 'my client code'
  });


  /**
   * 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>

</body>
</html>

我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

身份验证成功后,您需要致电viewSelector.execute()。就像现在一样,它只能通过以下方式第一次调用:

替换

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

使用:

// Render the view selector once user is authenticated.
gapi.analytics.auth.on('success', function(response) {
  viewSelector.execute();
});