在localhost html中显示我的网站的Google Analytics分析报告

时间:2015-05-28 07:54:35

标签: html google-analytics localhost

是否可以在localhost(wamp)中的html页面中显示google analytics报告。 我为一个网站创建了一个谷歌分析,并获得了像UI-XXXXXX-XX这样的跟踪ID。使用谷歌提供的脚本创建了一个html页面。但页面显示为空白页面。

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<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>
</head>
<body>


<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-xxxxxxx-1',
  });


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

简短的回答是,是的,这是可能的。

您必须修改一些内容,例如

  • clientid: 'UA-xxxxxxx-1',不是个人资料ID,而是您在google developer console
  • 上创建帐户后获得的客户ID
  • 在google开发者控制台中,您必须为oauth将一些回调网址列入白名单。我不认为http://localhost.localdomain/ ...可以添加,但是,您可以修改您的/ etc / hosts文件并使用您的localhost修改您想要的任何域

虽然答案对你来说太晚了,但我希望其他人能找到这个有用的