javascript - JavaScript 中的 Google Analytics 图表

标签 javascript charts google-analytics

早上好。我是 javascript 的新手,我认为我想做的事情很简单,但我不知道如何做。

我正在尝试绘制 Google 分析图表以显示在我自己的仪表板上。我的代码有效,但我注册了多个网站,并且总是显示相同的默认值,除非我手动更改它(该字段称为“属性”)。我希望默认网站是另一个。

这是我的代码(我已将客户端 ID 更改为“XXX”

<html>
<head>
<title> Google Analytics Charts </title>
</head>
<body>
<p align="center"><b><u>VISITS<u> </b></p>
<!-- Step 1: Create the containing elements. -->

<section id="auth-button" hidden></section>
<section id="view-selector"></section>
<section id="timeline" class="left clear"></section>
<section id="pie" class="right"></section>
<section id="table" class="left clear"></section>
<section id="gauge" class="right"></section>

<!-- Step 1.1: CSS. -->
<style>
.left {float:left}
.right {float:right}
.clear {clear:both}
</style>

<!-- 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 = 'XXX';

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.

//Chart 1
var timeline = new gapi.analytics.googleCharts.DataChart({
reportType: 'ga',
query: {
'metrics': 'ga:users',
'dimensions': 'ga:date',
//      'start-date': '30daysAgo',
//      'start-date': '2015-03-01',
'start-date': '7daysAgo',
'end-date': 'today',
},
chart: {
  type: 'LINE',
  container: 'timeline',
  options: {
    //width: '50%', height: '50%',
    title: 'Visits per day',
    curveType: 'function'
  }
}
});

//Chart 2
var pie = new gapi.analytics.googleCharts.DataChart({
query: {
  metrics: 'ga:sessions',
  dimensions: 'ga:country',
  'start-date': '30daysAgo',
  'end-date': 'yesterday',
  'max-results': 5,
  sort: '-ga:sessions'
},
chart: {
  container: 'pie',
  type: 'PIE',
  options: {
    // width: '50%', height: '50%',
    title: 'Visits per country',
    is3D: true
  }
}
});

//Chart 3
var table= new gapi.analytics.googleCharts.DataChart({
reportType: 'ga',
query: {
//      'metrics': 'ga:users',
  'metrics': 'ga:sessions',
//      'dimensions': 'ga:hour',
  'dimensions': 'ga:date',
  'dimensions': 'ga:campaign',
//      'start-date': '30daysAgo',
//      'start-date': '2015-03-01',
  'start-date': 'today',
  'end-date': 'today',
},
chart: {
  type: 'TABLE',
  container: 'table',
  options: {
    //width: '50%', height: '50%',
    title: 'Campaign visits today',
    is3D: true
  }
}
});

//Chart 4
var gauge= new gapi.analytics.googleCharts.DataChart({
query: {
  metrics: 'ga:socialInteractions',
//     metrics: 'ga:avgTimeOnPage',
  dimensions: 'ga:socialInteractionNetwork',
  'start-date': 'today',
  'end-date': 'today',
},
chart: {
  container: 'gauge',
  type: 'TABLE',
  options: {
    title: 'Exit Pages'
  }
}
});


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

有人可以帮我吗?提前非常感谢

最佳答案

如果您知道默认情况下要显示的 View 的 ids 参数,则无需使用 View 选择器组件,只需在查询中硬编码该值即可。

例如,使用您的“Chart1”代码:

// Chart 1
var timeline = new gapi.analytics.googleCharts.DataChart({
  query: {
    'ids': 'ga:XXXXXXX',
    'metrics': 'ga:users',
    'dimensions': 'ga:date',
    'start-date': '7daysAgo',
    'end-date': 'today',
  },
  chart: {
    type: 'LINE',
    container: 'timeline',
    options: {
      title: 'Visits per day',
      curveType: 'function'
    }
  }
});

您只需将 'ga:XXXXXXX' 替换为您要显示的 View 的 ids 参数即可。

如果您不知道 ids 参数,可以使用 Google Analytics Account Explorer 找到它工具。

关于javascript - JavaScript 中的 Google Analytics 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29393377/

相关文章:

excel - 在Excel中制作 "stacked"漏斗图?

java - Androidplot 线不显示

php - 没有 JavaScript 或 Cookie 的 Google Analytics

google-analytics - 测量协议(protocol)命中显示在实时 View 中,但不显示在报告中

javascript - Kendo Grid 数字 chop 到小数点后两位。如何让它尊重用户输入的内容?

javascript - jquery animatescrollTop 函数在 Internet Explorer 中不起作用

python - 使用 matplotlib 的极坐标图 x 轴标签位置

php - Analytics Reporting API V4 api 问题

Javascript/小型 Jquery 扩展方法可以自动更改 Z 索引以将元素放在所有内容之上?

javascript - 如何使用 PHP/HTML 限制选中复选框的数量并将值传递到不同的页面