mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 08:07:26 +02:00
feat(v2): implement Google analytics plugin
This commit is contained in:
parent
7375789e46
commit
cfffad8c6d
8 changed files with 138 additions and 17 deletions
16
packages/docusaurus-plugin-google-analytics/package.json
Normal file
16
packages/docusaurus-plugin-google-analytics/package.json
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"name": "@docusaurus/plugin-google-analytics",
|
||||||
|
"version": "2.0.0-alpha.19",
|
||||||
|
"description": "Global analytics (analytics.js) plugin for Docusaurus",
|
||||||
|
"main": "src/index.js",
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"peerDependencies": {
|
||||||
|
"@docusaurus/core": "^2.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8"
|
||||||
|
}
|
||||||
|
}
|
68
packages/docusaurus-plugin-google-analytics/src/analytics.js
Normal file
68
packages/docusaurus-plugin-google-analytics/src/analytics.js
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2017-present, Facebook, Inc.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import siteConfig from '@generated/docusaurus.config';
|
||||||
|
|
||||||
|
const {themeConfig} = siteConfig;
|
||||||
|
|
||||||
|
export default (function() {
|
||||||
|
if (!themeConfig.googleAnalytics) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const {trackingID} = themeConfig.googleAnalytics;
|
||||||
|
if (process.env.NODE_ENV === 'development' && !trackingID) {
|
||||||
|
console.warn(
|
||||||
|
'You specified the `googleAnalytics` object in `themeConfig` but the `trackingID` field was missing. ' +
|
||||||
|
'Please ensure this is not a mistake.',
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
// process.env.NODE_ENV !== 'production' || // TODO: Add it back after testing that it works.
|
||||||
|
!trackingID ||
|
||||||
|
typeof window === 'undefined'
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* eslint-disable */
|
||||||
|
(function(i, s, o, g, r, a, m) {
|
||||||
|
i['GoogleAnalyticsObject'] = r;
|
||||||
|
(i[r] =
|
||||||
|
i[r] ||
|
||||||
|
function() {
|
||||||
|
(i[r].q = i[r].q || []).push(arguments);
|
||||||
|
}),
|
||||||
|
(i[r].l = 1 * new Date());
|
||||||
|
(a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]);
|
||||||
|
a.async = 1;
|
||||||
|
a.src = g;
|
||||||
|
m.parentNode.insertBefore(a, m);
|
||||||
|
})(
|
||||||
|
window,
|
||||||
|
document,
|
||||||
|
'script',
|
||||||
|
'https://www.google-analytics.com/analytics.js',
|
||||||
|
'ga',
|
||||||
|
);
|
||||||
|
/* eslint-enable */
|
||||||
|
|
||||||
|
window.ga('create', trackingID, 'auto');
|
||||||
|
window.ga('send', 'pageview');
|
||||||
|
|
||||||
|
return {
|
||||||
|
onRouteUpdate({location}) {
|
||||||
|
// Set page so that subsequent hits on this page are attributed
|
||||||
|
// to this page. This is recommended for Single-page Applications.
|
||||||
|
window.ga('set', 'page', location.pathname);
|
||||||
|
// Always refer to the variable on window in-case it gets overridden elsewhere.
|
||||||
|
window.ga('send', 'pageview');
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})();
|
18
packages/docusaurus-plugin-google-analytics/src/index.js
Normal file
18
packages/docusaurus-plugin-google-analytics/src/index.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2017-present, Facebook, Inc.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = function() {
|
||||||
|
return {
|
||||||
|
name: 'docusaurus-plugin-google-analytics',
|
||||||
|
|
||||||
|
getClientModules() {
|
||||||
|
return [path.resolve(__dirname, './analytics')];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
|
@ -48,7 +48,10 @@ export default (function() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
onRouteUpdate({location}) {
|
onRouteUpdate({location}) {
|
||||||
gtag('event', 'page_view', {page_path: location.pathname});
|
// Always refer to the variable on window in-case it gets overridden elsewhere.
|
||||||
|
window.gtag('event', 'page_view', {
|
||||||
|
page_path: location.pathname,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -9,7 +9,7 @@ const path = require('path');
|
||||||
|
|
||||||
module.exports = function() {
|
module.exports = function() {
|
||||||
return {
|
return {
|
||||||
name: 'docusaurus-plugin-gtag',
|
name: 'docusaurus-plugin-google-gtag',
|
||||||
|
|
||||||
getClientModules() {
|
getClientModules() {
|
||||||
return [path.resolve(__dirname, './gtag')];
|
return [path.resolve(__dirname, './gtag')];
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
module.exports = function preset(context, opts = {}) {
|
module.exports = function preset(context, opts = {}) {
|
||||||
const {siteConfig = {}} = context;
|
const {siteConfig = {}} = context;
|
||||||
const {themeConfig} = siteConfig;
|
const {themeConfig} = siteConfig;
|
||||||
const {algolia} = themeConfig;
|
const {algolia, googleAnalytics, gtag} = themeConfig;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
themes: [
|
themes: [
|
||||||
|
@ -20,7 +20,8 @@ module.exports = function preset(context, opts = {}) {
|
||||||
['@docusaurus/plugin-content-docs', opts.docs],
|
['@docusaurus/plugin-content-docs', opts.docs],
|
||||||
['@docusaurus/plugin-content-blog', opts.blog],
|
['@docusaurus/plugin-content-blog', opts.blog],
|
||||||
['@docusaurus/plugin-content-pages', opts.pages],
|
['@docusaurus/plugin-content-pages', opts.pages],
|
||||||
['@docusaurus/plugin-google-gtag', opts.gtag],
|
googleAnalytics && '@docusaurus/plugin-google-analytics',
|
||||||
|
gtag && '@docusaurus/plugin-google-gtag',
|
||||||
['@docusaurus/plugin-sitemap', opts.sitemap],
|
['@docusaurus/plugin-sitemap', opts.sitemap],
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import _ from 'lodash';
|
||||||
import {generate} from '@docusaurus/utils';
|
import {generate} from '@docusaurus/utils';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import importFresh from 'import-fresh';
|
import importFresh from 'import-fresh';
|
||||||
|
@ -28,19 +29,30 @@ export async function loadPlugins({
|
||||||
pluginsRouteConfigs: RouteConfig[];
|
pluginsRouteConfigs: RouteConfig[];
|
||||||
}> {
|
}> {
|
||||||
// 1. Plugin Lifecycle - Initialization/Constructor
|
// 1. Plugin Lifecycle - Initialization/Constructor
|
||||||
const plugins: Plugin<any>[] = pluginConfigs.map(pluginItem => {
|
const plugins: Plugin<any>[] = _.compact(
|
||||||
let pluginModuleImport;
|
pluginConfigs.map(pluginItem => {
|
||||||
let pluginOptions = {};
|
let pluginModuleImport;
|
||||||
if (typeof pluginItem === 'string') {
|
let pluginOptions = {};
|
||||||
pluginModuleImport = pluginItem;
|
if (!pluginItem) {
|
||||||
} else if (Array.isArray(pluginItem)) {
|
return null;
|
||||||
pluginModuleImport = pluginItem[0];
|
}
|
||||||
pluginOptions = pluginItem[1] || {};
|
|
||||||
}
|
if (typeof pluginItem === 'string') {
|
||||||
// module is any valid module identifier - npm package or locally-resolved path.
|
pluginModuleImport = pluginItem;
|
||||||
const pluginModule = importFresh(pluginModuleImport);
|
} else if (Array.isArray(pluginItem)) {
|
||||||
return pluginModule(context, pluginOptions);
|
pluginModuleImport = pluginItem[0];
|
||||||
});
|
pluginOptions = pluginItem[1] || {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pluginModuleImport) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// module is any valid module identifier - npm package or locally-resolved path.
|
||||||
|
const pluginModule = importFresh(pluginModuleImport);
|
||||||
|
return pluginModule(context, pluginOptions);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
// 2. Plugin lifecycle - loadContent
|
// 2. Plugin lifecycle - loadContent
|
||||||
// Currently plugins run lifecycle in parallel and are not order-dependent. We could change
|
// Currently plugins run lifecycle in parallel and are not order-dependent. We could change
|
||||||
|
|
|
@ -36,6 +36,9 @@ module.exports = {
|
||||||
gtag: {
|
gtag: {
|
||||||
trackingID: 'UA-141789564-1',
|
trackingID: 'UA-141789564-1',
|
||||||
},
|
},
|
||||||
|
// googleAnalytics: {
|
||||||
|
// trackingID: 'UA-141789564-1',
|
||||||
|
// },
|
||||||
algolia: {
|
algolia: {
|
||||||
apiKey: '47ecd3b21be71c5822571b9f59e52544',
|
apiKey: '47ecd3b21be71c5822571b9f59e52544',
|
||||||
indexName: 'docusaurus-2',
|
indexName: 'docusaurus-2',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue