- 在
plugins
目录下创建名为ga.js
的文件:
/*
** 只在生产模式的客户端中使用
*/
if (process.client && process.env.NODE_ENV === 'production') {
/*
** Google 统计分析脚本
*/
;(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'
)
/*
** 当前页的访问统计
*/
ga('create', 'UA-XXXXXXXX-X', 'auto')
}
export default ({ app: { router }, store }) => {
/*
** 每次路由变更时进行pv统计
*/
router.afterEach((to, from) => {
/*
** 告诉 GA 增加一个 PV
*/
ga('set', 'page', to.fullPath)
ga('send', 'pageview')
})
}
- 将上述代码中的
UA-XXXXXXXX-X
替换为自己的跟踪编号。 - 在
nuxt.config.js
中添加这个插件:
module.exports = {
plugins: [{ src: '~plugins/ga.js', mode: 'client' }]
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
喜欢就支持一下吧