Search code examples
reactjsnext.jsgoogle-analytics-apigoogle-analytics-4

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


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?


Solution

  • 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