docusaurus

Docusaurus adding tracking google analytics


I have seen some posts about the plugin to add in docusaurs documentation, to track with google analytics. But I tried several methods, and I don't see the track in the google dashboard. I tried in this way:

presets: [
    [
      "classic",
      /** @type {import('@docusaurus/preset-classic').Options} */
      ({
        docs: {
          routeBasePath: "/",
          sidebarPath: require.resolve("./sidebars.js"),
          docLayoutComponent: "@theme/DocPage",
          docItemComponent: "@theme/ApiItem", // Derived from docusaurus-theme-openapi
        },
        gtag: {
          trackingID: "G-XXXXX",
          anonymizeIP: false,
        },
      }),

Can someone help me understand what I'm doing wrong?


Solution

  • Here are a couple of things you could try to make sure the google tag is being added correctly to your webpage:

    1. Make sure your dependencies in package.json are using the same version, e.g.:
    "dependencies": {
        "@docusaurus/core": "^2.4.3",
        "@docusaurus/plugin-google-gtag": "^2.4.3",
        "@docusaurus/preset-classic": "^2.4.3",
    ...
    
    1. Use preset-classic in this way:
    presets: [
        [
          '@docusaurus/preset-classic',
          {
            gtag: {
              trackingID: 'G-XXXXXXXX',
              anonymizeIP: true,
            },
          },
        ],
      ],
    
    1. create a local build (e.g. yarn build) and check the file build/index.html. In this file, look inside the <head> tag and search for a script like this:
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
    

    If this script is there and with the correct tag number, your deployed version should also contain it. Once you confirm that the deployed version has the same script, you can test it with the tag assistant.

    If the tag assistant can see it and the tag number is correct, the analytics service should be able to track it after a couple of days.