From ff4df285bf9ab240ca5ed9f77b2815296af2505b Mon Sep 17 00:00:00 2001 From: Junwei Zhao Date: Sun, 30 Aug 2020 00:33:14 +1000 Subject: [PATCH] Use another way to enable google analytics --- www/layouts/default.vue | 16 --------------- www/nuxt.config.js | 3 +-- www/plugins/ga.js | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 www/plugins/ga.js diff --git a/www/layouts/default.vue b/www/layouts/default.vue index 83178e7..a1f51dc 100644 --- a/www/layouts/default.vue +++ b/www/layouts/default.vue @@ -31,22 +31,6 @@ -
- - - -
diff --git a/www/nuxt.config.js b/www/nuxt.config.js index 04917c2..973781b 100644 --- a/www/nuxt.config.js +++ b/www/nuxt.config.js @@ -34,8 +34,7 @@ export default { ** Plugins to load before mounting the App ** https://nuxtjs.org/guide/plugins */ - plugins: [ - ], + plugins: [{ src: '~plugins/ga.js', mode: 'client' }], /* ** Auto import components ** See https://nuxtjs.org/api/configuration-components diff --git a/www/plugins/ga.js b/www/plugins/ga.js new file mode 100644 index 0000000..dde3f9b --- /dev/null +++ b/www/plugins/ga.js @@ -0,0 +1,44 @@ +/* eslint-disable */ + +export default ({ app }) => { + /* + ** Only run on client-side and only in production mode + */ + if (process.env.NODE_ENV !== "production") + return /* + ** Include Google Analytics Script + */; + (function(i, s, o, g, r, a, m) { + i["GoogleAnalyticsObject"] = r; + (i[r] = + i[r] || + function() { + (i[r].q = i[r].q || []).push(arguments); + }), + (i[r].l = 1 * new Date()); + (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); + a.async = 1; + a.src = g; + m.parentNode.insertBefore(a, m); + })( + window, + document, + "script", + "https://www.google-analytics.com/analytics.js", + "ga" + ); + /* + ** Set the current page + */ + ga("create", "UA-5243064-21", "auto"); + /* + ** Every time the route changes (fired on initialization too) + */ + app.router.afterEach((to, from) => { + /* + ** We tell Google Analytics to add a `pageview` + */ + ga("set", "page", to.fullPath); + ga("send", "pageview"); + }); +};