feat(core): rework client modules lifecycles, officially make API public (#6732)

This commit is contained in:
Joshua Chen 2022-04-29 21:11:20 +08:00 committed by GitHub
parent 2429bfbd59
commit ae788c536f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 259 additions and 126 deletions

View file

@ -5,21 +5,19 @@
* LICENSE file in the root directory of this source tree.
*/
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import type {ClientModule} from '@docusaurus/types';
export default (function analyticsModule() {
if (!ExecutionEnvironment.canUseDOM) {
return null;
}
return {
onRouteUpdate({location}: {location: Location}) {
const clientModule: ClientModule = {
onRouteDidUpdate({location, previousLocation}) {
if (previousLocation && location.pathname !== previousLocation.pathname) {
// 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');
},
};
})();
}
},
};
export default clientModule;