|
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 | + } |
9 | 6 | }
|
10 | 7 |
|
| 8 | +declare const gtag: (...args: any[]) => void |
| 9 | + |
11 | 10 | /**
|
12 |
| - * Add analytics.js to your site |
| 11 | + * Add gtag.js to your site |
13 | 12 | *
|
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 |
15 | 21 | */
|
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) |
23 | 33 |
|
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 | + } |
26 | 41 |
|
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) |
35 | 44 | }
|
0 commit comments