1

I'm getting the error cb=gapi.loaded_0:102 Uncaught [object Object] when I try to use this Google Analytics embed code to retrieve data from the platform. I got this code directly from the google developers website:

<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>

I'm not sure what's going on here, I've been searching everywhere for a solution for this, but can't find anything. It looks like it's something with the https://apis.google.com/js/platform.js, perhaps that API is deprecated.

1 Answers1

0

It appears to be a problem with the API version. Google will just leave everything as is without warning anyone that the things are deprecated.... I have solved the problem by using the V4 API, here https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js

Just follow all instructions carefully. Have in mind that after you have created the credentials, it may take some time until they start making effect.

EDIT: Nevermind, looks like I was wrong to some point. Both examples I've provided are correct, there is nothing deprecated. The only problem is the credentials part. It wasn't possible for me to make the first example work because I didn't configure the origins in the credentials and waited until they would take effect.

I believe that's case closed now. There, the problem lies within the credentials. Which can be created here: https://console.cloud.google.com/apis/credentials?pli=1

In my case I've included both my site url and localhost:8080 in the credentials origins.

Be happy. :)