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

feat: add google analytics gtag.js plugin #1702

Merged
merged 19 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function buildAllPlugin() {
var plugins = [
{name: 'search', input: 'search/index.js'},
{name: 'ga', input: 'ga.js'},
{name: 'gtag', input: 'gtag.js'},
{name: 'matomo', input: 'matomo.js'},
{name: 'emoji', input: 'emoji.js'},
{name: 'external-script', input: 'external-script.js'},
Expand Down
21 changes: 21 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ Configure by `data-ga`.
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/ga.min.js"></script>
```

## Google Universal Analytics (gtag.js)

Usage is same as `Google Analytics`.

```html
<script>
window.$docsify = {
ga: 'UA-XXXXX-Y'
}
</script>
<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/gtag.min.js"></script>
```

Configure by `data-ga`.

```html
<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js" data-ga="UA-XXXXX-Y"></script>
jhildenbiddle marked this conversation as resolved.
Show resolved Hide resolved
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/gtag.min.js"></script>
```

## Emoji

Renders a larger collection of emoji shorthand codes. Without this plugin, Docsify is able to render only a limited number of emoji shorthand codes.
Expand Down
47 changes: 47 additions & 0 deletions src/plugins/gtag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-console */
// copy from ./ga.js
// usage is the same as ./ga.js
function appendScript(id) {
const script = document.createElement('script');
script.async = true;
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + id;
document.body.appendChild(script);
}

function init(id) {
appendScript(id);

window.dataLayer = window.dataLayer || [];
window.gtag =
window.gtag ||
function () {
window.dataLayer.push(arguments);
};

window.gtag('js', new Date());
window.gtag('config', id);
}

function collect() {
if (!window.ga) {
init($docsify.ga);
}

// usage: https://developers.google.com/analytics/devguides/collection/gtagjs/pages
window.gtag('event', 'page_view', {
page_title: document.title,
page_location: location.href,
page_path: location.pathname,
});
}

const install = function (hook) {
if (!$docsify.ga) {
console.error('[Docsify] ga is required.');
return;
}

hook.beforeEach(collect);
};

$docsify.plugins = [].concat(install, $docsify.plugins);