如何从Google Analytic 4(GA4)中获取报告数据?

huangapple go评论106阅读模式
英文:

How to get report data from google analytic in Google Analytic 4 (GA4)?

问题

I use @google-analytics/data 来获取谷歌分析数据,代码示例如下(来自库的官方示例),但我认为这个库仍处于Beta版本,是否有其他人可以提供类似功能的替代方案?

/**
 * TODO(开发者):在运行示例之前取消注释此变量并替换为您的
 * Google Analytics 4属性ID。
 */
// propertyId = 'YOUR-GA4-PROPERTY-ID';

// 导入Google Analytics Data API客户端库。
const { BetaAnalyticsDataClient } = require('@google-analytics/data');

// 使用默认构造函数指示客户端使用在GOOGLE_APPLICATION_CREDENTIALS环境变量中指定的凭据。
const analyticsDataClient = new BetaAnalyticsDataClient();

// 运行一个简单的报告。
async function runReport() {
  const [response] = await analyticsDataClient.runReport({
    property: `properties/${propertyId}`,
    dateRanges: [
      {
        startDate: '2020-03-31',
        endDate: 'today',
      },
    ],
    dimensions: [
      {
        name: 'city',
      },
    ],
    metrics: [
      {
        name: 'activeUsers',
      },
    ],
  });

  console.log('报告结果:');
  response.rows.forEach(row => {
    console.log(row.dimensionValues[0], row.metricValues[0]);
  });
}

runReport();

我已为我的应用程序实施了此代码,并且运行正常,但由于仍处于Beta版本,我仍然担心将其用于生产。也许有人可以提供替代方案?

英文:

i use @google-analytics/data, to fetch data from google analytic, its look like in the code (official example from the library), but i think this one still Beta, is there anyone have alternative for to do same thing?

/**
 * TODO(developer): Uncomment this variable and replace with your
 *   Google Analytics 4 property ID before running the sample.
 */
// propertyId = 'YOUR-GA4-PROPERTY-ID';

// Imports the Google Analytics Data API client library.
const {BetaAnalyticsDataClient} = require('@google-analytics/data');

// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
const analyticsDataClient = new BetaAnalyticsDataClient();

// Runs a simple report.
async function runReport() {
  const [response] = await analyticsDataClient.runReport({
    property: `properties/${propertyId}`,
    dateRanges: [
      {
        startDate: '2020-03-31',
        endDate: 'today',
      },
    ],
    dimensions: [
      {
        name: 'city',
      },
    ],
    metrics: [
      {
        name: 'activeUsers',
      },
    ],
  });

  console.log('Report result:');
  response.rows.forEach(row => {
    console.log(row.dimensionValues[0], row.metricValues[0]);
  });
}

runReport();

I have implemented the code for my app, and works properly, but still worry to make production because still BETA. Maybe anyone can give me alternative?

答案1

得分: 0

API仍处于测试阶段,目前如果您想从GA4账户提取数据,这是官方API。

英文:

The API is still in beta there is no other option currently if you want to extract data from a GA4 account this is the official api

huangapple
  • 本文由 发表于 2023年5月14日 14:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定