google-analytics - 谷歌分析嵌入 API : Set selector display defaults

标签 google-analytics google-analytics-api

我正在使用 Google Analytics 嵌入 API。下面是我在 Google 的开发页面上使用的代码示例。有没有办法为选择器设置默认值?帐号 |属性(property) |看法

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

最佳答案

您首先要找到您想要作为默认帐户的 ids 值。您只需通过控制台记录“ids”然后在 View 选择器容器中选择选择器来完成此操作。这将在您的浏览器控制台中超过一个数字。
然后您需要将 'ids' 的值设置为这个数字。您可以在两个地方更改此设置,首先将其添加到 dataChart 查询中。因此,例如,如果您的 ID 号是 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%'
            }
        }
      });

然后,您还需要更改执行 dataChart 的 ids 值
viewSelector.on('change', function(ids) {
    dataChart.set({query: {ids: ids}}).execute();
});

所以在查询中,第二个“id”被改变,如下所示:
viewSelector.on('change', function(ids) {
    dataChart.set({query: {ids: 'ga:12345678'}}).execute();
    });

关于google-analytics - 谷歌分析嵌入 API : Set selector display defaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27531279/

相关文章:

google-analytics - 使用Google Analytics(分析)API显示我的网站统计信息

javascript - 捕获 GA 中事件的来源

google-analytics - 谷歌分析 : Invalid value for filters parameter

php - 如何向Google Analytics API请求添加维度

google-analytics - 在 Google Analytics 中查看事件及其参数

android - 谷歌分析 : how to report user scope custom dimentions

magento - 在 Google Analytics 中实现增强型电子商务

google-analytics - 使用 CSP nonce 加载 Google Analytics js 文件

google-analytics - Google的API API getID()的Hello Analytics程序错误

google-analytics - 是否根据身份验证状态切换Google Analytics(分析)ID?