使用API​​嵌入Google Analytics(分析)地图视图

时间:2018-10-22 20:11:41

标签: api google-analytics embed google-analytics-api polymer-elements

我正在尝试在美国地图上显示“站点上的活动用户”计数及其位置。我们已经安装了Google Analytics(分析),并且仪表板正在获取实时数据。我正试图将其插入到我们服务器上托管的网页上,以显示在公司大楼内的各个屏幕上。

我已阅读this documentation,但在放置此代码的网页上看不到任何显示。它只是一个白屏,控制台中没有错误。

看这部分Polymer Elements,看起来是可能的,我已经插入HTML元素了。但是,什么都没有。

这是我下面的代码,出于隐私目的,删除了CLIENT ID。

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

    <google-analytics-chart
      type="area"
      metrics="ga:sessions"
      dimensions="ga:date"
      startDate="30daysAgo"
      endDate="yesterday">
    </google-analytics-chart>

<!-- 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 = 'REMOVED-FOR-PRIVACY';

  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 chart = new gapi.analytics.googleCharts.DataChart({
    query: {
      'dimensions': 'ga:country',
      'metrics': 'ga:sessions',
      'start-date': '30daysAgo',
      'end-date': 'yesterday',
    },
    chart: {
      type: 'GEO',
      container: 'timeline',
      options: {
        region: '1', // Western Europe
        displayMode: 'markers'
      }
    }
  });

  // 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
      }
    }
    chart.set(newIds).execute();
  });
});
</script>
</body>
</html>

最终,我想嵌入并显示以下两个元素:

Google Analytics Dashboard

1 个答案:

答案 0 :(得分:1)

我认为这些聚合元素用于查看访问者可以访问的GA帐户上的数据,而不是让您向有关您网站的用户显示结果。

我建议您使用Google Data Studio并创建报告。然后将报告嵌入到您的网页中。 https://support.google.com/datastudio/answer/7450249?hl=en

还请注意,这将不是实时数据,您可以使用real time api,但我不知道有任何现成的解决方案可以显示数据。

相关问题