OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

"@google-analytics/data": "^4.x.x" add metricAggregations array. Throw error "Cannot serialize object to proto3 JSON since its .$type is unknown."

  • Thread starter Thread starter stevetanus
  • Start date Start date
S

stevetanus

Guest
I'm using "@google-analytics/data": "^4.7.0" to request ga report, in the request array, I add some payloads which can be mocked by GA4 query explorer, payloads are like dateRanges, metrics, dimensions, metricAggregations, dimensionFilter ..., and called the function batchRunReports to get back the report.

But the error 'Cannot serialize object to proto3 JSON since its .$type is unknown. Use Type.fromObject(obj) before calling toProto3JSON.' occurs when the script run through the metricAggregations array, I don't know how come it happened and how to fix it.

The example code:

Code:
async function () {
  const { BetaAnalyticsDataClient } = require('@google-analytics/data')
  const propertyId = 'propertyId'
  const ga_auth = {
    xxx
  }

  const analyticsDataClient = new BetaAnalyticsDataClient({
    projectId: ga_auth.project_id,
    credentials: ga_auth,
    fallback: true
  })

  const requests = [
      {
        dateRanges: [{ startDate: '2024-05-30', endDate: 'today' }],
        metrics: [
          { name: 'totalUsers' },
          { name: 'screenPageViews' },
          { name: 'averageSessionDuration' },
          { name: 'screenPageViewsPerSession' },
          { name: 'bounceRate' }
        ],
        dimensions: [],
        metricAggregations: ["Total"],
        property: 'properties/propertyId',
        dimensionFilter: {
          andGroup: { expressions: [{ filter: { fieldName: 'customEvent:product_id', stringFilter: {   value: 'some_product_id' } } }] }
      }
    }
  ]

  const [response] = await analyticsDataClient.batchRunReports({
    property: `properties/${propertyId}`,
    requests
  })
  console.log(response)
  return process.exit(0)
})()

And also, I try to log out the obj passed into the toProto3Json function, I get

Code:
{ obj: Metric { name: 'totalUsers' } }
{ obj: Metric { name: 'screenPageViews' } }
{ obj: Metric { name: 'averageSessionDuration' } }
{ obj: Metric { name: 'screenPageViewsPerSession' } }
{ obj: Metric { name: 'bounceRate' } }
{ obj: DateRange { startDate: '2024-05-30', endDate: 'today' } }
{ obj: 1 }

It looks like the metricAggregations array didn't be transferred correctly, does someone know how to fix the problem? Thanks you for your reading.

I am expecting getting back correct report with totals array. When I use package "@google-analytics/data": "^3.3.0",, it got me back the correct result, but I think it's because in this version, the script didn't run the toProto3JSON function.

<p>I'm using <code>"@google-analytics/data": "^4.7.0"</code> to request ga report, in the request array, I add some payloads which can be mocked by <a href="https://ga-dev-tools.google/ga4/query-explorer/" rel="nofollow noreferrer">GA4 query explorer</a>, payloads are like <code>dateRanges</code>, <code>metrics</code>, <code>dimensions</code>, <code>metricAggregations</code>, <code>dimensionFilter</code> ..., and called the function <code>batchRunReports</code> to get back the report.</p>
<p>But the error <code>'Cannot serialize object to proto3 JSON since its .$type is unknown. Use Type.fromObject(obj) before calling toProto3JSON.'</code> occurs when the script run through the <code>metricAggregations</code> array, I don't know how come it happened and how to fix it.</p>
<p>The example code:</p>
<pre><code>async function () {
const { BetaAnalyticsDataClient } = require('@google-analytics/data')
const propertyId = 'propertyId'
const ga_auth = {
xxx
}

const analyticsDataClient = new BetaAnalyticsDataClient({
projectId: ga_auth.project_id,
credentials: ga_auth,
fallback: true
})

const requests = [
{
dateRanges: [{ startDate: '2024-05-30', endDate: 'today' }],
metrics: [
{ name: 'totalUsers' },
{ name: 'screenPageViews' },
{ name: 'averageSessionDuration' },
{ name: 'screenPageViewsPerSession' },
{ name: 'bounceRate' }
],
dimensions: [],
metricAggregations: ["Total"],
property: 'properties/propertyId',
dimensionFilter: {
andGroup: { expressions: [{ filter: { fieldName: 'customEvent:product_id', stringFilter: { value: 'some_product_id' } } }] }
}
}
]

const [response] = await analyticsDataClient.batchRunReports({
property: `properties/${propertyId}`,
requests
})
console.log(response)
return process.exit(0)
})()
</code></pre>
<p>And also, I try to log out the <code>obj</code> passed into the <code>toProto3Json</code> function, I get</p>
<pre><code>{ obj: Metric { name: 'totalUsers' } }
{ obj: Metric { name: 'screenPageViews' } }
{ obj: Metric { name: 'averageSessionDuration' } }
{ obj: Metric { name: 'screenPageViewsPerSession' } }
{ obj: Metric { name: 'bounceRate' } }
{ obj: DateRange { startDate: '2024-05-30', endDate: 'today' } }
{ obj: 1 }
</code></pre>
<p>It looks like the <code>metricAggregations</code> array didn't be transferred correctly, does someone know how to fix the problem? Thanks you for your reading.</p>
<p>I am expecting getting back correct report with <code>totals</code> array. When I use package <code>"@google-analytics/data": "^3.3.0",</code>, it got me back the correct result, but I think it's because in this version, the script didn't run the <code>toProto3JSON</code> function.</p>
 

Latest posts

A
Replies
0
Views
1
Anthonin Chaboteaux
A
Top