Google Analytics Embed API:设置选择器显示默认值

Cor*_*ory 5 google-analytics google-analytics-api

我正在使用 Google Analytics Embed API。下面是我正在使用 Google 开发页面的代码示例。有没有办法设置选择器的默认值?账户 | 物业 | 看法

<!doctype html>
<html lang="en">
    <head>
    <title>Google Charts</title>
        <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>

        <script>
        gapi.analytics.ready(function() {
        var ACCESS_TOKEN = 'xxxxx'; // obtained from your service account

        gapi.analytics.auth.authorize({
            serverAuth: {
            access_token: ACCESS_TOKEN
            }
        });


        /**
           * 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:users',
           dimensions: 'ga:date',
           'start-date': '30daysAgo',
           '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>

</head>

<body> 

    <div id="embed-api-auth-container"></div>
    <div id="chart-container"></div>
    <div id="view-selector-container"></div> 

</body>
</html>  
Run Code Online (Sandbox Code Playgroud)

小智 4

您首先要找到您想要作为默认帐户的 ids 值。您只需通过控制台记录“ids”,然后在视图选择器容器中选择选择器即可完成此操作。这将超过浏览器控制台中的一个数字。然后,您需要将“ids”的值设置为该数字。您可以在两个地方更改它,首先将其添加到 dataChart 的查询中。例如,如果您的 ids 号码是 12345678,那么您将如下所示编写它( ids: 'ga:12345678' ):

var dataChart = new gapi.analytics.googleCharts.DataChart({
         query: {

         ids: 'ga:12345678',

         metrics: 'ga:users',
         dimensions: 'ga:date',
         'start-date': '30daysAgo',
         'end-date': 'yesterday'
         },
         chart: {
         container: 'chart-container',
         type: 'LINE',
         options: {
         width: '100%'
            }
        }
      });
Run Code Online (Sandbox Code Playgroud)

然后,您还需要更改执行 dataChart 的 ids 的值

viewSelector.on('change', function(ids) {
    dataChart.set({query: {ids: ids}}).execute();
});
Run Code Online (Sandbox Code Playgroud)

因此,在查询中,第二个“ids”发生更改,如下所示:

viewSelector.on('change', function(ids) {
    dataChart.set({query: {ids: 'ga:12345678'}}).execute();
    });
Run Code Online (Sandbox Code Playgroud)

  • 问题是如何为视图选择器组件设置预定义的“ids”属性。你的回答完全是关于不同的事情。 (2认同)