分析データサービス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
アナリティクス データ サービスを使用すると、Apps Script で Google アナリティクス Data API v1 を使用できます。この API を使用すると、Google アナリティクス ユーザーは Google アナリティクス 4(GA4)のレポートデータにプログラムでアクセスできます。
リファレンス
このサービスの詳細については、Google アナリティクス Data API v1 のリファレンス ドキュメントをご覧ください。
Apps Script のすべての高度なサービスと同様に、AnalyticsData サービスでは、公開 API と同じオブジェクト、メソッド、パラメータを使用します。詳細については、メソッド シグネチャの決定方法をご覧ください。
問題を報告したり、その他のサポートを見つけたりするには、Google アナリティクス 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```"]]