feat: add google analytics

This commit is contained in:
LeoKu 2021-10-17 17:32:01 +08:00
parent 34f19df3d2
commit 024f9afd5c
4 changed files with 33 additions and 1 deletions

View File

@ -4,9 +4,24 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<link rel="icon" href="/favicon.svg" /> <link rel="icon" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="A pure front-end avatar generator" /> <meta name="description" content="A pure front-end avatar generator." />
<title>Vue Color Avatar</title> <title>Vue Color Avatar</title>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-B4C9FN5DFR"
></script>
<script>
window.dataLayer = window.dataLayer || []
function gtag() {
dataLayer.push(arguments)
}
gtag('js', new Date())
gtag('config', 'G-B4C9FN5DFR')
</script>
<style> <style>
body { body {
margin: 0; margin: 0;

View File

@ -91,6 +91,7 @@ import {
NOT_COMPATIBLE_AGENTS, NOT_COMPATIBLE_AGENTS,
TRIGGER_PROBABILITY, TRIGGER_PROBABILITY,
} from '@/utils/constant' } from '@/utils/constant'
import { recordClick } from '@/utils/ga'
import Confetti from './components/Confetti.vue' import Confetti from './components/Confetti.vue'
@ -175,6 +176,10 @@ function handleAction(actionType: ActionType) {
case ActionType.Code: case ActionType.Code:
codeVisible.value = !codeVisible.value codeVisible.value = !codeVisible.value
recordClick({
event_category: 'code',
event_label: 'view code',
})
break break
} }
} }

5
src/env.d.ts vendored
View File

@ -6,3 +6,8 @@ declare module '*.vue' {
const component: DefineComponent<{}, {}, any> const component: DefineComponent<{}, {}, any>
export default component export default component
} }
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
gtag: any
}

7
src/utils/ga.ts Normal file
View File

@ -0,0 +1,7 @@
export function recordClick(params: {
event_category: string
event_label?: string
value?: number
}) {
window?.gtag('event', 'Click', params)
}