خدمة بيانات "إحصاءات Google"
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تتيح لك خدمة "بيانات إحصاءات Google" استخدام الإصدار 1 من Google Analytics Data API في Apps Script. تتيح واجهة برمجة التطبيقات هذه لمستخدِمي "إحصاءات Google" الوصول آليًا إلى بيانات التقارير في "إحصاءات Google 4" (GA4).
مراجع
للحصول على معلومات تفصيلية عن هذه الخدمة، يُرجى الاطّلاع على المستندات المرجعية للإصدار 1 من Google Analytics Data API.
مثل جميع الخدمات المتقدّمة في Apps Script، تستخدم خدمة AnalyticsData الكائنات والطُرق والمعلَمات نفسها المستخدَمة في واجهة برمجة التطبيقات العامة. لمزيد من المعلومات،
راجِع كيفية تحديد تواقيع الطرق.
للإبلاغ عن مشاكل والعثور على معلومات دعم أخرى، يُرجى الاطّلاع على صفحة الدعم الخاصة بالإصدار 1 من Google Analytics Data API.
نموذج التعليمات البرمجية
تنفيذ تقرير
ينفّذ النموذج تقريرًا لاسترداد عدد المستخدمين النشطين حسب المدينة
ويخزّن النتائج في جدول بيانات جديد.
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\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```"]]