【问题标题】:Google Analytics Embed API: Set selector display defaultsGoogle Analytics Embed API:设置选择器显示默认值
【发布时间】:2014-12-17 17:23:03
【问题描述】:

我正在使用 Google Analytics 嵌入 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>  

【问题讨论】:

  • 不幸的是,我没有找到任何方法为视图选择器设置默认“id”。
  • 作为替代方案,您可以查看嵌入 API 的 Web 组件实现:demoapicode。它确实支持将“ids”设置为视图选择器。我刚刚对此进行了测试,并通过示例创建了一个git repo
  • 如果您不想深入研究 Web Components/Polymer 并且有很多依赖项,那么另一个选择是使用来自 Google Account Explorer demo pageViewSelector2 组件。在 GitHub 上获取代码:view-selector2.js.
  • 对上一条评论的补充:使用viewSelector.set({viewId: '{your_view_id}'});函数。查看它在 Google 网站中的使用方式:index.js

标签: google-analytics google-analytics-api


【解决方案1】:

您首先要找到您想要作为默认帐户的 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%'
            }
        }
      });

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

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

所以在查询中第二个 'ids' 改变如下:

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

【讨论】:

  • 问题是如何为视图选择器组件设置预定义的 'ids' 属性。你的回答完全是关于不同的事情。
  • 谢谢!!。我最初尝试了马修的建议,但没有奏效。 @whyleee
【解决方案2】:

我也有同样的想法,页面上的 viewSelector 实际上构成了一些安全漏洞。我最终做的是为我想查看的单一视图 (GID) 和我的控制台帐户的客户端 ID 创建常量。

/********************** Constants ****************************/
var GID = {query: {ids:'ga:xxxxxxxxx'}};
var CLIENT_ID = 'xxxxxxxxxx-xxxxxxxxxxxx.apps.googleusercontent.com';
/*************************************************************/

我保留了授权码

gapi.analytics.auth.authorize({
  container: 'auth-button',
  clientid: CLIENT_ID,
});

我删除了 viewSelector 代码

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

执行 Datachart 对象时。我使用了这个代码。

sessions.set(GID).execute();

其他一切都保持不变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多