简体   繁体   English

在同一页面上添加多个Google Analytics(分析)图表

[英]Adding multiple Google Analytics charts on the same page

I have a dashboard in which I need to incorporate Google Analytics charts. 我有一个仪表板,需要在其中合并Google Analytics(分析)图表。 Each chart is a different widget and can be removed from or added to the dashboard. 每个图表是一个不同的小部件,可以从仪表板中删除或添加到其中。 The charts display properly when there is only ONE added to the dashboard. 当仪表板上仅添加一个时,图表将正确显示。 When I add two, they don't display anything anymore. 当我添加两个时,它们不再显示任何内容。 Could the fact that I call the same Embed API library in each widget cause this? 我在每个小部件中调用相同的Embed API库的事实会导致这种情况吗?

An example of the code I am using for each widget: 我为每个小部件使用的代码示例:

 <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-1"></div> <div id="chart-container-1"></div> <div id="view-selector-container-1"></div> <script> gapi.analytics.ready(function() { gapi.analytics.auth.authorize({ container: 'embed-api-auth-container-1', clientid: '***' }); var viewSelector = new gapi.analytics.ViewSelector({ container: 'view-selector-container-1' }); viewSelector.execute(); var dataChart = new gapi.analytics.googleCharts.DataChart({ query: { metrics: 'ga:sessions', dimensions: 'ga:date', 'start-date': '30daysAgo', 'end-date': 'yesterday' }, chart: { container: 'chart-container-1', type: 'LINE', options: { width: '100%' } } }); viewSelector.on('change', function(ids) { dataChart.set({query: {ids: ids}}).execute(); }); }); </script> 

Managed to temporarily fix this by calling the Embed API library ONLY in the first widget that appears on the page. 通过仅在页面上显示的第一个窗口小部件中调用Embed API库来临时解决此问题。 Not ideal, but it works for now. 并不理想,但目前可以使用。 If anybody figures out the right solution, I'd like to know. 如果有人能找到正确的解决方案,我想知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM