4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Nuxt.jsの@nuxtjs/google-analyticsをGA4に移行してみた

Last updated at Posted at 2023-02-10

Nuxt.js(2系)のプロジェクトで@nuxtjs/google-analyticsのモジュールを使って既存のGoogleAnalyticsを利用していました。
この度GA4に移行したので手順をメモして起きます。

モジュールの変更

今まで使用していた@nuxtjs/google-analyticsは削除します。

yarn remove @nuxtjs/google-analytics

新たに@nuxtjs/google-gtagを追加します。

yarn add @nuxtjs/google-gtag

nuxt.config.jsの変更

モジュールの変更に伴いnuxt.config.jsの設定を変更します。

  buildModules: [
    // '@nuxtjs/google-analytics', 消去
    '@nuxtjs/google-gtag' // 追加
  ],
  /* 追加 */
  'google-gtag': {
    id: process.env.GOOGLE_ANALYTICS_ID,
    debug: true
  },
  /*
  古いアナリティクスの設定の為消去
  publicRuntimeConfig: {
    googleAnalytics: {
      id: process.env.GOOGLE_ANALYTICS_ID
    }
  },
 */

idは環境変数で指定していますが、直接指定しても問題ありません。
オプションの項目などはドキュメントで説明があります。
https://www.npmjs.com/package/@nuxtjs/google-gtag

GA4で確認する

nuxt-ga4.png

GA4にアクセスしてデータ収集が有効となれば上手く設定できています。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?