rsbuild-plugin-google-analytics

February 7, 2026 ยท View on GitHub

An Rsbuild plugin to setup Google Analytics in your website.

npm version license downloads

Usage

Install:

npm add rsbuild-plugin-google-analytics -D

Add plugin to your rsbuild.config.ts:

// rsbuild.config.ts
import { pluginGoogleAnalytics } from 'rsbuild-plugin-google-analytics';

export default {
  plugins: [
    pluginGoogleAnalytics({
      // replace this with your Google tag ID
      // see: https://support.google.com/analytics/answer/9539598?hl=en
      id: 'G-xxxxxxxxxx',
    }),
  ],
};

Options

Here are the available options:

NameTypeDescriptionDefaults
idstringGoogle tag IDundefined
enablebooleanWhether to enable Google Analyticstrue

TypeScript support

If you experience this error on UI side window.gtag is not a function, you may need to add types for gtag by installing this package:

npm install --save-dev @types/gtag.js

and you also have to add it to the types in your tsconfig.json:

{
  "compilerOptions": {
    "types": ["gtag.js"]
  }
}

Then you won't experience any TS errors with code like this:

if (isProd()) {
  gtag('event', 'exception', { description: err.message });
}

License

MIT.