Displaying numerous Google Analytics graphs within a single webpage

Incorporating Google Analytics charts into my dashboard has been a challenge. Each chart is displayed as a widget that can be added or removed from the dashboard at will. The issue arises when I try to add more than one chart - they no longer display properly. Could this be due to calling the same Embed API library in each widget?

Here is an example of the code snippet used for each widget:

<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-1"></div>
<div id="chart-container-1"></div>
<div id="view-selector-container-1"></div>

<script>

gapi.analytics.ready(function() {

  gapi.analytics.auth.authorize({
    container: 'embed-api-auth-container-1',
    clientid: '***'
  });


  var viewSelector = new gapi.analytics.ViewSelector({
    container: 'view-selector-container-1'
  });


  viewSelector.execute();

  var dataChart = new gapi.analytics.googleCharts.DataChart({
    query: {
      metrics: 'ga:sessions',
      dimensions: 'ga:date',
      'start-date': '30daysAgo',
      'end-date': 'yesterday'
    },
    chart: {
      container: 'chart-container-1',
      type: 'LINE',
      options: {
        width: '100%'
      }
    }
  });

  viewSelector.on('change', function(ids) {
    dataChart.set({query: {ids: ids}}).execute();
  });

});
</script>

Answer №1

Implemented a temporary solution by including the Embed API library in only the initial widget shown on the page. Although not perfect, it's functioning for the time being. If anyone discovers the correct resolution, please share with me.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to generate a Token in Nexus for private packages without using the UI interface?

We have implemented Sonatype Nexus Repository ManagerOSS 3.29.0-02 and are currently facing an issue in generating a TOKEN that can be used with .npmrc following this specific structure: registry=http://NEXUS-IP:8081/repository/GROUP-NAME http://NEXUS-IP:8 ...

Unlocking Bootstrap variables in Vue development (using Vanilla Bootstrap)

What is the best way to customize or theme bootstrap? I have heard that using sass to override bootstrap variables is recommended, but how can I integrate this into the Vue webpack workflow? I tried searching online and found suggestions to edit the vue.c ...

Issue with Docker Backend Deployment: Unable to Locate '/app/package.json' - Troubleshooting on DigitalOcean

I'm encountering an issue in my deployment logs where there's an error indicating that the package.json file cannot be found. I suspect this may be related to the volumes I've configured, but I'm having trouble pinpointing the problem. ...

The JSON output is throwing an error because it is unable to access the property '0' since it is

Trying to convert JSON data into an HTML table, I encountered the following error: "Cannot read property '0' of undefined" <?php $idmatchs = "bGdzkiUVu,bCrAvXQpO,b4I6WYnGB,bMgwck80h"; $exploded = explode(",", $idmatchs); $count = count( ...

Is it advisable to solely rely on CDN for incorporating Bootstrap JS components, or are there any potential drawbacks to

I'm in the process of customizing Bootstrap using my own styles, by utilizing a local version of the source SASS files as outlined in the official documentation, and importing them into a custom.scss file. My main focus is on altering the visual aspe ...

Choosing specific information from a deeply nested JSON structure

I am currently dealing with a multi-level JSON object that includes an array of 143 other objects. When I run console.log(obj) on this object, the following output is displayed: 0: Object ActFTEs: 0.00 Actual: 11111 Bud_Month: "October" FY_CD: 201 ...

Creating a dynamic Bootstrap 5 carousel with active class on the first item and indicators

I am curious to know how to create a dynamic Bootstrap 5 carousel. Specifically, I want to know how to display indicators dynamically and add an active class to the first item of the carousel. In Bootstrap 4, it is done in the following way: $('#main ...

What is the best way to transfer an item to another fixed object using THREE.JS?

Having just started out, I've managed to create a scene, renderer, camera, and two objects in THREE.JS. Now I'm trying to figure out how to move one object to another object. I've tried a few things but haven't had much success. Any gui ...

Updating hrefs within an array using a JavaScript function

I specialize in creating web resumes and would like to include URLs for my projects and previous employers. I attempted to use a function for this purpose, but encountered an issue where only the first project was being updated with the URL from the last e ...

Avoid nesting ternary expressions when possible - consider alternative solutions

I am currently working on a React component that has 4 different states: advisoryResults, results, noResults, and newAccount. While I initially thought about using a ternary expression for this, it turns out that it is not allowed in this case. Can you su ...

Material-UI and TypeScript are having trouble finding a compatible overload for this function call

Currently, I'm in the process of converting a JavaScript component that utilizes Material-ui to TypeScript, and I've encountered an issue. Specifically, when rendering a tile-like image where the component prop was overridden along with an additi ...

Receiving multiple NodeJS Responses through AJAX for a single request

I have been working on a WebApp that involves heavy AJAX calls from the frontend and NodeJS Express at the backend. Here is a glimpse of my Frontend Code- Below is the global AJAX function I consistently use in all my projects: function _ajax(params = {}, ...

Organizing a JSON array into separate arrays

I have come across the following JSON array {"label":[{"name":"Government Schools", "datapoints":[{"year":"2007-2008","total":"1300"}, {"year":"2008-2009","total":"1280"}, {"year":"2009-2010","tot ...

A new reference is created when Angular.copy is used

Whenever I attempt to duplicate a new object into an old object using angular.copy, the reference changes. If I try using = or JSON.parse(JSON.stringify(newObj)), the view does not reflect the new value. Is there a solution to this issue? Here is a code ...

Whenever I try to open my jQuery UI Dialog without first displaying an alert, it does not work

Beginning of my HTML page, initializing a dialog : <script type="text/javascript"> $(function() { $( "#dialog-confirm" ).dialog({ autoOpen: false, resizable: true, height:180, width:300, modal: true, buttons: { "Yes": ...

Utilize the power of Await Async while iterating through a massive JSON dataset

The data in the JSON file is quite extensive, weighing around 20MB. My goal is to ensure that the age returned is accurate, whether by waiting for the result or looping through the entire file. Currently, the output is always 0 even when the actual age is ...

Error: The parameter "callback" must be in the form of a function

Following a tutorial to upload images to Twitter using Node.js with Twit. Here is the code: function upload_random_image(){ console.log('Opening an image...'); var image_path = path.join(__dirname, '/random_cam/' + random_cam()), ...

Troubleshooting Token Issue in Node.Js with Jest Testing (Encountering 401 "Unauthorized" Error)

Hey there, I am currently experimenting with Jest on a Node.js project where I need to test my functions. const userOne = { _id: userOneId, name: 'Mike', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa9 ...

JavaScript file encountering a Typescript issue with a property defined in a subclass

Currently, I am utilizing Typescript to validate my Javascript files. One issue I have encountered is that when I inherit from a class, Typescript does not recognize the types of the properties in the parent class. I am unsure if I am overlooking something ...

CORS - Preflight request response fails access control verification

I've been attempting to send a GET request to a server with the same domain as my local host, but I keep encountering the following error: The preflight request response fails the access control check: The requested resource does not have an ' ...