当我尝试使用此Google Analytics嵌入代码从平台检索数据时,我收到错误cb=gapi.loaded_0:102 Uncaught [object Object]
。我直接从谷歌开发者网站获得了这段代码:
<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"></div>
<div id="chart-container"></div>
<div id="view-selector-container"></div>
<script>
gapi.analytics.ready(function() {
/**
* Authorize the user immediately if the user has already granted access.
* If no access has been created, render an authorize button inside the
* element with the ID "embed-api-auth-container".
*/
gapi.analytics.auth.authorize({
container: 'embed-api-auth-container',
clientid: '738804163208-286qgip15f1jmkjtdbpfnp7824e1qviv.apps.googleusercontent.com'
});
/**
* 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:sessions',
dimensions: 'ga:date',
'start-date': '90daysAgo',
'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>
我不确定这里发生了什么,我一直在寻找解决方案,但无法找到任何解决方案。它似乎与https://apis.google.com/js/platform.js
有关,也许该API已被弃用。
答案 0 :(得分:0)
这似乎是API版本的问题。谷歌将保留所有内容,而不会警告任何人不赞成的事情.... 我已使用V4 API(https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js
)解决了这个问题请仔细遵循所有说明。请记住,在创建凭据后,可能需要一些时间才能开始生效。
编辑:没关系,看起来我错了。我提供的两个例子都是正确的,没有任何弃用。唯一的问题是凭证部分。我不可能让第一个例子起作用,因为我没有在凭证中配置起源并一直等到它们生效。我相信现在这个案子已经结束了。在那里,问题在于凭证。可在此处创建:https://console.cloud.google.com/apis/credentials?pli=1
在我的情况下,我在凭据来源中包含了我的网站网址和localhost:8080。
快乐。 :)