How to inject the google analytics tracking information into the html file #272
-
Hi there I am wondering if there is anyway to inject the google analytics tracking info into the output html file? Recently I used the honkit framework to generate book from md, it has a plugin as below. https://www.npmjs.com/package/honkit-plugin-google-analytics |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
BTW, is there any document related to the custom engine.js? I know this is a very powerful way to customize build process, but don't know how to leverage this power to inject some fix content into the output html file. `javascript
}) |
Beta Was this translation helpful? Give feedback.
-
Place the snippet by Google Analytics into the first page of your Markdown, and output HTML in Marp CLI with enabled <script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script> https://developers.google.com/analytics/devguides/collection/analyticsjs Of course you also can make a plugin to prepend the script like this: const script = `
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
`
module.exports = ({ marp }) =>
marp.use((md) => {
// Remember old renderer, if overridden, or proxy to default renderer
// @see https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
const defaultRender =
md.renderer.rules.marpit_slide_open ||
function (tokens, i, opts, _, slf) {
return slf.renderToken(tokens, i, opts)
}
// "marpit_slide_open" token type is meaning the opening tag of
// Marpit-compatible slide. Commonly it would be rendered as <section> tag.
// @see https://github.com/marp-team/marpit/blob/main/src/markdown/slide.js
md.renderer.rules.marpit_slide_open = function (tokens, i, opts, env, slf) {
// Pass token to default renderer and store the rendered HTML
const rendered = defaultRender(tokens, i, opts, env, slf)
// The slide index has stored in meta.marpitSlide.
const { meta } = tokens[i]
// If it is the first slide, prepend <script> tag before the rendered tag.
if (meta && meta.marpitSlide === 0) return script + rendered
return rendered
}
}) Marp is extended from markdown-it, and the plugin architecture is exactly the same as markdown-it. Refer to #206 (comment). There is no documentation for Marp/Marpit internal tokens because they are basically private interfaces.
|
Beta Was this translation helpful? Give feedback.
Place the snippet by Google Analytics into the first page of your Markdown, and output HTML in Marp CLI with enabled
--html
option.https://developers.google.com/analytics/devguides/collection/analyticsjs
Of course you also can make a plugin to prepend the script like this: