NodeJS 从Google Analytic 4(GA4)获取数据以创建粒度为每日的图表时缺少日期数据?

clj7thdc  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(126)

我刚开始使用Google Analytics 4,我有一个任务是使用自定义维度创建图表报告。从API获取数据时遇到问题。从GA4获取数据时缺少日期数据。我想使用此数据构建图表

const {
      propertyId,
      startDate,
      endDate,
    } = req.query;

    // Request report data.
    const analyticsDataClient = await getAnalyticsDataClient(getActiveShop(req));
    const response = await analyticsDataClient.runPivotReport({
      property: `properties/${propertyId}`,
      dimensions: [
        { name: "date" },
        { name: "CUSTOM_DIMENSION_EVENT" },
        { name: "CUSTOM_DIMENSION_EVENT" },
        { name: "CUSTOM_DIMENSION_EVENT" },
      ],
      metrics: [
        { name: "sessions" },
        { name: "activeUsers" },
        { name: "CUSTOM_METRIC_EVENT" },
        { name: "CUSTOM_METRIC_EVENT" },
      ],
      dateRanges: [{ startDate: 2022-12-06, endDate: 2023-01-03 }], // Dateranges
      pivots: [
        {
          fieldNames: ["date"],
          limit: 30,
        },
        {
          fieldNames: ["CUSTOM_DIMENSION_EVENT"],
          limit: 5,
        },
        {
          fieldNames: ["CUSTOM_DIMENSION_EVENT"],
          limit: 5,
        },
        {
          fieldNames: ["CUSTOM_DIMENSION_EVENT"],
          limit: 5,
        },
      ],

      dimensionFilter: {
        filter: {
          fieldName: "CUSTOM_METRIC_EVENT", // id
          stringFilter: {
            matchType: "EXACT",
            value: "CUSTOM_METRIC_EVENT_ID",
          },
        },
      },

      keepEmptyRows: true,
    });

响应GA4 API

{
  "response": [
    {
      "pivotHeaders": [
        {
          "pivotDimensionHeaders": [
            {
              "dimensionValues": [
                {
                  "value": "20221227", // return this date only
                  "oneValue": "value"
                }
              ]
            }
          ],
          "rowCount": 1
        },
        {
          "pivotDimensionHeaders": [
            {
              "dimensionValues": [
                {
                  "value": "885c7b0d-bc65-47be-b7df-871947bc5de4",
                  "oneValue": "value"
                }
              ]
            }
          ],
          "rowCount": 1
        },
        {
          "pivotDimensionHeaders": [
            {
              "dimensionValues": [
                {
                  "value": "New Page For Test GA4",
                  "oneValue": "value"
                }
              ]
            }
          ],
          "rowCount": 1
        },
        {
          "pivotDimensionHeaders": [
            {
              "dimensionValues": [
                {
                  "value": "index",
                  "oneValue": "value"
                }
              ]
            }
          ],
          "rowCount": 1
        }
      ],
      "dimensionHeaders": [
        {
          "name": "date"
        },
        {
          "name": "customEvent:page_id"
        },
        {
          "name": "customEvent:page_name"
        },
        {
          "name": "customEvent:page_type"
        }
      ],
      "metricHeaders": [
        {
          "name": "sessions",
          "type": "TYPE_INTEGER"
        },
        {
          "name": "activeUsers",
          "type": "TYPE_INTEGER"
        },
        {
          "name": "customEvent:revenue",
          "type": "TYPE_CURRENCY"
        }
      ],
      "rows": [
        {
          "dimensionValues": [
            {
              "value": "20221227",
              "oneValue": "value"
            },
            {
              "value": "885c7b0d-bc65-47be-b7df-871947bc5de4",
              "oneValue": "value"
            },
            {
              "value": "New Page For Test GA4",
              "oneValue": "value"
            },
            {
              "value": "index",
              "oneValue": "value"
            }
          ],
          "metricValues": [
            {
              "value": "7",
              "oneValue": "value"
            },
            {
              "value": "7",
              "oneValue": "value"
            },
            {
              "value": "0",
              "oneValue": "value"
            },
            {
              "value": "8",
              "oneValue": "value"
            }
          ]
        }
      ],
      "aggregates": [],
      "metadata": {
        "dataLossFromOtherRow": false,
        "schemaRestrictionResponse": {
          "activeMetricRestrictions": []
        },
        "_schemaRestrictionResponse": "schemaRestrictionResponse",
        "_currencyCode": "currencyCode",
        "timeZone": "America/Los_Angeles",
        "_timeZone": "timeZone"
      },
      "propertyQuota": null,
      "kind": "analyticsData#runPivotReport"
    },
    null,
    null
  ]
}

虽然我设置了dateRanges { startDate: 2022-12-06, endDate: 2023-01-03 },但它只返回20221227给我,这是什么问题?

ffscu2ro

ffscu2ro1#

如果选中文档中的runPivotReport,您将发现RunPivotReportResponse的响应主体
不包含日期,API可能会假设您知道您请求的日期。

{
  "pivotHeaders": [
    {
      object (PivotHeader)
    }
  ],
  "dimensionHeaders": [
    {
      object (DimensionHeader)
    }
  ],
  "metricHeaders": [
    {
      object (MetricHeader)
    }
  ],
  "rows": [
    {
      object (Row)
    }
  ],
  "aggregates": [
    {
      object (Row)
    }
  ],
  "metadata": {
    object (ResponseMetaData)
  },
  "propertyQuota": {
    object (PropertyQuota)
  },
  "kind": string
}

相关问题