google 分析核心 api 结果抽样级别

google analytics core api results sampling level

我有一个脚本从 Google 分析核心 api 中提取数据。由于我正在使用数据结果成功填充 GSheets 中的 sheet,我知道我的数据拉取是成功的。

我正在阅读文档 here

特别是这个 table:

不过,我想Logger.log()查询的采样级别:

// check sampling for each report
    if(!results.containsSampledData) {
      Logger.log('sampling: none');
    } else {
      Logger.log('sampling: ' + results.query.samplingLevel);
    }

当我查看日志时,我得到 'sampling: undefined'。

如何从结果对象中获取采样结果?

这是生成结果对象的原因,但我认为它不相关(但可能是错误的):

// get GA data from core api
function getReportDataForProfile(profile, len_results, start_num) {
  var startDate = getLastNdays(30); // set date range here
  var endDate = getLastNdays(0);

  var optArgs = {
    'dimensions': 'ga:dimension5, ga:dimension4', // Comma separated list of dimensions.
    'start-index': start_num,
    'max-results': len_results,
    'filters': 'ga:source==cj'
  };

      // Make a request to the API.
      var results = Analytics.Data.Ga.get( // mcf for multi channel api, Ga for core
          profile,                    // Table id (format ga:xxxxxx).
          startDate,                  // Start-date (format yyyy-MM-dd).
          endDate,                    // End-date (format yyyy-MM-dd).
          'ga:goalCompletionsAll, ga:users, ga:sessions', // Comma seperated list of metrics.
          optArgs);

      return results;
    }

我想你错过了这句话:

The following table summarizes all the query parameters accepted by the Core reporting API.

这些是查询参数。换句话说,您提供的价值。所以,既然你确定了采样级别,你应该已经知道它是什么了。

Here's the doc 采样级别。如果未提供,它将 samplingLevel 设置为 DEFAULT。

编辑: Here's the doc 回复。我看到它确实包含一个 samplingLevel 字段,但如果您进一步向下滚动,samplingLevel 不是响应字段 table 中描述的字段之一。我怀疑它是无意中包含在响应中的,或者由于缺少文档而您不能依赖该字段。

啊。如果我往下读,我会看到这一段:

Sampling

Google Analytics calculates certain combinations of dimensions and metrics on the fly. To return the data in a reasonable time, Google Analytics may only process a sample of the data.

You can specify the sampling level to use for a request by setting the samplingLevel parameter.

If a Core Reporting API response contains sampled data, then the containsSampledData response field will be true. In addition, 2 properties will provide information about the sampling level for the query: sampleSize and sampleSpace. With these 2 values you can calculate the percentage of sessions that were used for the query. For example, if sampleSize is 201,000 and sampleSpace is 220,000 then the report is based on (201,000 / 220,000) * 100 = 91.36% of sessions.

See Sampling for a general description of sampling and how it is used in Google Analytics."

因此,为了获得百分比形式的样本量(我以前经常看到的),我这样做了:results.sampleSize/results.sampleSpace