谷歌分析图表的JavaScript
问题描述:
早安。我是JavaScript的新手,我想我想做的事很容易,但我不知道如何。谷歌分析图表的JavaScript
我想提请谷歌分析图表,以显示自己的仪表板。我的代码工作,但我有几个网站注册,并始终显示我相同的默认值,除非我手动更改(该字段被称为属性)。我想默认的网站是另一个。
这里是我的代码(我已经改变了客户端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>
任何人可以帮助我呢?在此先感谢
答
这么多,如果你知道该视图的ids
参数你想用默认显示那么你就需要使用视图选择组件,你可以硬编码值在查询
例如,使用“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'
替换为要显示的视图的参数ids
即可。
如果您不知道ids
参数,您可以使用Google Analytics Account Explorer工具找到它。
答
我喜欢Query explorer它会给你的帐户ID以及其他参数。你可以测试看看它是否会返回你所期望的。
+0
非常感谢你的帮助罗斯洛克。它是一个伟大的工具。现在,我可以找到我想要使用的指标。 – Josto90 2015-04-06 10:32:44
非常感谢你的帮助菲利普。它现在有效! – Josto90 2015-04-06 10:31:20
太棒了!如果它对你有用,那么你应该将答案标记为已接受,所以其他具有相同问题的人可以受益。 – 2015-04-06 14:58:46