Repository navigation
Description
Google recently introduces Analytics v4.
ga4 no longer provides IDs compatible with analytics.js which are in the format UA-#####-#
ga4 now uses a new gtag.js and IDs in the format G-ABC123
Google no longer allows creation of properties with the older analytics IDs. Existing Google Analytics properties can be upgraded and converted to ga4 which will replace their UA-#####-# ID with a G-ABC123 ID. There is no way to downgrade.
Google is emailing existing users encouraging them to upgrade to ga4. To my knowledge at this time they have not announced any kind of cut off date for when migrating to ga4 must be completed.
Currently it is possible to use template overrides to put the new ga4 into an mkdocs site, however the
google_analytics:
- UA-#####
- autoformat is extremely convenient and easy to use (by comparison to the slightly more advanced setup of overrides).
For the purpose of making the transition possible without breaking existing setups without warning, I think it would be great to add
gtag:
- G-ABC123options to mkdocs.yml that would use the new gtag.js which is documented by google to be implemented as
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABC123"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-ABC123');
</script>This would allow existing mkdocs users to continue using google_analytics: for as long as google continues to support it, or upgrade to gtag: very easily at their own discretion.
Alternatively it would also probably be file to implement something in google_analytics: handling that uses gtag depending on the ID specified.
Activity
waylan commentedon Dec 13, 2020
We have wanted to move analytics to a plugin for some time (which would make it easier for users to switch it out for another service of their choice). This would be a great opportunity to do that. As users would need to update their config to include the new
gtaganyway, they can do so by enabling the plugin and providing thegtagas a config option on the plugin.Personally, the time I have available to work on MkDocs is very limited these days, so any contributions would help this alone.
waylan commentedon Dec 13, 2020
I just noticed that there are some references to this issue from the material theme. Rather than moving analytics to a plugin, another possibility would be to make the new
gtagconfig option a theme option (nested under the theme setting). This would give theme developers complete control over it and possibly allow users to support a different service through theme customization.The more I think about it, I'm not sure a plugin makes sense. All we need to do is insert a few lines of javascript into each page with the
gtagincluded. That is clearly the role of templates. But to make that work from a plugin would be more difficult as a plugin can't override part of a template. It would have to work with an "included" template provided by the plugin. And that means the plugin would need to provide the relevant template. However, I suspect some template devs would prefer to provide their own.@squidfunk, do you have any comments on whether this would make more sense as a plugin or as a theme specific config option?
squidfunk commentedon Dec 13, 2020
That's definitely an option, but I first wanted to discuss this with you. IIRC, you were considering removing the top-level
google_analyticskey anyway, as it was added in a time were there was basically only Google Analytics offering this service. I agree that it would make much more sense as either a theme option or as a plugin. The former would give more flexibility. I just did not want to offer two options, one at the top-level and one belowtheme, as that would lead to confusion.If you ask me, I'd be all for deprecating the
google_analyticskey and leaving it up to the theme to implement it. Just print a notice that it's deprecated and that themes should implement it downstream. Over at Material for MkDocs I'll move configuration into thethemeorextrascope, and fall back to thegoogle_analyticskey as long as it is in deprecation phase. Then, with the next major release, we could remove it altogether. This would essentially free you from maintaining it.waylan commentedon Dec 13, 2020
I think that's an excellent plan. The deprecation notice should include a comment that Google is deprecating the old ID format. Let's pass on the blame for the change. 😁
We could raise the warning for two separate conditions: (1) when a user sets the
google_analyticsconfig setting, and (2) when a template referencesconfig.google_analytics. Does it make more sense to do 1, 2 or both?We'll plan on doing the same with the built-in themes and recommend third party themes do the same.
Not entirely, but it would move maintenance strictly to the built-in themes, which ensures that any details are theme specific. That eliminates any backward compatibility issues with third party themes in the future.
As an aside, this move will also be more consistent. From time to time we receive a request for some feature and I respond that the feature should be implemented through a theme. An often cited counterargument is that analytics is not implemented through a theme. This change will eliminate that counterargument.
waylan commentedon Dec 14, 2020
Is there a link we can point to for this?
nemchik commentedon Dec 14, 2020
https://support.google.com/analytics/answer/9744165?hl=en
waylan commentedon Dec 14, 2020
Thanks @nemchik. That will be helpful to link to in the release notes. However, I was also looking for confirmation that "Google no longer allows creation of properties with the older analytics IDs." We don't want to make that claim unless we can confirm it.
waylan commentedon Dec 14, 2020
The note at the top of this page seems to discourage using the old IDs, but I get the sense that you can still create them and use them.
nemchik commentedon Dec 14, 2020
I looked into this and actually found I was slightly mistaken.
Google defaults to new accounts being created as GA4, but does still offer the ability to create property IDs for the older version of GA. They hide the option under the "advanced" button during creation.
Also for existing accounts that do the upgrade it looks like a new property for GA4 is created, but the old one is still usable even after performing this upgrade. Collection of information will only happen on one or the other (depending on whether you have analytics.js or gtag.js on the page). When I first upgraded one of my properties to GA4 the old property ID disappeared so I thought this was an irreversible change, however it has now reappeared (I'm not crazy, I promise it was gone 😄 ).
waylan commentedon Dec 14, 2020
Based on the comments in the bottom section of this page, if you properly do an upgrade, the new and old IDs will be linked and the old data will continue to be updated from the new ID. However, the new ID will not have the old data transferred over.
This is good to know as some users may want to continue to use the old data format. They can continue to do so. According to this, even new users can still create an old style ID and link it to the new
G-ID.waylan commentedon Dec 14, 2020
I have a working and documented (but not fully tested) implementation in #2253. Any feedback is welcome.
Deprecate google_analytics config option.
Type checking improvements