Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: setup gtm analytic #44

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GTM_ID=xxxx
28 changes: 15 additions & 13 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const withNextra = require('nextra')({
theme: 'nextra-theme-docs',
themeConfig: './theme.config.jsx'
})

const withNextra = require("nextra")({
theme: "nextra-theme-docs",
themeConfig: "./theme.config.jsx",
});

const nextConfig = {
reactStrictMode: true,
output: 'export',
images: {
formats: ['image/webp'],
unoptimized: true,
},
}
reactStrictMode: true,
output: "export",
env: {
GTM_ID: process.env.GTM_ID,
},
images: {
formats: ["image/webp"],
unoptimized: true,
},
};

module.exports = withNextra(nextConfig)
module.exports = withNextra(nextConfig);
19 changes: 19 additions & 0 deletions src/pages/_app.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,28 @@ import '@code-hike/mdx/styles.css'
import { Fragment } from "react"
import Script from "next/script"


export default function App({ Component, pageProps }) {
return (
<Fragment>
<Script
strategy="afterInteractive"
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GTM_ID}`}
/>
<Script
id="gtag-init"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.GTM_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
<Component {...pageProps} />
</Fragment>
)
Expand Down
Loading