mkdocs-material icon indicating copy to clipboard operation
mkdocs-material copied to clipboard

Google Analytics with New Tag Format

Open menbren opened this issue 6 years ago • 33 comments

Hi!

Thanks for the awesome theme!

Quick one, I couldn't get the build-in Google Analytics support to work with Google's new tag format (G-GXXXXXXXXX). I was able to get it to work using an override, but I thought you might wanted to know.

Cheers, Menno

menbren avatar Nov 19 '20 09:11 menbren

Thanks for reporting. Could you share some more details (i.e. the "why" and your solution), so we can decide whether we should address this? ☺️

squidfunk avatar Nov 19 '20 10:11 squidfunk

Of course! So I was trying to setup Analytics as described here. It mentions you need to provide the following info:

google_analytics:
  - UA-XXXXXXXX-X
  - auto

Only, Google has changed to structure of its tags for Google Analytics 4 (GA4). They are now formatted as G-GXXXXXXXXX. But if I enter such a tag, it doesn't automatically generate a functional analytics script. My guess is, the theme doesn't support the new tag structure yet.

menbren avatar Nov 19 '20 12:11 menbren

Thanks for providing more details. So this demands the use of Google Tag manager? If so, please see https://github.com/squidfunk/mkdocs-material/issues/1972#issuecomment-706375807

squidfunk avatar Nov 19 '20 21:11 squidfunk

Not tag manager in particular. When you currently go to Google Analytics and you add a new property, the only tag that you are given by default is this new GA4 tag now. I believe they have sort of integrated/unified these tags.

It appears to me as they are phasing out the UA-XXXXXXXX-X formatted tags in favour of these newly formatted G-GXXXXXXXXX tags.

menbren avatar Nov 20 '20 07:11 menbren

Could you share your override? That would make it much simpler for me to integrate.

squidfunk avatar Nov 20 '20 07:11 squidfunk

Sure, I've put this in the main.html

{% extends "base.html" %}

{% block analytics %}
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag() {
        dataLayer.push(arguments);
    }
    gtag('js', new Date());
    gtag('config', 'G-XXXXXXXXXX');
</script>
{% endblock %}

edited by @squidfunk to include analytics block

menbren avatar Nov 20 '20 07:11 menbren

I see. This, however, is gtag.js, which we won't switch to right now for the reasons stated in the linked comment. Unless there's a solution, which is based on analytics.js, I'd postpone this until Google improved/modularized gtag.js or analytics.js is not available anymore. It really is a bait-and-switch, see the comment I linked before.

For everybody else coming here – if you need this, you can use this code as a custom analytics override.

squidfunk avatar Nov 20 '20 08:11 squidfunk

Just found this thread while looking things up after responding here https://github.com/squidfunk/mkdocs-material/issues/1972#issuecomment-743767157

As stated in my other comment it seems like google no longer allows new users to sign up for google analytics without using v4, which no longer provides a UA-#####. Existing users are being sent emails urging them to upgrade, and once you upgrade to v4 you loose your UA-##### and are assigned a new G-ABC123 which appears to be incompatible with gtags.

So currently all users who do not have a pre-existing UA-##### will be forced to use an override for the template. While it's great that this functionality is available it's unfortunate that the bar is set slightly higher in this scenario.

Thinking about it: would it be possible to have both? Meaning leave analytics.js in place and usable when users setup

google_analytics:
  - UA-#####
  - auto

but introduce a new

gtags:
  - G-ABC123

for those who have upgraded or are new?

edit: removed - auto from the gtags example, it seems they don't use it.

nemchik avatar Dec 12 '20 15:12 nemchik

I’d consider this to be a good candidate for an upstream change that would need to be addressed as part of MkDocs, as this theme solely consumes the properties set through mkdocs.yml. The best idea may be to open an issue over at the upstream repo if the GA change is irreversible, since it will concern all themes, not only this one.

squidfunk avatar Dec 12 '20 22:12 squidfunk

Meanwhile... maybe a quick tuto ? Most of us do not want/care to extend the theme, just use it simply, + activate Google Analytics, so I'm convinced a "step by step" instructions set would be very appreciated by a lot of users of this great repository :)

nvuillam avatar Dec 12 '20 23:12 nvuillam

I haven't received any mails from Google yet regarding the depreciation - care to write a guide? We can collaborate on a PR to get something up in the docs.

squidfunk avatar Dec 12 '20 23:12 squidfunk

I want the guide for myself haha ^^ (pretty busy on the repos I already contribute to ^^ )

nvuillam avatar Dec 12 '20 23:12 nvuillam

here https://github.com/TRaSH-/Guides/pull/57/files and here https://github.com/nemchik/nemchik/compare/c06c7cc51dfdc2ca54e2b5f1adecf2c81bf62952...master

In both cases I am using navigation.instant. if you are not then you can skip the javascript extras and use what was posted above https://github.com/squidfunk/mkdocs-material/issues/2055#issuecomment-730974196

nemchik avatar Dec 13 '20 01:12 nemchik

I’d consider this to be a good candidate for an upstream change that would need to be addressed as part of MkDocs, as this theme solely consumes the properties set through mkdocs.yml. The best idea may be to open an issue over at the upstream repo if the GA change is irreversible, since it will concern all themes, not only this one.

Hopefully I've described this well enough https://github.com/mkdocs/mkdocs/issues/2252

nemchik avatar Dec 13 '20 01:12 nemchik

@nemchik many many thanks for your PR link, that was all I needed :)

https://github.com/nvuillam/mega-linter/pull/155

nvuillam avatar Dec 13 '20 10:12 nvuillam

@nemchik Your implementation is broken in version 7.0 of mkdocs-material.

Most likely related to the changes to overrides.

th0mk avatar Feb 26 '21 14:02 th0mk

@nemchik Your implementation is broken in version 7.0 of mkdocs-material.

Most likely related to the changes to overrides.

Looks like the changes needed to resolve the issue are actually because app.location$.subscribe changes to location$.subscribe which is now documented here https://squidfunk.github.io/mkdocs-material/setup/setting-up-site-analytics/#other-analytics-providers (the change isn't called out, but the new way to do it is documented). So a commit like this should fix the issue. https://github.com/nemchik/nemchik/commit/71dcbfa47091da22eb7e002d8bb57c449c5c5f5f#diff-345aa1c0dd357fc2ce4eba286da0c84b0a6b6c97096f72cbf3c8c213a2accd72

nemchik avatar Mar 02 '21 14:03 nemchik

I should note that my current method (after reviewing the above) is no longer to use an override for the analytics block but instead only use an extra javascript that looks like this

var gtag_id = "G-ABC123";

var script = document.createElement("script");
script.src = "https://www.googletagmanager.com/gtag/js?id=" + gtag_id;
document.head.appendChild(script);

location$.subscribe(function (url) {
  window.dataLayer = window.dataLayer || [];

  function gtag() {
    dataLayer.push(arguments);
  }

  gtag("js", new Date());
  gtag("config", gtag_id);
});

nemchik avatar Mar 02 '21 14:03 nemchik

Hi, are there any update for using Google Analytics 4?

Mistic92 avatar May 28 '21 12:05 Mistic92

Nope, still waiting for https://github.com/mkdocs/mkdocs/issues/2252.

squidfunk avatar May 28 '21 12:05 squidfunk