docsify icon indicating copy to clipboard operation
docsify copied to clipboard

feat: add google analytics gtag.js plugin

Open ekoz opened this issue 3 years ago • 18 comments

Summary

Google Universal Analytics (gtag.js) plugin, usage is the same as src/plugins/ga.js

What kind of change does this PR introduce?

Feature

For any code change,

  • [ ] Related documentation has been updated if needed
  • [ ] Related tests have been updated or tests have been added

Does this PR introduce a breaking change? (check one)

  • [ ] Yes
  • [ ] No

If yes, please describe the impact and migration path for existing applications:

Related issue, if any:

#1695

Tested in the following browsers:

  • [x] Chrome
  • [ ] Firefox
  • [ ] Safari
  • [ ] Edge
  • [ ] IE

ekoz avatar Dec 29 '21 09:12 ekoz

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/docsify-core/docsify-preview/42cj1nPMt4MNgraowyET4t8gHDEe
✅ Preview: https://docsify-preview-git-fork-ekoz-develop-docsify-core.vercel.app

vercel[bot] avatar Dec 29 '21 09:12 vercel[bot]

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6a3bd84a07d9a50e0dc3beb5cb67890ca3dd5fe8:

Sandbox Source
docsify-template Configuration

codesandbox-ci[bot] avatar Dec 29 '21 09:12 codesandbox-ci[bot]

Nice, thanks! Can you add a similar doc section as GA? Also it needs lint fix.

trusktr avatar Dec 30 '21 00:12 trusktr

@trusktr Hi, I update this merge request again, pls check it when you are free, thanks ~

ekoz avatar Jan 04 '22 06:01 ekoz

@trusktr Hi, there is an error, ci/codesandbox — Building packages failed., Is there anything I can do?

ekoz avatar Jan 05 '22 02:01 ekoz

Hey @ekoz. Updated and re-running the checks.

trusktr avatar Jan 26 '22 17:01 trusktr

Hi @ekoz. Checking in to see if/when the requested changes can be made so we can move forward with this PR.

Thanks!

jhildenbiddle avatar Apr 21 '22 17:04 jhildenbiddle

@jhildenbiddle I want to design $docsify.ga to support both strings and arrays, but there are relatively few data for Google ads and floodlight. Can the current version only support GA3 and GA4?

After we support arrays, if the user passes in multiple G-, such as [G-111, G-222, G-333], do we need to support them? This problem does not exist in the case of string.

ekoz avatar Apr 22 '22 07:04 ekoz

@ekoz --

It is my understanding that the only thing required to support multiple properties ("G-", "AW-", "DC-", ...) or multiple identifiers for the same property ("G-XX1", "G-XX2", ...) is to render a separate gtag() statement for each:

// GA3
gtag('config', 'UA-XXXXXXXX');

// Multiple GA4
gtag('config', 'G-XXXXXXX1');
gtag('config', 'G-XXXXXXX2');

// Other Services
gtag('config', 'AW-XXXXXXXX');
gtag('config', 'DC-XXXXXXXX');

This is based on the following documentation:

  • https://developers.google.com/tag-platform/gtagjs/install
  • https://developers.google.com/analytics/devguides/collection/gtagjs/#configure_additional_google_analytics_properties

So the implementation should be simple: just iterate over the array of IDs and render a gtag('${id}') for each.

It's worth nothing that this post on StackOverflow seems to imply that this method of supporting multiple IDs for the same service does not always work. I can't verify this, but since but since Google's official documentation states that it this is how it is supposed to work, I think we're okay just rendering a gtag() for each ID and calling it a wrap.

jhildenbiddle avatar Apr 22 '22 16:04 jhildenbiddle

@jhildenbiddle Hi, The idea you pointed out should be achievable, and the first ID in arrays must be a global site tag id, each page should have only one global site tag instance.

ekoz avatar Apr 24 '22 14:04 ekoz

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docsify-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 27, 2023 2:27am

vercel[bot] avatar Apr 24 '22 15:04 vercel[bot]

Just wanted to pop in and say thanks for this update. Much appreciated. I have a ga3 and GA4 property that I can test with if necessary 👍

shawaj avatar Apr 29 '22 01:04 shawaj

One other request: Please add tests for the plugin. Basic tests that verify that the script is being added to the DOM properly, that IDs match, and that the gtag calls are being made as expected is all we need.

Thanks @ekoz!

jhildenbiddle avatar May 17 '22 22:05 jhildenbiddle

@ekoz Ping! 😄

jhildenbiddle avatar May 23 '22 18:05 jhildenbiddle

@jhildenbiddle I use an HTML page under docsify directory to test

<html>
  <head>
    <title>ga test</title>
    <link rel="stylesheet" href="lib/themes/vue.css" />
  </head>
  <body>
    <div id="app"></div>
    <script>
      window.$docsify = {
        // ga: [
        //   'AW-XXXXXXXX', // Google Ads
        //   'DC-XXXXXXXX', // Floodlight
        //   'G-XXXXXXXX', // Google Analytics 4 (GA4)
        //   'UA-XXXXXXXX', // Google Universal Analytics (GA3)
        // ],
      };
    </script>
    <script src="lib/docsify.min.js" data-ga="G-XXXXXXXX"></script>
    <script src="lib/plugins/ga.min.js"></script>
  </body>
</html>

ekoz avatar May 24 '22 14:05 ekoz

@ekoz --

I use an HTML page under docsify directory to test

I was referring to an automated test within our /test/ directory. If you need help with this, I'm happy to lend a hand.

jhildenbiddle avatar May 24 '22 17:05 jhildenbiddle

@jhildenbiddle thanks, I read the docs https://playwright.dev/docs and have a try, run with npm run test:e2e ga.test.js.

How to test data-ga with playwright?

ekoz avatar May 25 '22 08:05 ekoz

@jhildenbiddle Ping! 😄

ekoz avatar Jun 21 '22 02:06 ekoz

can we merge this one?

JobaDiniz avatar May 11 '23 14:05 JobaDiniz