├── package.json ├── src ├── statcounter.js └── index.js └── README.md /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "src/index.js", 3 | "name": "@jsplumb/docusaurus-plugin-statcounter", 4 | "version": "1.0.1" 5 | } 6 | -------------------------------------------------------------------------------- /src/statcounter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) jsPlumb Pty Ltd 3 | */ 4 | 5 | import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment'; 6 | 7 | export default (function () { 8 | if (!ExecutionEnvironment.canUseDOM) { 9 | return null; 10 | } 11 | 12 | return { 13 | onRouteUpdate({location}) { 14 | if (typeof _statcounter !== "undefined") { 15 | _statcounter.record_pageview() 16 | } 17 | }, 18 | }; 19 | })(); 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docusaurus-plugin-statcounter 2 | A docusaurus plugin offering integration with StatCounter 3 | 4 | **This plugin is for v2 of Docusaurus** 5 | 6 | ## Installation 7 | 8 | ```bash 9 | npm i @jsplumb/docusaurus-plugin-statcounter 10 | ``` 11 | 12 | ## Configuration 13 | 14 | - Add the plugin to the `plugins` array in your `docusaurus.config.js`: 15 | 16 | ```javascript 17 | plugins:[ 18 | 19 | "@jsplumb/docusaurus-plugin-statcounter", 20 | 21 | ], 22 | ``` 23 | 24 | - Add a `statCounter` entry to the `themeConfig` section of your `docusaurus.config.js`: 25 | 26 | ```javascript 27 | themeConfig: { 28 | 29 | statCounter:{ 30 | projectId:"PROJECT ID", 31 | securityCode:"SECURITY CODE" 32 | }, 33 | 34 | } 35 | ``` 36 | 37 | `projectId` and `securityCode` are available in the Statcounter console. 38 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) jsPlumb Pty Ltd 3 | */ 4 | 5 | const path = require('path'); 6 | 7 | module.exports = function (context) { 8 | const {siteConfig} = context; 9 | const {themeConfig} = siteConfig; 10 | const {statCounter} = themeConfig || {}; 11 | 12 | if (!statCounter) { 13 | throw new Error( 14 | `You need to provide a "statCounter" object in the "themeConfig" to use @jsplumb/docusaurus-plugin-statcounter.` 15 | ); 16 | } 17 | 18 | const {projectId, securityCode} = statCounter; 19 | 20 | if (!projectId) { 21 | throw new Error(`The statcounter plugin requires a "projectId" to be set`) 22 | } 23 | 24 | if (!securityCode) { 25 | throw new Error(`The statcounter plugin requires a "securityCode" to be set`) 26 | } 27 | 28 | return { 29 | name: 'docusaurus-plugin-statcounter', 30 | 31 | getClientModules() { 32 | return [path.resolve(__dirname, './statcounter')] 33 | }, 34 | 35 | injectHtmlTags() { 36 | 37 | return { 38 | headTags:[ 39 | { 40 | tagName:'script', 41 | innerHTML: ` 42 | var sc_project="${projectId}"; 43 | var sc_invisible=1; 44 | var sc_security="${securityCode}"; 45 | ` 46 | }, 47 | { 48 | tagName:'script', 49 | attributes:{ 50 | src:"https://www.statcounter.com/counter/counter.js" 51 | } 52 | } 53 | ] 54 | } 55 | } 56 | }; 57 | }; 58 | --------------------------------------------------------------------------------