我正在使用 Embed API 从 ServiceAccount 在我的网站上包含 GA 图表。
该网站是一个使用 AngularJS 的单页应用程序。
我为每种图表类型和控制器创建了多个指令,将其命名GAController
,在他们之间共享。在这个控制器中我使用gapi.analytics.ready(..)
等待分析对象完全加载。
问题是当GAController
页面刷新后第一次调用,然后gapi.analytics.ready(..)
通过回调函数调用,然后执行身份验证和其他逻辑。
但是,如果我转到此页面第二次FE从其他页面通过ui-router
,然后将回调添加到ready
但没有被执行。完全没有。
在模板页面底部,有gapi 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'));
我的代码如下:
gapi.analytics.ready(function () {
// this is not called the second time
if (!gapi.analytics.auth.isAuthorized()) {
gapi.analytics.auth.authorize({
serverAuth: $scope.$parent.getAccessToken()
});
}
// do some other logic
});
但所有的时候gapi.analytics
对象处于相同状态{ q: [], ready: function()...}
.
有人可以帮助我了解发生了什么事吗?
谢谢你的帮助