Repository navigation
Closed as not planned
Labels
Description
I'm trying to switch from Docsify, where, at runtime I would have this JS run:
let googleAnalyticsId = 'UA-1234567-50'; // default to dev
if (window.location.host.includes('apps-test.example.com')) {
googleAnalyticsId = 'UA-1234567-59';
} else if (window.location.host.includes('apps.example.com')) {
googleAnalyticsId = 'UA-1234567-51';
}
window.$docsify = {
ga: googleAnalyticsId,
};The documentation is vague and does not explain what the code is, or how it works:
google_analytics: ['UA-36723568-3', 'mkdocs.org']Is the second argument a name? Something to match against before analytics are enabled? How would you go about defining which ID to use based on the current environment (dev/test/prod/local)? Can you hook into this on page load (since GA is just running in JS anyway) so the above logic can stay in the JavaScript?
Can the docs be updated to have these answers?
It looks like there is some theme or template file for analytics, but there is no example.
For others looking for a work around to the lack of proper documentation, here's the best thing I could come up with:
- In the root of your project create a
themefolder - Put
footer.htmlin the folder - Edit it and add this:
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
let googleAnalyticsId = 'UA-1234567-50'; // default to dev
if (window.location.host.includes('apps-test.example.com')) {
googleAnalyticsId = 'UA-1234567-59';
} else if (window.location.host.includes('apps.example.com')) {
googleAnalyticsId = 'UA-1234567-51';
}
ga('create', googleAnalyticsId, 'auto');
ga('send', 'pageview');
</script>Reacted by Christian Oliff and Martin Michlmayr
Activity
waylan commentedon May 11, 2020
In the
mkdocstheme, those config options are used in the template here:mkdocs/mkdocs/themes/mkdocs/base.html
Lines 44 to 56 in a4eb4eb
And for the
readthedocstheme:mkdocs/mkdocs/themes/readthedocs/base.html
Lines 55 to 76 in a4eb4eb
Of course, for third-party themes, things may be different.
Regarding the two values, they are the two values passed to
ga(create...). Of course, back when the documentation was written, it corresponded with Google documentation and was clear. Google has since changed their documentation which makes ours less clear.In any event, a PR with suggested changes is always welcome.
sorenwacker commentedon Sep 24, 2021
The documentation says something like:
Which one is correct now??
lovelydinosaur commentedon Apr 12, 2024
Let's close this off in favor of #3630
Including GA should be supported as a template override, not a config option.
Any docs here should probably be generic - we can leave it to analytics providers to deal with their own "here's how to include us on your website" docs.