【问题标题】:Query multiple ID's on Google Analytics Javascript API在 Google Analytics Javascript API 上查询多个 ID
【发布时间】:2014-10-15 18:14:28
【问题描述】:

我正在使用 Google Analytic 的 JavaScript API。

我正在查询用户对数据范围的计数,并将视图 ID 传递给查询:

gapi.client.analytics.data.ga.get({
    'ids': 'ga:XXXXXXXX',
    'start-date': '2014-10-01',
    'end-date': '2014-10-15',
    'metrics': 'ga:users'
}).execute(function (results) {
    if (results.rows && results.rows.length)
        console.log(results.rows[0][0]);
});

但是,由于我正在处理使用配额,因此我需要查询多个视图。

有没有办法在同一个查询中为多个 id 请求报告?

类似:

 gapi.client.analytics.data.ga.get({
    'ids': 'ga:XXXXXXXX, ga:XXXXXXXXX, ga:XXXXXXXXXX', //Which obviously doesn't work

【问题讨论】:

    标签: google-analytics google-api google-analytics-api


    【解决方案1】:

    回答:不,无法请求多个配置文件 ID

    Google 分析报告 API reference

    ids=ga:12345 ga:XXXX 形式的唯一表 ID,其中 XXXX 是 Analytics 查询将为其检索数据的视图(配置文件)ID。

    即使它被称为 IDS 并不意味着您可以发送多个。你不能一次只能发送一个。这是一个独特的价值,您将不得不多次运行您的请求。如果您考虑一下,这是有道理的,因为您将要返回的数据将在不同的配置文件 ID 之间混合,您将无法分辨数据来自哪个配置文件,因为配置文件不是维度。

    【讨论】:

      【解决方案2】:

      这个答案可能有点晚,但我想帮助那些可能有类似用例的人。借助 Google Analytics Reporting API V4,我找到了一种方法来做到这一点,同时还可以在可能有数百行的表上提取 3 个不同日期范围的信息。

      field metrics 下,您可以添加别名。这是我传递解析 JSON 响应所需的视图 ID 和日期范围信息的地方。

      以下内容基于 Google's Hello Analytics 示例 - 可能有更好的方法可以做到这一点,但这满足了我目前的需求。

      我在下面列出了表格的结构方式以及正常运行的 javascript。表格是这样设置的,所以我也可以使用list.js 进行排序。

      var VIEW_ID = ''
       function runReport() {
           //tbody has id gReport and I want to loop through each row
           $("#gReport tr").each(function() {
            //VIEW_ID is pulled from the table data
               VIEW_ID = $(this).find('.view').html();
               // Run the function with the set time frames along with a td I need to manipulate
               queryReports('30daysAgo', '.thirty')
               queryReports('7daysAgo', '.seven')
               queryReports('yesterday', '.one')
           });
       };
      
      function queryReports(startDate, tdClass) {
           return gapi.client.request ({
               path: '/v4/reports:batchGet',
               root: 'https://analyticsreporting.googleapis.com/',
               method: 'POST',
               body: {
                   reportRequests: [
                       {
                           viewId: VIEW_ID,
                           dateRanges: [
                               {
                                   startDate: startDate,
                                   endDate: 'today'
                               }
                                           ],
                           metrics: [
                               {
                                   expression: 'ga:sessions',
                                   alias: VIEW_ID + ":" + tdClass
                               }
                           ]
                       }
                   ]
               }
           }).then(displayResults, console.error.bind(console));
       }
       function displayResults(response) {
       // find the alias string in JSON
           i = response.result.reports[0].columnHeader.metricHeader.metricHeaderEntries[0].name
           // manipulate string so I have variables to update on the table
           c = i.split(":").pop();
           u = i.split(":").shift();
           // return value given to me by google
           countValue = response.result.reports[0].data.totals[0].values[0]
           // find specific td of the row that contains the profile id and the time class I want to manage
           $("tr:contains('" + u + "')").find(c).html(countValue);
      }
      <!--- table row setup - I also have a tbody with the ID gReport -->
      <tr>
         <td class="site"><a href="https://"></a></td>
         <td class="proj"><a href=""></a></td>
         <td class="ga"></td>
         <td class="view"></td>
         <td class="thirty"></td>
         <td class="seven"></td>
         <td class="one"></td>
         <td class="notes"></td>
      </tr>

      【讨论】:

        【解决方案3】:

        是的,根据 GA Core Reporting API 文档,您一次只能查询一个视图 ID(在同一个查询中)。

        但是,您可能会决定在运行查询时选择一些编程语言脚本并遍历多个视图 ID。这就是我使用 R 语言 和一个名为 RGoogleAnalytics 的包(它允许连接到 GA API)所做的。运行 for 循环 允许我从多个视图 ID 检索数据并将其存储在同一个数据集中。这是一篇解释如何在 R 中执行此操作的帖子:http://www.analyticsforfun.com/2015/05/query-multiple-google-analytics-view.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-14
          • 1970-01-01
          • 1970-01-01
          • 2018-04-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多