将GA嵌入API与Google Charts的高级功能结合使用

时间:2016-12-28 14:44:38

标签: javascript google-analytics

我使用embed api将GA中的一些数据显示到图表中,但api仅支持基本内容(LINE,COLUMN,BAR,TABLE和GEO。)。

有什么方法可以将嵌入式api用于Google Charts库吗?

比如使用饼图而不是柱状图?

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ChartDemo</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: 'supersecretclientid.apps.googleusercontent.com'
  });


  /**
   * 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: 'COLUMN',
      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>

3 个答案:

答案 0 :(得分:0)

您不仅可以使用Google Charts显示结果,还可以选择您喜欢的第三方可视化库。

此处提供更多信息和演示:https://ga-dev-tools.appspot.com/embed-api/third-party-visualizations/

答案 1 :(得分:0)

找到解决方案,必须下载并运行一个可以在github页面上找到的sdk。否则,将不会定义gapi.analytics.ext语句...

答案 2 :(得分:0)

晚会: 你也可以显示PIE图表,

/**
   * 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: 'PIE',
      options: {
        width: '100%',
        'pieHole': 4 / 9, // **pay attention here**
        'colors': ['#14dd9f', '#ffffff', '#61d3ff','#ffff7b','#ff5e64','#ff991c']
      }
    }
  });
dataChart.execute();