บริการข้อมูล Analytics
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
บริการข้อมูล Analytics ช่วยให้คุณใช้ Data API v1 ของ Google Analytics
ใน Apps Script ได้ API นี้ช่วยให้ผู้ใช้ Google Analytics เข้าถึงข้อมูลรายงานของ Google Analytics 4 (GA4) แบบเป็นโปรแกรมได้
ข้อมูลอ้างอิง
ดูข้อมูลโดยละเอียดเกี่ยวกับบริการนี้ได้ที่เอกสารอ้างอิงของ Google Analytics Data API v1
เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script บริการ AnalyticsData จะใช้วัตถุ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ
ดูข้อมูลเพิ่มเติมได้ที่วิธีกำหนดลายเซ็นของเมธอด
หากต้องการรายงานปัญหาและรับการสนับสนุนอื่นๆ โปรดดู
หน้าการสนับสนุน Google Analytics Data API v1
โค้ดตัวอย่าง
เรียกใช้รายงาน
ตัวอย่างจะเรียกใช้รายงานเพื่อดึงจํานวนผู้ใช้ที่ใช้งานอยู่ตามเมือง
และจัดเก็บผลลัพธ์ในสเปรดชีตใหม่
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 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```"]]