1
1

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 3 years have passed since last update.

Vue.jsにGoogleAnalyticsを導入

Posted at

概要

SPA化したVue.jsにGoogleAnalyticsを導入したので手順をメモします。
vue-analyticsを利用しました。

環境

Vue.js 2.5.17
vue-analytics 5.22.1

実装

前提

Googleアカウントを持っていて、プロパティを作成し終えていること。

こちらを参考にさせていただきました。
Googleアナリティクスでサイトを追加(プロパティ作成)する方法

手順1 vue-analyticsインストール

npm install vue-analytics

手順1 GoogleAnalyticsのプロパティIDをコピー

GoogleAnalyticsにログインします。

ヘッダーから対象のプロパティを選択→「UA-XXXXXXX」のIDをコピーします。

手順2 実装

私は全てのページに適用させたかったのでvue-routerに実装しました。

router.js
import VueAnalytics from 'vue-analytics'

const router = new VueRouter({
  mode: 'history',
  routes: [
  //省略
  ],
})
Vue.use(VueAnalytics, {
  id: 'UA-XXXXXXXX',
  router
})

.js
Vue.use(VueAnalytics, {
  id: 'UA-XXXXXXXX',
  router
})

このIDのところに先ほどコピーしたIDをペーストします。

まとめ

GoogleAnalyticsを開いて確認してみるとしっかりと接続されていました。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?