Skip to content

Commit 4e59458

Browse files
authored
feat: add GTM and GA support (#211)
* feat: add GTM and GA support * chore: format * chore: remove noscript version of GTM * chore: remove unused import
1 parent 12a36a9 commit 4e59458

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

packages/server/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export interface ConfigWithoutLocales {
3131
// Adds Google Tag Manager to your documentation pages.
3232
googleTagManager: string;
3333
// Whether zoomable images are enabled by default
34+
googleAnalytics: string;
35+
// Whether zoomable images are enabled by default
3436
zoomImages: boolean;
3537
// Whether CodeHike is enabled
3638
experimentalCodehike: boolean;
@@ -71,6 +73,8 @@ export interface ConfigWithLocales {
7173
variables: Record<string, string>;
7274
// Adds Google Tag Manager to your documentation pages.
7375
googleTagManager: string;
76+
// Adds Google Analytics to your documentation pages.
77+
googleAnalytics: string;
7478
// Whether zoomable images are enabled by default
7579
zoomImages: boolean;
7680
// Whether CodeHike is enabled
@@ -119,6 +123,8 @@ export interface OutputConfig {
119123
variables: Record<string, string>;
120124
// Adds Google Tag Manager to your documentation pages.
121125
googleTagManager: string;
126+
// Adds Google Analytics to your documentation pages.
127+
googleAnalytics: string;
122128
// Whether zoomable images are enabled by default
123129
zoomImages: boolean;
124130
// Whether CodeHike is enabled
@@ -141,6 +147,7 @@ export const defaultConfig: OutputConfig = {
141147
headerDepth: 3,
142148
variables: {},
143149
googleTagManager: '',
150+
googleAnalytics: '',
144151
zoomImages: false,
145152
experimentalCodehike: false,
146153
experimentalMath: false,

website/app/components/Head.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,37 @@ import codeHikeStyles from '@code-hike/mdx/dist/index.css';
44

55
export const Head = ({ data }: { data: DocumentationLoader }) => {
66
const favicon = getFavicon({ data });
7+
78
return (
89
<Helmet>
10+
{data.config.googleAnalytics && (
11+
<script
12+
async
13+
src={`https://www.googletagmanager.com/gtag/js?id=${data.config.googleAnalytics}`}
14+
></script>
15+
)}
16+
{data.config.googleAnalytics && (
17+
<script type="text/javascript">
18+
{`
19+
window.dataLayer = window.dataLayer || [];
20+
function gtag(){dataLayer.push(arguments);}
21+
gtag('js', new Date());
22+
23+
gtag('config', '${data.config.googleAnalytics}');
24+
`}
25+
</script>
26+
)}
27+
{data.config.googleTagManager && (
28+
<script type="text/javascript">
29+
{`
30+
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
31+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
32+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
33+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
34+
})(window,document,'script','dataLayer','${data.config.googleTagManager}');
35+
`}
36+
</script>
37+
)}
938
<link rel="icon" href={favicon} />
1039
{data.config.experimentalMath && (
1140
<link

website/app/utils/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export interface ProjectConfig {
9292
variables: Record<string, string>;
9393
// Adds Google Tag Manager to your documentation pages.
9494
googleTagManager: string;
95+
// Adds Google Analytics to your documentation pages.
96+
googleAnalytics: string;
9597
// Whether zoomable images are enabled by default
9698
zoomImages: boolean;
9799
// Whether CodeHike is enabled
@@ -114,6 +116,7 @@ export const defaultConfig: ProjectConfig = {
114116
headerDepth: 3,
115117
variables: {},
116118
googleTagManager: '',
119+
googleAnalytics: '',
117120
zoomImages: false,
118121
experimentalCodehike: false,
119122
experimentalMath: false,
@@ -142,6 +145,7 @@ export function mergeConfig(json: Record<string, unknown>): ProjectConfig {
142145
headerDepth: getNumber(json, 'headerDepth', defaultConfig.headerDepth),
143146
variables: getValue(json, 'variables', defaultConfig.variables) as Record<string, string>,
144147
googleTagManager: getString(json, 'googleTagManager', defaultConfig.googleTagManager),
148+
googleAnalytics: getString(json, 'googleAnalytics', defaultConfig.googleAnalytics),
145149
zoomImages: getBoolean(json, 'zoomImages', defaultConfig.zoomImages),
146150
// TODO: tidy the following:
147151
locales: (json.locales as Record<string, string>) ?? undefined,

0 commit comments

Comments
 (0)