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