Dịch vụ dữ liệu của Analytics
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Dịch vụ Dữ liệu Analytics cho phép bạn sử dụng Google Analytics Data API phiên bản 1 trong Apps Script. API này cung cấp cho người dùng Google Analytics quyền truy cập theo phương thức có lập trình vào dữ liệu báo cáo của Google Analytics 4 (GA4).
Tài liệu tham khảo
Để biết thông tin chi tiết về dịch vụ này, hãy xem tài liệu tham khảo về Google Analytics Data API phiên bản 1.
Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ AnalyticsData sử dụng cùng các đối tượng, phương thức và tham số như API công khai. Để biết thêm thông tin, hãy xem bài viết Cách xác định chữ ký phương thức.
Để báo cáo vấn đề và tìm thông tin hỗ trợ khác, hãy xem trang hỗ trợ Google Analytics Data API phiên bản 1.
Mã mẫu
Chạy báo cáo
Mẫu này chạy một báo cáo để truy xuất số lượng người dùng đang hoạt động theo thành phố và lưu trữ kết quả trong một bảng tính mới.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eThe Analytics Data service enables programmatic access to Google Analytics 4 (GA4) report data within Apps Script using the Google Analytics Data API v1.\u003c/p\u003e\n"],["\u003cp\u003eIt's an advanced service requiring prior enabling before use and utilizes the same structure as the public API.\u003c/p\u003e\n"],["\u003cp\u003eComprehensive documentation, support, and sample code are available for guidance on utilizing this service effectively, including a sample for running reports to retrieve data and store it in a spreadsheet.\u003c/p\u003e\n"]]],[],null,["# Analytics Data Service\n\nThe Analytics Data service allows you to use the [Google Analytics Data API v1](/analytics/devguides/reporting/data/v1)\nin Apps Script. This API gives Google Analytics users programmatic access to\nGoogle Analytics 4 (GA4) report data.\n| **Note:** This is an advanced service that must be [enabled before use](/apps-script/guides/services/advanced).\n\nReference\n---------\n\nFor detailed information on this service, see the [Google Analytics Data API v1 reference documentation](/analytics/devguides/reporting/data/v1/rest).\n\nLike all advanced services in Apps Script, the AnalyticsData service uses the\nsame objects, methods, and parameters as the public API. For more information,\nsee [How method signatures are determined](/apps-script/guides/services/advanced#how_method_signatures_are_determined).\n\nTo report issues and find other support, see the\n[Google Analytics Data API v1 support page](/analytics/devguides/reporting/data/v1/help).\n\nSample code\n-----------\n\n### Run a report\n\nThe sample runs a report to retrieve the active users count by city\nand stores the results in a new spreadsheet. \nadvanced/analyticsData.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/analyticsData.gs) \n\n```javascript\n/**\n * Runs a report of a Google Analytics 4 property ID. Creates a sheet with the\n * report.\n */\nfunction runReport() {\n /**\n * TODO(developer): Uncomment this variable and replace with your\n * Google Analytics 4 property ID before running the sample.\n */\n const propertyId = 'YOUR-GA4-PROPERTY-ID';\n\n try {\n const metric = AnalyticsData.newMetric();\n metric.name = 'activeUsers';\n\n const dimension = AnalyticsData.newDimension();\n dimension.name = 'city';\n\n const dateRange = AnalyticsData.newDateRange();\n dateRange.startDate = '2020-03-31';\n dateRange.endDate = 'today';\n\n const request = AnalyticsData.newRunReportRequest();\n request.dimensions = [dimension];\n request.metrics = [metric];\n request.dateRanges = dateRange;\n\n const report = AnalyticsData.Properties.runReport(request,\n 'properties/' + propertyId);\n if (!report.rows) {\n console.log('No rows returned.');\n return;\n }\n\n const spreadsheet = SpreadsheetApp.create('Google Analytics Report');\n const sheet = spreadsheet.getActiveSheet();\n\n // Append the headers.\n const dimensionHeaders = report.dimensionHeaders.map(\n (dimensionHeader) =\u003e {\n return dimensionHeader.name;\n });\n const metricHeaders = report.metricHeaders.map(\n (metricHeader) =\u003e {\n return metricHeader.name;\n });\n const headers = [...dimensionHeaders, ...metricHeaders];\n\n sheet.appendRow(headers);\n\n // Append the results.\n const rows = report.rows.map((row) =\u003e {\n const dimensionValues = row.dimensionValues.map(\n (dimensionValue) =\u003e {\n return dimensionValue.value;\n });\n const metricValues = row.metricValues.map(\n (metricValues) =\u003e {\n return metricValues.value;\n });\n return [...dimensionValues, ...metricValues];\n });\n\n sheet.getRange(2, 1, report.rows.length, headers.length)\n .setValues(rows);\n\n console.log('Report spreadsheet created: %s',\n spreadsheet.getUrl());\n } catch (e) {\n // TODO (Developer) - Handle exception\n console.log('Failed with error: %s', e.error);\n }\n}\n```"]]