无法显示Analytics Using API文档

时间:2015-05-04 07:17:22

标签: api analytics

我跟着这个: https://ga-dev-tools.appspot.com/embed-api/basic-dashboard/

创造了这个:

<!DOCTYPE html>
<html>
<head>
<title>Analytics Test</title>
</head>
<body>

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

<div id="embed-api-auth-container"></div>
<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: 'UA-1234567-00',
  });


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

但访问该页面时看不到任何内容。

我检查了API并且API处于活动状态,但域名不在引荐列表中,所以我添加了它,但仍然没有。

有什么想法......?

1 个答案:

答案 0 :(得分:0)

你的

clientID: 'UA-XXXXXXX-XX'

不是正确的客户ID。这应该看起来像:

clientID: 'XXXXXX.apps.googleusercontent.com'

相反,请尝试在https://console.developers.google.com/创建项目。在凭据选项卡下,您可以创建新的客户端ID。

如果更容易,Google会提供一些分步说明,可以看到here