【问题标题】:Google Charts Embed API is not working谷歌图表嵌入 API 不工作
【发布时间】:2015-09-04 16:58:48
【问题描述】:

我正在使用 Embed API 在我的网站上包含来自 ServiceAccount 的 GA 图表。 该站点是一个带有 AngularJS 的单页应用程序。 我为每种图表类型和一个控制器创建了几个指令,将其命名为GAController,在它们之间共享。在这个控制器中,我使用gapi.analytics.ready(..) 等待分析对象完全加载。

问题是当页面刷新后第一次调用GAController时,会通过回调函数调用gapi.analytics.ready(..),然后执行认证等逻辑。 但是,如果我 第二次访问此页面 f.e。从其他页面通过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()...}

有人可以帮助我了解发生了什么。 感谢您的帮助

【问题讨论】:

  • 我也有同样的问题,请大家帮忙

标签: javascript angularjs google-analytics-api


【解决方案1】:

我找到了答案。 你可以试试这个,替换“gapi脚本”

                    (function (w, d, s, g, js, fs) {
                    if ($('#googleCache').length > 0) return;
                    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';
                    js.id = "googleCache";
                    fs.parentNode.insertBefore(js, fs); js.onload = function () { g.load('analytics'); };
                }(window, document, 'script'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多