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

Add Google Analytics #233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,25 @@ const bannerOffsetByPosition = {
}
const bannerOffset =
bannerOffsetByPosition[siteConfig.banner.position || 'center']

const gtagId = siteConfig.googleAnalytics? siteConfig.googleAnalytics.id : ''
---

<!DOCTYPE html>
<html lang={siteLang} class="bg-[var(--page-bg)] transition text-[14px] md:text-[16px]"
data-overlayscrollbars-initialize
>
<head>
{gtagId.length > 0 &&
<!-- Google tag (gtag.js) -->
<script async src={`https://www.googletagmanager.com/gtag/js?id=${gtagId}`}></script>
<script is:inline define:vars={{gtagId}}>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', gtagId);
</script>}

<title>{pageTitle}</title>

Expand Down
3 changes: 3 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export type SiteConfig = {
enable: boolean
depth: 1 | 2 | 3
}
googleAnalytics?: {
id: string
}
Comment on lines +27 to +29
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should also be added to the src/config.ts and default to empty.


favicon: Favicon[]
}
Expand Down