2 Replies Latest reply on Jan 4, 2018 9:46 AM by Robert Rouse

    Using JavaScript API server-side (Node.js)

    Robert Rouse

      I'm trying to write scripts to get data about several visualizations on Tableau Server and store that in a file or database. This information is only available through the JavaScript API. I'd prefer to use Node.js for the task instead of running things in a browser, but I'm having some trouble making it work. I've tried a few things with the jsdom npm package but with no success. Can anyone help?

       

      The code below returns errors saying 'tableauSoftware' is not defined:

       

      const jsdom = require("jsdom");

       

      const { JSDOM } = jsdom;


      const dom = new JSDOM(`

        <script src="https://public.tableau.com/javascripts/api/tableau-2.1.1.min.js" type="text/javascript"></script>

        <script>

        var startTime = Math.floor(Date.now());

        run_viz();

        function run_viz(){

          //load the viz

          var viz, workbook;

          var vizDiv = document.getElementById('tableau');

          var vizURL = "https://public.tableau.com/views/USvsTHEM/USvs_THEM";

          var options = {

              width: '200px',

              height: '200px',

              //these options change performance

              hideToolbar: true,

              hideTabs: true,

              onFirstInteractive: function () {

                  getAudit();

              }

          };

          viz = new tableauSoftware.Viz(vizDiv, vizURL, options);

      }


      function getAudit(){

              //clock time from start to loaded

              console.log('loaded in '+(Math.floor(Date.now())-startTime)+' ms');

         

              //gather info

              console.log(viz.getWorkbook().getName());

              console.log(viz.getWorkbook().getParametersAsync());

      }

        </script>

        <body></body>`, { runScripts: "dangerously" });

        • 1. Re: Using JavaScript API server-side (Node.js)
          Robin Cottiss

          Hello Robert,

           

          I am not sure if JSDOM is going to provide enough functionality to do what you need to do but it looks like your HTML is incomplete. For example you define <body></body> element but it does not contain the placeholder for the embedded viz. You try getting the placeholder div element in: 

           

          var vizDiv = document.getElementById('tableau');

           

          But that div does not exist. The first thing I might try it to take a working page to embed in your JSDOM variable because the lack of placeholder div might not be the only problem. But as I said I am not sure if JSDOM will work its has limitations. You might try a different approach. have you looked at Puppeteer?

           

          GitHub - GoogleChrome/puppeteer: Headless Chrome Node API

           

          The approach might be different in that you would create a working web page with your embedded content then drive that web page using Puppeteer.

           

          Puppeteer is not easy but the advantage is that it is effectively a complete chrome browser.

           

          Robin

          1 of 1 people found this helpful
          • 2. Re: Using JavaScript API server-side (Node.js)
            Robert Rouse

            Thanks, Robin. I'll try Puppeteer. At first glance it looks a little easier. I tried adding in the Tableau placeholder div but that didn't correct the issue.