当 API 没有记录时,Google GA4 batchRunReports 抛出 500内部服务器错误

问题描述

https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/TopLevel/batchRunReports

请求:

{
"entity": {
    "propertyId": "XXXXXXXX"
},"requests": [
    {
        "entity": {
            "propertyId": "XXXXXXXX"
        },"dimensions": [
            {
                "name": "date"
            },{
                "name": "dateHour"
            },{
                "name": "firstUserCampaignName"
            }
        ],"metrics": [
            {
                "name": "sessions"
            }
        ],"dateranges": [
            {
                "startDate": "2021-04-06","endDate": "2021-04-07"
            }
        ],"metricAggregations": [
            "TOTAL"
        ],"dimensionFilter": {
            "andGroup": {
                "expressions": [
                    {
                        "filter": {
                            "fieldName": "medium","stringFilter": {
                                "matchType": "EXACT","value": "Test"
                            }
                        }
                    }
                ]
            }
        },"orderBys": [
            {
                "desc": true,"metric": {
                    "metricName": "sessions"
                }
            },{
                "desc": false,"dimension": {
                    "dimensionName": "dateHour"
                }
            }
        ],"keepEmptyRows": true
    }
]

}

回复

{
"error": {
    "code": 500,"message": "Internal error encountered.","status": "INTERNAL"
}

}

但是如果从请求中删除以下属性

 "metricAggregations": [
            "TOTAL"
        ],

我可以在没有行的情况下看到以下响应:

{
"reports": [
    {
        "metricHeaders": [
            {
                "name": "sessions","type": "TYPE_INTEGER"
            }
        ],"Metadata": {},"dimensionHeaders": [
            {
                "name": "date"
            },"kind": "analyticsData#runReport"
    }
],"kind": "analyticsData#batchRunReports"

}

知道如何在这种情况下防止 500 内部服务器错误吗?

错误阻止了 Google Api 调用一个小时。

解决方法

Furqan,数据 API 似乎存在问题,如果生成的报告为空,则使用 struct ContentView: View { @State var activeView: Int = 0 @State var activeNavigationLink: Int = 0 //<-- Here let items = ["View1","View2"] func navigationLinkBinding(id: Int) -> Binding<Bool> { //<-- Here .init { () -> Bool in activeNavigationLink == id } set: { (newValue) in if newValue { activeNavigationLink = id } else { activeNavigationLink = 0 } } } var body: some View { TabView(selection: Binding<Int> ( get: { activeView },set: { activeView = $0 activeNavigationLink = 0 //<-- Here })) { NavigationView { HStack { VStack { NavigationLink( destination: Text("View 1"),isActive: navigationLinkBinding(id: 1),label: { //<-- Here Rectangle() .fill(Color.red) .cornerRadius(12) .frame(width: 70,height: 70) }).isDetailLink(false) Text(items[0]) } VStack{ NavigationLink( destination: Text("View 2"),isActive: navigationLinkBinding(id: 2),height: 70) }).isDetailLink(false) Text(items[1]) } }.navigationTitle("") .navigationBarHidden(true) } .tabItem { Image(systemName: "a.circle") Text("Main") } .tag(0) Text("View 3") .padding() .tabItem { Image(systemName: "b.circle") Text("View 3") } .tag(1) } } } 的调用将失败。同时,要解决此错误,您可以修改查询,使生成的报告包含多于 0 行。