如何在 Google 分析数据 API 中使用 FilterExpression Google Apps 脚本

How To use FilterExpression in Google Analytics Data API in Google Apps Script

我正在尝试使用新的 Google 分析数据 API 从我们的 GA4 属性 中提取一些非常简单的数据并放入电子表格中。

这一切都很好。

不过,我现在想应用一些维度过滤器,以便 returns 只有那些与过滤器匹配的行。

这是我添加的代码,但我认为格式有误。

const dimensionfilter = AnalyticsData.newFilterExpression();
dimensionfilter.filter.fieldName = 'pageTitle';
dimensionfilter.filter.stringFilter.value = 'MYPAGETITLETEXT';

Apps 脚本中没有添加 DimensionFilter 等的示例

有人做过吗?有没有人有任何非常简单的例子。

非常感谢。

这是我的完整代码

   function runReport() {
  const propertyId = '29045017783';

  try {
    const metric = AnalyticsData.newMetric();
    metric.name = 'screenPageViews';


    const pagetitle = AnalyticsData.newDimension();
    pagetitle.name = 'pageTitle';

    const pagepath = AnalyticsData.newDimension();
    pagepath.name = 'pagePath';

    const dateRange = AnalyticsData.newDateRange();
    dateRange.startDate = '2022-05-01';
    dateRange.endDate = 'today';


    const dimensionfilter = AnalyticsData.newFilterExpression();
    dimensionfilter.filter.fieldName = 'pageTitle';
    dimensionfilter.filter.stringFilter.value = 'MYPAGETITLETEXT';


   

    const request = AnalyticsData.newRunReportRequest();
    request.dimensions = [pagetitle, pagepath];
    request.metrics = [metric];
    request.dateRanges = dateRange;
    request.limit=10;
    request.dimensionFilter = dimensionfilter;

    const report = AnalyticsData.Properties.runReport(request,'properties/' + propertyId);

    if (!report.rows) {
      Logger.log('No rows returned.');
      return;
    }

    const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = spreadsheet.getActiveSheet();

    // Append the headers.
    const dimensionHeaders = report.dimensionHeaders.map(
        (dimensionHeader) => {
          return dimensionHeader.name;
        });
    const metricHeaders = report.metricHeaders.map(
        (metricHeader) => {
          return metricHeader.name;
        });
    const headers = [...dimensionHeaders, ...metricHeaders];

    sheet.appendRow(headers);

    // Append the results.
    const rows = report.rows.map((row) => {
      const dimensionValues = row.dimensionValues.map(
          (dimensionValue) => {
            return dimensionValue.value;
          });
      const metricValues = row.metricValues.map(
          (metricValues) => {
            return metricValues.value;
          });
      return [...dimensionValues, ...metricValues];
    });

    sheet.getRange(2, 1, report.rows.length, headers.length)
        .setValues(rows);

    Logger.log('Report spreadsheet created: %s',
        spreadsheet.getUrl());
  } catch (e) {
    // TODO (Developer) - Handle exception
    Logger.log('Failed with error: %s', e.error);
  }
}

这是应用维度过滤器的方法

const dimensionfilter = AnalyticsData.newFilterExpression()
dimensionfilter.filter = AnalyticsData.newFilter()
dimensionfilter.filter.fieldName = 'pageTitle'
dimensionfilter.filter.stringFilter = AnalyticsData.newStringFilter()
dimensionfilter.filter.stringFilter.value = 'MYPAGETITLETEXT'

编辑:针对多个过滤器

将多个过滤器与 OR 条件相结合

const pageTitles = [
  'MYPAGETITLETEXT1',
  'MYPAGETITLETEXT2'
]

const dimensionfilter = AnalyticsData.newFilterExpression()
dimensionfilter.orGroup = AnalyticsData.newFilterExpressionList()
dimensionfilter.orGroup.expressions = []

for (const pageTitle of pageTitles) {
  const filterExpression = AnalyticsData.newFilterExpression()
  filterExpression.filter = AnalyticsData.newFilter()
  filterExpression.filter.fieldName = 'pageTitle'
  filterExpression.filter.stringFilter = AnalyticsData.newStringFilter()
  filterExpression.filter.stringFilter.value = pageTitle
  dimensionfilter.orGroup.expressions.push(filterExpression)
}

你可以参考这篇文档了解How method signatures are determined