Google Charts Embed API is not working

只谈情不闲聊 提交于 2019-12-23 15:44:44

问题


I'm using Embed API to include GA charts on my site from ServiceAccount. The site is a Single Page application with AngularJS. I've created several directives for every chart type and a controller, name it GAController, shared between them. In this controller i use gapi.analytics.ready(..) to wait until analytics object is fully loaded.

The problem is that when GAController is called for first time after page refresh, then gapi.analytics.ready(..) is called with callback function and then authentication and other logic executes. Hovewer, if i go to this page for the second time f.e. from other page via ui-router, then callback is added to ready but not executed. At all.

at the template page bottom, there's 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'));

my code is the following:

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
});

but all the times gapi.analytics object is in the same state { q: [], ready: function()...}.

Can somebody help me to understand what's going on. Thanks for any help


回答1:


I found the answer. You can try this, alternate the "gapi script"

                    (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'));


来源:https://stackoverflow.com/questions/32403126/google-charts-embed-api-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!