Skip to content

Commit d2393f7

Browse files
committed
feat(plugin-google-analytics)!: migrate to google analytics 4 (close #36)
BREAKING CHANGE: migrate to google analytics 4 and drop v3 support
1 parent 5c80ff3 commit d2393f7

File tree

4 files changed

+40
-34
lines changed

4 files changed

+40
-34
lines changed

Diff for: .vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"esbuild",
2424
"frontmatter",
2525
"globby",
26+
"gtag",
2627
"nprogress",
2728
"prefetch",
2829
"preload",

Diff for: packages/@vuepress/plugin-google-analytics/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
"dependencies": {
3232
"@vuepress/client": "2.0.0-alpha.20",
3333
"@vuepress/core": "2.0.0-alpha.20",
34-
"@vuepress/utils": "2.0.0-alpha.20",
35-
"vue-router": "^4.0.3"
34+
"@vuepress/utils": "2.0.0-alpha.20"
3635
},
3736
"publishConfig": {
3837
"access": "public"

Diff for: packages/@vuepress/plugin-google-analytics/src/clientAppEnhance.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ declare const GA_ID: string
77

88
const id = GA_ID
99

10-
const clientAppEnhance: ClientAppEnhance = ({ router }) => {
10+
const clientAppEnhance: ClientAppEnhance = () => {
1111
if (__DEV__ || __SSR__ || !id) return
1212

13-
useGoogleAnalytics({
14-
id,
15-
router,
16-
})
13+
useGoogleAnalytics(id)
1714
}
1815

1916
export default clientAppEnhance
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
1-
import { START_LOCATION } from 'vue-router'
2-
import type { Router } from 'vue-router'
3-
4-
declare const ga: (...args: any) => void
5-
6-
export interface UseGoogleAnalyticsOptions {
7-
id: string
8-
router: Router
1+
declare global {
2+
interface Window {
3+
dataLayer: any[]
4+
gtag: (...args: any[]) => void
5+
}
96
}
107

8+
declare const gtag: (...args: any[]) => void
9+
1110
/**
12-
* Add analytics.js to your site
11+
* Add gtag.js to your site
1312
*
14-
* @see https://developers.google.com/analytics/devguides/collection/analyticsjs
13+
* @see https://developers.google.com/analytics/devguides/collection/gtagjs
14+
* @see https://developers.google.com/analytics/devguides/collection/gtagjs/pages
15+
* @see https://developers.google.com/analytics/devguides/collection/gtagjs/single-page-applications
16+
*
17+
* The enhanced measurement will listen to browser history events (i.e `pushState`, `popState`, and `replaceState`)
18+
* to collect page_view event, so we do not need to report it manually
19+
*
20+
* @see https://support.google.com/analytics/answer/9216061
1521
*/
16-
export const useGoogleAnalytics = ({
17-
id,
18-
router,
19-
}: UseGoogleAnalyticsOptions): void => {
20-
// @ts-ignore
21-
// eslint-disable-next-line
22-
;(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');
22+
export const useGoogleAnalytics = (id: string): void => {
23+
// avoid duplicated import
24+
if (window.dataLayer && window.gtag) {
25+
return
26+
}
27+
28+
// insert gtag `<script>` tag
29+
const gtagScript = document.createElement('script')
30+
gtagScript.src = `https://www.googletagmanager.com/gtag/js?id=${id}`
31+
gtagScript.async = true
32+
document.head.appendChild(gtagScript)
2333

24-
ga('create', id, 'auto')
25-
ga('set', 'anonymizeIp', true)
34+
// insert gtag snippet
35+
window.dataLayer = window.dataLayer || []
36+
// the gtag function must use `arguments` object to forward parameters
37+
window.gtag = function () {
38+
// eslint-disable-next-line prefer-rest-params
39+
window.dataLayer.push(arguments)
40+
}
2641

27-
router.afterEach((to, from) => {
28-
// send pv after the route is changed
29-
// notice vue-router will trigger an initial navigation from `START_LOCATION`
30-
if (to.path !== from.path || from === START_LOCATION) {
31-
ga('set', 'page', `${router.options.history.base}${to.path}`)
32-
ga('send', 'pageview')
33-
}
34-
})
42+
gtag('js', new Date())
43+
gtag('config', id)
3544
}

0 commit comments

Comments
 (0)