Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@google-analytics/data

googleapis251.8kApache-2.05.1.0TypeScript support: included

Data client for Node.js

google apis client, google api client, google apis, google api, google, google cloud platform, google cloud, cloud, google data, data, alpha analytics data

readme

Google Cloud Platform logo

Google Analytics Data: Node.js Client

release level npm version

Data client for Node.js

A comprehensive list of changes in each version may be found in the CHANGELOG.

Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.

Table of contents:

Quickstart

Before you begin

  1. Select or create a Cloud Platform project.
  2. Enable the Google Analytics Data API.
  3. Set up authentication with a service account so you can access the API from your local workstation.

Installing the client library

npm install @google-analytics/data

Using the client library

/**
 * TODO(developer): Uncomment this variable and replace with your
 *   Google Analytics 4 property ID before running the sample.
 */
// propertyId = 'YOUR-GA4-PROPERTY-ID';

// Imports the Google Analytics Data API client library.
const {BetaAnalyticsDataClient} = require('@google-analytics/data');

// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
const analyticsDataClient = new BetaAnalyticsDataClient();

// Runs a simple report.
async function runReport() {
  const [response] = await analyticsDataClient.runReport({
    property: `properties/${propertyId}`,
    dateRanges: [
      {
        startDate: '2020-03-31',
        endDate: 'today',
      },
    ],
    dimensions: [
      {
        name: 'city',
      },
    ],
    metrics: [
      {
        name: 'activeUsers',
      },
    ],
  });

  console.log('Report result:');
  response.rows.forEach(row => {
    console.log(row.dimensionValues[0], row.metricValues[0]);
  });
}

runReport();

Samples

Samples are in the samples/ directory. Each sample's README.md has instructions for running its sample.

Sample Source Code Try it
Alpha_analytics_data.create_audience_list source code Open in Cloud Shell
Alpha_analytics_data.create_recurring_audience_list source code Open in Cloud Shell
Alpha_analytics_data.create_report_task source code Open in Cloud Shell
Alpha_analytics_data.get_audience_list source code Open in Cloud Shell
Alpha_analytics_data.get_property_quotas_snapshot source code Open in Cloud Shell
Alpha_analytics_data.get_recurring_audience_list source code Open in Cloud Shell
Alpha_analytics_data.get_report_task source code Open in Cloud Shell
Alpha_analytics_data.list_audience_lists source code Open in Cloud Shell
Alpha_analytics_data.list_recurring_audience_lists source code Open in Cloud Shell
Alpha_analytics_data.list_report_tasks source code Open in Cloud Shell
Alpha_analytics_data.query_audience_list source code Open in Cloud Shell
Alpha_analytics_data.query_report_task source code Open in Cloud Shell
Alpha_analytics_data.run_funnel_report source code Open in Cloud Shell
Alpha_analytics_data.sheet_export_audience_list source code Open in Cloud Shell
Beta_analytics_data.batch_run_pivot_reports source code Open in Cloud Shell
Beta_analytics_data.batch_run_reports source code Open in Cloud Shell
Beta_analytics_data.check_compatibility source code Open in Cloud Shell
Beta_analytics_data.create_audience_export source code Open in Cloud Shell
Beta_analytics_data.get_audience_export source code Open in Cloud Shell
Beta_analytics_data.get_metadata source code Open in Cloud Shell
Beta_analytics_data.list_audience_exports source code Open in Cloud Shell
Beta_analytics_data.query_audience_export source code Open in Cloud Shell
Beta_analytics_data.run_pivot_report source code Open in Cloud Shell
Beta_analytics_data.run_realtime_report source code Open in Cloud Shell
Beta_analytics_data.run_report source code Open in Cloud Shell
Quickstart source code Open in Cloud Shell

The Google Analytics Data Node.js Client API Reference documentation also contains samples.

Supported Node.js Versions

Our client libraries follow the Node.js release schedule. Libraries are compatible with all current active and maintenance versions of Node.js. If you are using an end-of-life version of Node.js, we recommend that you update as soon as possible to an actively supported LTS version.

Google's client libraries support legacy versions of Node.js runtimes on a best-efforts basis with the following warnings:

  • Legacy versions are not tested in continuous integration.
  • Some security patches and features cannot be backported.
  • Dependencies cannot be kept up-to-date.

Client libraries targeting some end-of-life versions of Node.js are available, and can be installed through npm dist-tags. The dist-tags follow the naming convention legacy-(version). For example, npm install @google-analytics/data@legacy-8 installs client libraries for versions compatible with Node.js 8.

Versioning

This library follows Semantic Versioning.

This library is considered to be in preview. This means it is still a work-in-progress and under active development. Any release is subject to backwards-incompatible changes at any time.

More Information: Google Cloud Platform Launch Stages

Contributing

Contributions welcome! See the Contributing Guide.

Please note that this README.md, the samples/README.md, and a variety of configuration files in this repository (including .nycrc and tsconfig.json) are generated from a central template. To edit one of these files, make an edit to its templates in directory.

License

Apache Version 2.0

See LICENSE

changelog

Changelog

5.1.0 (2025-03-19)

Features

  • [Many APIs] await/catch promises, and update listOperationsAsync return type (#6182) (c41ff07)

5.0.0 (2025-03-18)

⚠ BREAKING CHANGES

  • upgrade to Node 18 (#6096)

Features

  • [Many APIs] add request/response debug logging to gapics, update templates to gax 5 (54a73fe)

Miscellaneous Chores

4.12.1 (2025-02-12)

Bug Fixes

  • [Many APIs] finalize fixing typings for headers in generator (#6011) (ee865ff)

4.12.0 (2024-12-18)

Features

  • [analytics-data] add the EmptyFilter type to the Data API v1alpha (#5845) (1bcd241)

4.11.0 (2024-11-21)

Features

  • [analytics-data] add EmptyFilter type to the Data API v1beta (#5831) (ced8054)

4.10.0 (2024-10-30)

Features

  • [analytics-data] add SamplingLevel type to Data API v1alpha (#5759) (96bb1b5)

4.9.0 (2024-09-24)

Features

  • [analytics-data] add GetPropertyQuotasSnapshot method to the Data API v1alpha (#5692) (a143a79)

4.8.0 (2024-08-09)

Features

  • [analytics-data] add the comparisons field to the Metadata resource (#5600) (e77b934)

4.7.0 (2024-05-21)

Features

  • [Many APIs] update Nodejs generator to send API versions in headers for GAPICs (#5351) (01f48fc)
  • [Many APIs] update Nodejs generator to send API versions in headers for GAPICs (#5354) (a9784ed)

4.6.0 (2024-05-08)

Features

  • Add ReportTask, Metric, OrderBy, Cohort, CohortsRange, CohortReportSettings, ResponseMetaData, MetricAggregation, RestrictedMetricType types to the Data API v1alpha (#5318) (76ae561)

4.5.0 (2024-04-16)

Features

  • [Many APIs] support GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable (#5185) (cc54e98)

4.4.0 (2024-02-09)

Features

  • Trusted Private Cloud support, use the universeDomain parameter (#5022) (b6498d8)

4.3.0 (2023-12-11)

Features

  • [analytics-data] add CreateAudienceExport, QueryAudienceExport, GetAudienceExport, ListAudienceExports methods to the Data API v1 beta (#4878) (1f9dc5a)

4.2.0 (2023-11-30)

Features

  • [analytics-data] add CreateRecurringAudienceList, GetRecurringAudienceList, ListRecurringAudienceLists methods to the Data API v1 alpha (#4851) (400c893)

4.1.0 (2023-11-16)

Features

  • [analytics-data] add the SheetExportAudienceList method to the Data API v1 alpha (#4804) (279e77c)

4.0.1 (2023-09-06)

Bug Fixes

  • [Many APIs] simplify logic for HTTP/1.1 REST fallback option (#4583) (c3ddba8)
  • deps: Update dependency google-auth-library to v9 (#4560) (e5099b7)

4.0.0 (2023-08-06)

⚠ BREAKING CHANGES

  • migrate to Node 14 (#4443)

Bug Fixes

Miscellaneous Chores

3.3.0 (2023-07-11)

Features

  • [analytics-data] add CreateAudienceList, QueryAudienceList,GetAudienceList,ListAudienceLists methods to the Data API v1 alpha (#4409) (b81d695)

3.2.2 (2023-04-13)

Bug Fixes

3.2.1 (2023-02-15)

Bug Fixes

  • [Many APIs] changing format of the jsdoc links (#3985) (fb2a6fd)

3.2.0 (2023-01-28)

Features

3.1.2 (2022-12-16)

Bug Fixes

3.1.1 (2022-11-10)

Bug Fixes

3.1.0 (2022-10-13)

Features

  • Add subject_to_thresholding field to ResponseMetadata type (#265) (410f0e0)

3.0.3 (2022-09-22)

Bug Fixes

  • Preserve default values in x-goog-request-params header (#237) (ae646a5)

3.0.2 (2022-09-01)

Bug Fixes

  • Allow passing gax instance to client constructor (#232) (df75612)
  • Do not import the whole google-gax from proto JS (#1553) (#231) (635cddf)

3.0.1 (2022-08-23)

Bug Fixes

3.0.0 (2022-06-27)

⚠ BREAKING CHANGES

  • rename the funnel_filter field of the FunnelFilterExpression type to funnel_field_filter
  • rename the type FunnelFilter to FunnelFieldFilter (#218)
  • update library to use Node 12 (#213)

Features

  • add runFunnelReport method to the Alpha version of the Data API (#208) (2390a92)
  • support regapic LRO (#219) (547ecd7)

Bug Fixes

  • rename the funnel_filter field of the FunnelFilterExpression type to funnel_field_filter (9ff9521)
  • rename the type FunnelFilter to FunnelFieldFilter (#218) (9ff9521)

Build System

2.8.0 (2021-11-08)

Features

  • add the schema_restriction_response field to the ResponseMetaData type that contains the schema restrictions actively enforced in creating a report feat: add the currency_code, time_zone fields to the ResponseMetaData type feat: add the... (#154) (37f6077)

2.7.0 (2021-09-01)

Features

  • add CheckCompatibility method to the API (#141) (62c787f)

2.6.0 (2021-08-23)

Features

  • turns on self-signed JWT feature flag (#138) (c7df6d6)

2.5.3 (2021-08-17)

Bug Fixes

2.5.2 (2021-08-09)

Bug Fixes

2.5.1 (2021-07-16)

Bug Fixes

  • Updating WORKSPACE files to use the newest version of the Typescript generator. (#126) (7272509)

2.5.0 (2021-07-12)

Features

  • add minute_ranges field to RunRealtimeReportRequest object which can be used to specify the time range (in minutes) for realtime report queries docs: document the increase from 8 to 9 of the number of allowed dimensions in a report query (#123) (975776b)

Bug Fixes

2.4.3 (2021-06-29)

Bug Fixes

2.4.2 (2021-05-25)

Bug Fixes

  • GoogleAdsError missing using generator version after 1.3.0 (#110) (816580a)

2.4.1 (2021-05-19)

Bug Fixes

2.4.0 (2021-04-02)

Features

  • add kind field which is used to distinguish between response types feat: add potentially_thresholded_requests_per_hour field to PropertyQuota (#85) (00b1d04)

2.3.0 (2021-03-11)

Features

  • add v1beta surface (issues fixed) (#69) (44005ca)

2.2.1 (2021-03-04)

Bug Fixes

  • deps: update dependency open to v8 (#62) (3724c67)
  • remove v1beta1 to nodejs-analytics-data (73aca08)

2.2.0 (2021-03-02)

Features

  • add v1beta1 to nodejs-analytics-data (#60) (ffe0395)

Bug Fixes

  • deps: update dependency google-auth-library to v7 (#58) (77002d8)

2.1.0 (2021-01-09)

Features

2.0.0 (2020-12-28)

⚠ BREAKING CHANGES

  • GetUniversalMetadata method removed from the API, GetMetadata method should be used instead feat: reporting requests support longer date ranges (#43)

Bug Fixes

  • GetUniversalMetadata method removed from the API, GetMetadata method should be used instead feat: reporting requests support longer date ranges (#43) (661ee0e)

1.1.0 (2020-11-11)

Features

  • added RunRealtimeReport method that returns a customized report of realtime event data for a GA4 property docs: minor documentation updates (#29) (58da0fe)

1.0.2 (2020-11-07)

Bug Fixes

  • do not modify options object, use defaultScopes (#30) (f5c9f64)

1.0.1 (2020-11-03)

Bug Fixes

  • deps: update dependency @google-cloud/billing to v2 (#7) (de83230)

1.0.0 (2020-09-02)

⚠ BREAKING CHANGES

  • generate first stub of library (#1)

Features

  • generate first stub of library (#1) (5514d1c)