Google Analytics嵌入式API演示无效

时间:2014-08-23 20:28:05

标签: google-analytics

我按照以下网址中的步骤进行操作:https://developers.google.com/analytics/devguides/reporting/embed/v1/devguide#client-id

当我执行下面的代码时,我在控制台中收到错误消息 我保持了#34; localhost"在我创建客户端ID时,在我的Javascript域中。 你能建议吗?谢谢

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

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

<section id="auth-button"></section>
<section id="view-selector"></section>
<section id="timeline"></section>

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

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

<script>
gapi.analytics.ready(function() {

  // Step 3: Authorize the user.

  var CLIENT_ID = 'XXXXXXXX';


  gapi.analytics.auth.authorize({
    container: 'auth-button',
    clientid: CLIENT_ID,
  });

  // Step 4: Create the view selector.

  var viewSelector = new gapi.analytics.ViewSelector({
    container: 'view-selector'
  });

  // Step 5: Create the timeline chart.

  var timeline = new gapi.analytics.googleCharts.DataChart({
    reportType: 'ga',
    query: {
      'dimensions': 'ga:date',
      'metrics': 'ga:sessions',
      'start-date': '30daysAgo',
      'end-date': 'yesterday',
    },
    chart: {
      type: 'LINE',
      container: 'timeline'
    }
  });

  // Step 6: Hook up the components to work together.

  gapi.analytics.auth.on('success', function(response) {
    viewSelector.execute();
  });

  viewSelector.on('change', function(ids) {
    var newIds = {
      query: {
        ids: ids
      }
    }
    timeline.set(newIds).execute();
  });
});
</script>
</body>
</html>

Object {error: Object}
error: Object
errors: Array[1]
0: Object
message: "immediate_failed"
reason: "invalidParameter"
__proto__: Object
length: 1
__proto__: Array[0]
message: "immediate_failed"
__proto__: Object
__proto__: Object
 cb=gapi.loaded_0:433
_.nH cb=gapi.loaded_0:433
_.du.Vh cb=gapi.loaded_0:459
YP.Ka cb=gapi.loaded_0:466
_.k.iu cb=gapi.loaded_0:291
ix cb=gapi.loaded_0:431
(anonymous function) cb=gapi.loaded_0:433
h.BE cb=gapi.loaded_0:137
Wq cb=gapi.loaded_0:140
_.C.ye cb=gapi.loaded_0:140
Ap

4 个答案:

答案 0 :(得分:1)

您的服务器或客户端ID来源必定存在问题。

将该代码完全复制并粘贴到jsbin.com(仅更改客户端ID)我能够正常工作。我所要做的就是将http://run.jsbin.com添加到我提供的客户ID的已批准来源列表中。

这是一个有效的例子:
http://jsbin.com/batexelohuve/1/edit

如果您将http://run.jsbin.com添加到客户端ID的原始列表中并且它在jsbin中运行,则表示您的设置有问题,而不是您提供的代码。

答案 1 :(得分:1)

生成客户ID后,将其复制并粘贴到您的代码中,并将代码保存为.html而不是.php。对于localhost,通常只需将其写为http://localhost即可。除非您指定了某个端口,否则您可能想要更改它。否则通常会这样做。要知道您的计算机正在侦听哪个端口,请访问您的httpd.conf文件并找到listen命令。

要添加,请在写入ID时忘记放置``符号。

希望有助于解决您的问题!

答案 2 :(得分:0)

我认为您需要在代码中导入JavaScript API。试试这个:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

答案 3 :(得分:0)

您必须正确指定Origin uri和Authorized uri,例如,如果您在http://localhost:8080中运行演示,那么您在Origin URI和Authorized URI中都添加了此url,那么您将在浏览器中获得一个访问按钮。

相关问题