google-analyticsgoogle-analytics-4google-analytics-sdk

Unique Row ID in GA4 Query


I am using the following query to collect information for my organisation.

{
   "dimensions":[
      {
         "name":"date"
      },
      {
         "name":"deviceCategory"
      },
      {
         "name":"eventName"
      }
   ],
   "metrics":[
      {
         "name":"eventCount"
      },
      {
         "name":"sessions"
      },
      {
         "name":"totalUsers"
      }
   ],
   "dateRanges":[
      {
         "startDate":"2023-09-12",
         "endDate":"2023-09-18"
      }
   ],
   "offset":"0",
   "limit":"1"
}

We store this in our DB and assign an auto-generated PK. Our data team uses this information in the downstream process to run reports. However, due to our business requirements, we delete and insert N-days worth of data during every import. This causes the PK to change. For example, if the following data has the PK - 987 during the first insert. During the subsequent runs, the PK can be 1587, 2698, etc.

{
   "dimensionHeaders":[
      {
         "name":"date"
      },
      {
         "name":"deviceCategory"
      },
      {
         "name":"eventName"
      }
   ],
   "metricHeaders":[
      {
         "name":"eventCount",
         "type":"TYPE_INTEGER"
      },
      {
         "name":"sessions",
         "type":"TYPE_INTEGER"
      },
      {
         "name":"totalUsers",
         "type":"TYPE_INTEGER"
      }
   ],
   "rows":[
      {
         "dimensionValues":[
            {
               "value":"20230912"
            },
            {
               "value":"desktop"
            },
            {
               "value":"10% scroll"
            }
         ],
         "metricValues":[
            {
               "value":"1"
            },
            {
               "value":"1"
            },
            {
               "value":"1"
            }
         ]
      }
   ],
   "rowCount":22479,
   "metadata":{
      "currencyCode":"USD",
      "timeZone":"America/Chicago"
   },
  "kind":"anaa lyticsData#runReport"
}

I wanted to know, if there's way to request a unique identifier for each row in the response data set.

I have checked GA documents, StackOverflow, etc and found nothing helpful.


Solution

  • Not natively, anyway you can add it to the array later, before writing to the database.

    Example (see index key/value after run code snippet below):

    var arr_obj = [
       {
         name: 'Pippo',
         order: 'books',
       },
       {
         name: 'Pluto',
         order: 'brooms',
       },
       {
        name: 'Donald Duck',
        order: 'food',
       }
    ];
    
    var index = 1;
    arr_obj.forEach(d => {
      d['index'] = index;
      index += 1;
    });
    console.log(arr_obj)