سرویس داده تجزیه و تحلیل
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
سرویس Analytics Data به شما امکان می دهد از Google Analytics Data API v1 در Apps Script استفاده کنید. این API به کاربران Google Analytics دسترسی برنامهای به دادههای گزارش Google Analytics 4 (GA4) میدهد.
مرجع
برای اطلاعات دقیق درباره این سرویس، به مستندات مرجع Google Analytics Data API v1 مراجعه کنید.
مانند همه سرویس های پیشرفته در Apps Script، سرویس AnalyticsData از همان اشیا، روش ها و پارامترهای API عمومی استفاده می کند. برای اطلاعات بیشتر، نحوه تعیین امضای روش را ببینید.
برای گزارش مشکلات و یافتن پشتیبانی دیگر، به صفحه پشتیبانی Google Analytics Data API v1 مراجعه کنید.
کد نمونه
یک گزارش اجرا کنید
نمونه گزارشی را برای بازیابی تعداد کاربران فعال بر اساس شهر اجرا می کند و نتایج را در یک صفحه گسترده جدید ذخیره می کند.
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\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```"]]