Skip to content

Commit

Permalink
feat(ganalytics): support google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Octobug committed Nov 22, 2023
1 parent 55adf29 commit 5e22cc5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfigWithTheme } from "vitepress";
import type { ThemeConfig } from "./theme/types/theme-config";
import themeConfig from "./config.theme";
import { BASE_URL, withBaseURL } from "./config.utils";
import gaConfig from "./theme/ganalytics";

export default defineConfigWithTheme<ThemeConfig>({
title: "WhaleVocal",
Expand All @@ -25,6 +26,7 @@ export default defineConfigWithTheme<ThemeConfig>({
content: "Octobug",
},
],
...gaConfig
],
rewrites: {
":postsdir/:title/README.md": ":postsdir/:title.md",
Expand Down
28 changes: 28 additions & 0 deletions .vitepress/theme/ganalytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { HeadConfig } from "vitepress";
import { env } from "process";

const G_M_ID = env.G_MEASUREMENT_ID;

const scriptGTag = [
"script",
{
async: "async",
src: `https://www.googletagmanager.com/gtag/js?id=${G_M_ID}`
},
] satisfies HeadConfig;

const scriptGTagData = [
"script",
{},
`window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${G_M_ID}');`
] satisfies HeadConfig;

const gaConfig = [
scriptGTag,
scriptGTagData
];

export default gaConfig satisfies HeadConfig[];

0 comments on commit 5e22cc5

Please sign in to comment.