Skip to content

Commit 02eeb16

Browse files
committed
Add Typedoc GA plugin
1 parent 5396f26 commit 02eeb16

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

typedoc-plugin-ga.mjs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// @ts-check
2+
// Adapted from https://github.com/TypeStrong/typedoc/issues/2600#issuecomment-2184995074
3+
4+
import td from 'typedoc';
5+
6+
/** @param {td.Application} app */
7+
export function load(app) {
8+
app.options.addDeclaration({
9+
name: 'gaID',
10+
help: 'Set the Google Analytics tracking ID and activate tracking code',
11+
type: td.ParameterType.String,
12+
});
13+
14+
app.renderer.hooks.on('body.end', () => {
15+
const gaID = app.options.getValue('gaID');
16+
if (gaID) {
17+
const script = `
18+
window.dataLayer = window.dataLayer || [];
19+
function gtag(){dataLayer.push(arguments);}
20+
gtag('js', new Date());
21+
gtag('config', '${gaID}');
22+
`.trim();
23+
24+
return td.JSX.createElement(td.JSX.Fragment, null, [
25+
td.JSX.createElement('script', {
26+
async: true,
27+
src: 'https://www.googletagmanager.com/gtag/js?id=' + gaID,
28+
}),
29+
td.JSX.createElement(
30+
'script',
31+
null,
32+
td.JSX.createElement(td.JSX.Raw, { html: script })
33+
),
34+
]);
35+
}
36+
37+
return td.JSX.createElement(td.JSX.Fragment, null);
38+
});
39+
}

typedoc.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"GitHub": "https://github.com/jakeboone02/parse-ingredient/",
99
"Support": "https://github.com/sponsors/jakeboone02"
1010
},
11+
"plugin": ["./typedoc-plugin-ga.mjs"],
1112
"gaID": "G-TMFVDH6J7S"
1213
}

0 commit comments

Comments
 (0)