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(plugin-vercel-analytics): add new vercel analytics plugin #9687

Merged
merged 23 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/docusaurus-plugin-vercel-analytics/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.tsbuildinfo*
tsconfig*
__tests__
9 changes: 9 additions & 0 deletions packages/docusaurus-plugin-vercel-analytics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `@docusaurus/plugin-vercel-analytics`

Vercel analytics plugin for Docusaurus. [Official documentation](https://vercel.com/docs/analytics)
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved

## Usage

TODO: add documentation

See [plugin-vercel-analytics documentation]().
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need plugin docs

35 changes: 35 additions & 0 deletions packages/docusaurus-plugin-vercel-analytics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@docusaurus/plugin-vercel-analytics",
"version": "3.0.0",
"description": "Global vercel analytics plugin for Docusaurus.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc --build",
"watch": "tsc --build --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/facebook/docusaurus.git",
"directory": "packages/docusaurus-plugin-vercel-analytics"
},
"license": "MIT",
"dependencies": {
"@docusaurus/core": "3.0.0",
"@docusaurus/types": "3.0.0",
"@docusaurus/utils-validation": "3.0.0",
"@vercel/analytics": "^1.1.1",
"webpack": "^5.88.1",
"tslib": "^2.6.0"
},
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"engines": {
"node": ">=18.0"
}
}
63 changes: 63 additions & 0 deletions packages/docusaurus-plugin-vercel-analytics/src/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {inject} from '@vercel/analytics';
import globalData from '@generated/globalData';
import type {PluginOptions} from '@docusaurus/plugin-vercel-analytics';
// import {Joi} from '@docusaurus/utils-validation';
// import type {
// OptionValidationContext,
// ThemeConfig,
// ThemeConfigValidationContext,
// } from '@docusaurus/types';

const {debug, mode} = globalData['docusaurus-plugin-vercel-analytics']
?.default as PluginOptions;

slorber marked this conversation as resolved.
Show resolved Hide resolved
inject({
mode,
debug,
});

// export type Options = {
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
// trackingID: string | [string, ...string[]];
// anonymizeIP?: boolean;
// };

// export const DEFAULT_OPTIONS: Partial<PluginOptions> = {
// anonymizeIP: false,
// };

// const pluginOptionsSchema = Joi.object<PluginOptions>({
// // We normalize trackingID as a string[]
// trackingID: Joi.alternatives()
// .try(
// Joi.alternatives().conditional(Joi.string().required(), {
// then: Joi.custom((val: boolean) => [val]),
// }),
// Joi.array().items(Joi.string().required()),
// )
// .required(),
// anonymizeIP: Joi.boolean().default(DEFAULT_OPTIONS.anonymizeIP),
// });

// export function validateOptions({
// validate,
// options,
// }: OptionValidationContext<Options, PluginOptions>): PluginOptions {
// return validate(pluginOptionsSchema, options);
// }

// export function validateThemeConfig({
// themeConfig,
// }: ThemeConfigValidationContext<ThemeConfig>): ThemeConfig {
// if ('gtag' in themeConfig) {
// throw new Error(
// 'The "gtag" field in themeConfig should now be specified as option for plugin-google-gtag. More information at https://github.com/facebook/docusaurus/pull/5832.',
// );
// }
// return themeConfig;
// }
28 changes: 28 additions & 0 deletions packages/docusaurus-plugin-vercel-analytics/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import type {LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions} from '@docusaurus/plugin-vercel-analytics';

export default function pluginVercelAnalytics(
context: LoadContext,
options: PluginOptions,
): Plugin {
const isProd = process.env.NODE_ENV === 'production';

return {
name: 'docusaurus-plugin-vercel-analytics',

getClientModules() {
return isProd ? ['./analytics'] : [];
},

contentLoaded({actions}) {
actions.setGlobalData(options);
},
};
}
6 changes: 6 additions & 0 deletions packages/docusaurus-plugin-vercel-analytics/src/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

declare module '@docusaurus/plugin-vercel-analytics' {
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
export type PluginOptions = {
/**
* Turn debug mode on
*/
debug?: boolean;
/**
* TODO add description
*/
mode: 'auto' | 'development' | 'production';
};

export type Options = Partial<PluginOptions>;
}
8 changes: 8 additions & 0 deletions packages/docusaurus-plugin-vercel-analytics/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/// <reference types="@docusaurus/module-type-aliases" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": false,
"composite": true,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo-client",
"moduleResolution": "bundler",
"module": "esnext",
"target": "esnext",
"rootDir": "src",
"outDir": "lib"
},
"include": ["src/analytics.ts", "src/*.d.ts"],
"exclude": ["**/__tests__/**"]
}
13 changes: 13 additions & 0 deletions packages/docusaurus-plugin-vercel-analytics/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"references": [{"path": "./tsconfig.client.json"}],
"compilerOptions": {
"noEmit": false,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib"
},
"include": ["src"],
"exclude": ["src/analytics.ts", "**/__tests__/**"]
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3863,6 +3863,13 @@
resolved "https://registry.yarnpkg.com/@ungap/structured-clone/-/structured-clone-1.2.0.tgz#756641adb587851b5ccb3e095daf27ae581c8406"
integrity sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==

"@vercel/analytics@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@vercel/analytics/-/analytics-1.1.1.tgz#2a712378a95014a548b4f9d2ae1ea0721433908d"
integrity sha512-+NqgNmSabg3IFfxYhrWCfB/H+RCUOCR5ExRudNG2+pcRehq628DJB5e1u1xqwpLtn4pAYii4D98w7kofORAGQA==
dependencies:
server-only "^0.0.1"

"@webassemblyjs/ast@1.11.6", "@webassemblyjs/ast@^1.11.5":
version "1.11.6"
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.11.6.tgz#db046555d3c413f8966ca50a95176a0e2c642e24"
Expand Down Expand Up @@ -14718,6 +14725,11 @@ serve-static@1.15.0:
parseurl "~1.3.3"
send "0.18.0"

server-only@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/server-only/-/server-only-0.0.1.tgz#0f366bb6afb618c37c9255a314535dc412cd1c9e"
integrity sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA==

set-blocking@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
Expand Down