我正在使用Google Analytics Embed API。以下是我在Google的“开发”页面中使用的代码示例。有没有办法设置选择器的默认值?帐户|财产|图
<!doctype html>
<html lang="en">
<head>
<title>Google Charts</title>
<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>
<script>
gapi.analytics.ready(function() {
var ACCESS_TOKEN = 'xxxxx'; // obtained from your service account
gapi.analytics.auth.authorize({
serverAuth: {
access_token: ACCESS_TOKEN
}
});
/**
* 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:users',
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>
</head>
<body>
<div id="embed-api-auth-container"></div>
<div id="chart-container"></div>
<div id="view-selector-container"></div>
</body>
</html>
答案 0 :(得分:4)
您首先要查找所需帐户的ID值作为默认值。您只需控制日志记录'ids',然后在视图选择器容器中选择选择器即可。这将通过浏览器控制台中的数字。 然后,您需要将'ids'的值设置为此数字。您可以在两个位置更改此设置,首先将其添加到dataChart的查询中。因此,例如,如果您的ID号为12345678,那么您将按如下所示编写它(ids:'ga:12345678'):
var dataChart = new gapi.analytics.googleCharts.DataChart({
query: {
ids: 'ga:12345678',
metrics: 'ga:users',
dimensions: 'ga:date',
'start-date': '30daysAgo',
'end-date': 'yesterday'
},
chart: {
container: 'chart-container',
type: 'LINE',
options: {
width: '100%'
}
}
});
然后,您还需要更改执行dataChart
的ID的值viewSelector.on('change', function(ids) {
dataChart.set({query: {ids: ids}}).execute();
});
所以在查询中第二个'ids'改变如下:
viewSelector.on('change', function(ids) {
dataChart.set({query: {ids: 'ga:12345678'}}).execute();
});
答案 1 :(得分:2)
我有同样的想法,因为页面上的viewSelector实际上造成了一些安全漏洞。我最终做的是为我想看的单一视图(GID)和我的控制台帐户的客户端ID创建常量。
/********************** Constants ****************************/
var GID = {query: {ids:'ga:xxxxxxxxx'}};
var CLIENT_ID = 'xxxxxxxxxx-xxxxxxxxxxxx.apps.googleusercontent.com';
/*************************************************************/
我保留了授权码
gapi.analytics.auth.authorize({
container: 'auth-button',
clientid: CLIENT_ID,
});
我删除了viewSelector Code
var viewSelector = new gapi.analytics.ViewSelector({
container: 'view-selector'
});
执行Datachart对象时。我用过这段代码。
sessions.set(GID).execute();
其他一切都保持不变。