我正在尝试在美国地图上显示“站点上的活跃用户”计数及其位置。我们安装了 Google Analytics,我们的仪表板正在获取实时数据。我正在尝试将其插入到我们服务器上托管的网页上,以显示在我们公司大楼内的各种屏幕上。
我已通读此文档,但在放置此代码的网页上没有看到任何显示的内容。它只是一个白屏,控制台中没有错误。
看看这部分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>
最终,我想嵌入并显示以下两个元素: