Skip to content

Commit

Permalink
feat: implement fathom
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhojang6 committed Jun 27, 2023
1 parent 8c0d5ee commit 966c6b3
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-fathom/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Change Log
38 changes: 38 additions & 0 deletions packages/docusaurus-fathom/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@acid-info/docusaurus-fathom",
"version": "1.0.0-alpha.1",
"description": "Docusaurus fathom plugin",
"main": "lib/index.js",
"types": "src/plugin.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/acid-info/logos-docusaurus-plugins.git",
"directory": "packages/docusaurus-fathom"
},
"license": "MIT",
"scripts": {
"build": "tsc --build",
"watch": "tsc --build --watch",
"prepublishOnly": "yarn build"
},
"dependencies": {
"@docusaurus/core": "^2.4.1",
"@docusaurus/module-type-aliases": "^2.4.1",
"@docusaurus/types": "^2.4.1",
"@docusaurus/utils": "^2.4.1",
"@docusaurus/utils-common": "^2.4.1",
"@docusaurus/utils-validation": "^2.4.1",
"lodash": "^4.17.21",
"node-html-parser": "^6.1.5",
"object-hash": "^3.0.0",
"satori": "^0.10.1",
"sharp": "^0.32.1"
},
"engines": {
"node": ">=16.14"
},
"devDependencies": {
"@types/lodash": "^4.14.186"
},
"gitHead": "d2ee08c6c0678f78ad70f5d04183f7f781d11563"
}
33 changes: 33 additions & 0 deletions packages/docusaurus-fathom/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { SITE_ID, SCRIPT_URL } from './options'

declare global {
interface Window {
fathom: any
}
}

;(function (f: any, a: Window, t: string, h: string) {
a[h] =
a[h] ||
function () {
;(a[h].q = a[h].q || []).push(arguments)
}
const o = f.createElement('script')
const m = f.getElementsByTagName('script')[0]
o.async = 1
o.src = t
o.id = 'fathom-script'
m.parentNode.insertBefore(o, m)
})(document, window, SCRIPT_URL, 'fathom')

const { fathom } = window as any
fathom('set', 'siteId', SITE_ID)
fathom('trackPageview')

console.log('PLUGIN_SITE_ID', SITE_ID)

export function onRouteDidUpdate({ location, previousLocation }) {
if (location.pathname !== previousLocation?.pathname) {
fathom('trackPageview')
}
}
2 changes: 2 additions & 0 deletions packages/docusaurus-fathom/src/client/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SITE_ID = ''
export const SCRIPT_URL = ''
4 changes: 4 additions & 0 deletions packages/docusaurus-fathom/src/client/types/plugin.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type PluginOptions = {
siteId: string
scriptUrl: string
}
Empty file.
25 changes: 25 additions & 0 deletions packages/docusaurus-fathom/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { LoadContext, Plugin } from '@docusaurus/types'
import { PluginOptions } from './client/types/plugin.types'
import path from 'path'
import fs from 'fs-extra'

export default function fathomPlugin(
context: LoadContext,
options: PluginOptions,
): Plugin<undefined> {
const { siteId, scriptUrl } = options
const dir = path.join(context.generatedFilesDir, 'docusaurus-fathom/default')
fs.ensureDirSync(dir)

fs.writeFileSync(
path.join(dir, 'options.ts'),
`export const SITE_ID = '${siteId}'\nexport const SCRIPT_URL = '${scriptUrl}'`,
)

return {
name: 'docusaurus-fathom',
getClientModules: () => [path.resolve(__dirname, './client/index.js')],
}
}

export { type PluginOptions }
1 change: 1 addition & 0 deletions packages/docusaurus-fathom/src/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type * from './index'
16 changes: 16 additions & 0 deletions packages/docusaurus-fathom/tsconfig.client.json
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",
"rootDir": "src",
"outDir": "lib",
"module": "esnext",
"target": "esnext",
"lib": ["DOM"]
},
"include": ["src/client", "src/*.d.ts"],
"exclude": ["**/__tests__/**"]
}
14 changes: 14 additions & 0 deletions packages/docusaurus-fathom/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"references": [{ "path": "./tsconfig.client.json" }],
"compilerOptions": {
"noEmit": false,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib",
"lib": ["DOM"]
},
"include": ["src"],
"exclude": ["src/theme", "**/__tests__/**"]
}
9 changes: 9 additions & 0 deletions packages/docusaurus-playground/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const config = {
],
],

// clientModules: ['./src/fathom.ts'],

plugins: [
[
'@docusaurus/plugin-content-blog',
Expand All @@ -69,6 +71,13 @@ const config = {
authorsMapPath: 'authors.yml',
}),
],
[
'@acid-info/docusaurus-fathom',
{
siteId: 'SIDE_ID_2',
scriptUrl: 'SCRIPT_URL_2',
},
],
],

themeConfig:
Expand Down
33 changes: 33 additions & 0 deletions packages/docusaurus-playground/src/fathom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const SCRIPT_URL = '//fathom.status.im/tracker.js'
const SITE_ID = 'FUTMI'

declare global {
interface Window {
fathom: any
}
}

;(function (f: any, a: Window, t: string, h: string) {
a[h] =
a[h] ||
function () {
;(a[h].q = a[h].q || []).push(arguments)
}
const o = f.createElement('script')
const m = f.getElementsByTagName('script')[0]
o.async = 1
o.src = t
o.id = 'fathom-script'
m.parentNode.insertBefore(o, m)
})(document, window, SCRIPT_URL, 'fathom')

const { fathom } = window as any
fathom('set', 'siteId', SITE_ID)
fathom('trackPageview')

export function onRouteDidUpdate({ location, previousLocation }) {
console.log('vvxcvxcv')
if (location.pathname !== previousLocation?.pathname) {
fathom('trackPageview')
}
}
1 change: 1 addition & 0 deletions packages/logos-docusaurus-preset/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function logosPreset(
context: LoadContext,
options: PluginOptions,
): Preset {
//@ts-ignore
const docsEnabled = options.docs !== false

const siteConfig: typeof context.siteConfig = defaultsDeep(
Expand Down

0 comments on commit 966c6b3

Please sign in to comment.