Search code examples
reactjsreact-nativeapigoogle-analyticsgoogle-analytics-api

Getting dates from google analytics API


I am trying to get the individual dates from data retrieved from the google analytics api. I am trying to emulate what the google analytics app does.

So for example the google analytics app has a line chart and if the date range is April 20-25 and the total users is 4, it shows that there were 2 users on the 22nd and 2 on the 27th. etc...

But when I retrieve data using the api such as

 const url = `https://analyticsdata.googleapis.com/v1beta/properties/${propertyId}:runReport`;

  const requestBody = {
    metrics: [
      {
        name: 'activeUsers',
      },
      {
        name: 'bounceRate',
      },
      {
        name: 'averageSessionDuration',
      },
      {
        name: 'engagedSessions',
      },
      {
        name: 'eventCount',
      },
      {
        name: 'eventsPerSession',
      },
      {
        name: 'screenPageViewsPerSession',
      },
      {
        name: 'sessions',
      },
      {
        name: 'sessionsPerUser',
      },
      { name: 'totalUsers' },
    ],
    dateRanges: [
      {
        startDate: startDate,
        endDate: endDate,
        name: 'date_range',
      },
    ],
  };
 const response = await axios.post(url, requestBody, requestOptions);

The data returns this

{
    "metricHeaders": [
        {
            "name": "activeUsers",
            "type": "TYPE_INTEGER"
        },
        {
            "name": "bounceRate",
            "type": "TYPE_FLOAT"
        },
        {
            "name": "averageSessionDuration",
            "type": "TYPE_SECONDS"
        },
        {
            "name": "engagedSessions",
            "type": "TYPE_INTEGER"
        },
        {
            "name": "eventCount",
            "type": "TYPE_INTEGER"
        },
        {
            "name": "eventsPerSession",
            "type": "TYPE_FLOAT"
        },
        {
            "name": "screenPageViewsPerSession",
            "type": "TYPE_FLOAT"
        },
        {
            "name": "sessions",
            "type": "TYPE_INTEGER"
        },
        {
            "name": "sessionsPerUser",
            "type": "TYPE_FLOAT"
        },
        {
            "name": "totalUsers",
            "type": "TYPE_INTEGER"
        }
    ],
    "rows": [
        {
            "metricValues": [
                {
                    "value": "4"
                },
                {
                    "value": "0.6"
                },
                {
                    "value": "29.2088052"
                },
                {
                    "value": "2"
                },
                {
                    "value": "45"
                },
                {
                    "value": "9"
                },
                {
                    "value": "4.2"
                },
                {
                    "value": "5"
                },
                {
                    "value": "1.25"
                },
                {
                    "value": "4"
                }
            ]
        }
    ],
    "rowCount": 1,
    "metadata": {
        "currencyCode": "USD",
        "timeZone": "Etc/GMT-3"
    },
    "kind": "analyticsData#runReport"
}

So it just shows totals without showing explicitly when the users were active.

Am I missing a parameter or is it simply not possible to get those dates?

By the way I am trying to fetch the new GA4 tracking id's.

Thanks for your time


Solution

  • From your requestBody I see that you are requesting the metrics only, for this reason you are getting the aggregated data for the entire selected period.

    You should also enter the date dimension to have the breakdown of values by date in the desired range.