英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论