Skip to content

Commit

Permalink
Add --gaMeasurementId option
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
Drarig29 committed Jan 8, 2023
1 parent a6b95db commit 3b70c45
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist/
.idea/
package-lock.json
.DS_Store
.vscode
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ The following arguments can be used in addition to the default [TypeDoc argument
Default: `false`<br>
**Note:** If combined with `--footerDate`, it will append "Jul 29, 2022, 3:44:42 PM GMT+2".

- `--favicon`<br>
Specify a [Google Analytics](https://support.google.com/analytics/answer/1008080?hl=en&ref_topic=1008079#zippy=%2Cin-this-article%2Cstatic-website) measurement ID to insert in a `gtag.js` snippet.<br>
Example: `abc123`<br>

## Testing

To test this plugin, you can generate TypeDoc documentation for this plugin.
Expand Down
24 changes: 21 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export function makeRelativeToRoot(hostPath: string, targetFilename: string): st
*/
export function appendFavicon(html: string, url: string): string {
return html.replace('</head>',
'\t' + // Some tabulation to fit the rest of the document.
`<link rel="icon" href="${url}" />` +
'\n' + // Push the end of <head> to the next line.
'</head>'
Expand All @@ -45,7 +44,7 @@ export function appendToFooter(html: string, value: string): string {
}

/**
* Sets up the newline in footer.
* Sets up a space between the first line of the footer and the date.
* @param html HTML string to append to.
*/
export function setupNewlineInFooter(html: string): string {
Expand Down Expand Up @@ -155,4 +154,23 @@ export const getDateTimeScript = (options: PluginOptions) => {
document.getElementById('generation-date').innerText = formatter.format(window.GENERATION_DATE);
`;
};
};

export const insertGAScriptInHead = (html: string, measurementId: string) => {
const script = `<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=${measurementId}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${measurementId}');
</script>`

return html.replace('<head>',
'<head>' +
'\n' +
script +
'\n'
);
}
19 changes: 15 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
replaceDescription,
setupNewlineInFooter,
getLastModifiedScript,
getDateTimeScript
getDateTimeScript,
insertGAScriptInHead
} from './helpers';

const TYPEDOC_VERSION = Application.VERSION;
Expand All @@ -28,6 +29,7 @@ export const pluginOptions = (app: Application) => ({
customTitle: app.options.getValue('customTitle') as string | undefined,
customTitleLink: app.options.getValue('customTitleLink') as string | undefined,
customDescription: app.options.getValue('customDescription') as string | undefined,
gaMeasurementId: app.options.getValue('gaMeasurementId') as string | undefined,
}),
});

Expand Down Expand Up @@ -91,6 +93,11 @@ export function load(app: Application) {
defaultValue: undefined
});

app.options.addDeclaration({
name: 'gaMeasurementId',
help: 'Extras Plugin: Specify a Google Analytics measurement ID to insert in a gtag.js snippet.'
});

const options = pluginOptions(app);

app.renderer.on(PageEvent.END, onPageRendered.bind(options));
Expand All @@ -117,8 +124,6 @@ function onPageRendered(this: PluginOptionsGetter, page: PageEvent) {
page.contents = appendToFooter(page.contents, ` v${TYPEDOC_VERSION}`);
}

page.contents = setupNewlineInFooter(page.contents);

// Add generation date.
if (!options.hideGenerator && (options.footerLastModified || options.footerDate || options.footerTime)) {
const now = new Date();
Expand All @@ -139,6 +144,7 @@ function onPageRendered(this: PluginOptionsGetter, page: PageEvent) {
</script>
`;

page.contents = setupNewlineInFooter(page.contents);
page.contents = appendToFooter(page.contents, html);
}

Expand All @@ -152,10 +158,15 @@ function onPageRendered(this: PluginOptionsGetter, page: PageEvent) {
page.contents = replaceTopMostTitleLink(page.contents, options.customTitleLink);
}

// Set custom description
// Set custom description.
if (options.customDescription) {
page.contents = replaceDescription(page.contents, options.customDescription);
}

// Insert Google Analytics script.
if (options.gaMeasurementId) {
page.contents = insertGAScriptInHead(page.contents, options.gaMeasurementId);
}
}

function onRenderFinished(this: PluginOptionsGetter) {
Expand Down

0 comments on commit 3b70c45

Please sign in to comment.