Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Analytics documenation #2101

Closed
TheJaredWilcurt opened this issue May 11, 2020 · 3 comments
Closed

Google Analytics documenation #2101

TheJaredWilcurt opened this issue May 11, 2020 · 3 comments

Comments

@TheJaredWilcurt
Copy link

TheJaredWilcurt commented May 11, 2020

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:

  1. In the root of your project create a theme folder
  2. Put footer.html in the folder
  3. 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>
@waylan
Copy link
Member

waylan commented May 11, 2020

In the mkdocs theme, those config options are used in the template here:

{%- block analytics %}
{%- if config.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');
ga('create', '{{ config.google_analytics[0] }}', '{{ config.google_analytics[1] }}');
ga('send', 'pageview');
</script>
{%- endif %}
{%- endblock %}

And for the readthedocs theme:

{%- block analytics %}
{% if config.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');
ga('create', '{{ config.google_analytics[0] }}', '{{ config.google_analytics[1] }}');
ga('send', 'pageview');
</script>
{% endif %}
{%- endblock %}
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
{# SIDE NAV, TOGGLES ON MOBILE #}
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">

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
Copy link

The documentation says something like:

extra:
  analytics:
    provider: google
    propertyl: UA-...

Which one is correct now??

@tomchristie
Copy link
Member

tomchristie commented 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.

@tomchristie tomchristie closed this as not planned Won't fix, can't repro, duplicate, stale Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants