Learn business growth with Google Analytics 4 Forums Google Analytics 4 How to retrieve report data from Google Analytics 4 (GA4)

  • How to retrieve report data from Google Analytics 4 (GA4)

    Posted by Liam on 21 February 2023 at 4:43 pm

    Hey folks! Just wondering, has anyone else been using @google-analytics/data to pull data from Google Analytics? It’s looking good so far in the example from the library. But here’s the thing – it’s still in BETA. Does that make anyone nervous? Like, could it flip out on me at any moment? ‘Cause that’s what I’m thinking!

    Here’s a snippet from the official example:

    /**
     * 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/<span class="hljs-subst">${propertyId}</span>,
        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 was able to get this up and running in my app, and it’s doing its thing perfectly. But yeah, the BETA label is worrying me a bit. Anyone got an alternative in case this one blows up on me? Much appreciated!

    Liam replied 1 year, 4 months ago 3 Members · 2 Replies
  • 2 Replies
  • Lucy

    Member
    6 April 2023 at 3:37 pm

    Hey, I get your point with the whole BETA thing, it sure does add a spice of thrill, doesn’t it? But you know what, that’s technology for you, always evolving and improving. So, yeah, the API is still in beta, no doubt about that. There’s no other Google-sanctioned way currently to extract data from a GA4 account. So, let’s ride this wave together, my friend! And remember, sometimes trying out something different (even something in BETA) is what sparks brilliant solutions. Hope everything goes smoothly with your app!

  • Liam

    Member
    7 April 2023 at 5:06 pm

    Yes, many have been using the @google-analytics/data library to fetch data from Google Analytics within their applications. However, your concern is valid. The ‘BETA’ label means the library is not yet officially released as a stable version, so it may contain bugs or the API might change in the future. While it may work perfectly at the moment, issues could arise unexpectedly, which is something to consider when building something crucial for your application that needs high reliability. For alternatives, you can consider googleapis, another Google’s Node.js client library which supports Google Analytics as well. Just remember no matter what library or API you use, the best practice is to be prepared to handle any potential errors or changes to ensure your application runs smoothly.

Log in to reply.