From b261902a06e326d37c866dfdcae48a88eda4997b Mon Sep 17 00:00:00 2001 From: Till Sanders Date: Fri, 26 Feb 2021 00:13:16 +0100 Subject: [PATCH 1/2] Support new configuration styles Nuxt.js moves towards top-level and runtime config. Signed-off-by: Till Sanders --- README.md | 50 ++++++++++++++++++++++++++++++++++++++--------- lib/module.js | 46 ++++++++++++++++++------------------------- package-lock.json | 2 +- package.json | 3 ++- types/index.d.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 38 deletions(-) create mode 100644 types/index.d.ts diff --git a/README.md b/README.md index a71178c..abfbda4 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,23 @@ # Nuxt Fathom -Implement Fathom analytics in your Nuxt app. +Implement [Fathom Analytics](https://usefathom.com/) in your Nuxt app. -## Quick Install +## Installation ``` -$ npm i @yabhq/nuxt-fathom +$ npm i -D @yabhq/nuxt-fathom ``` -Configure the module in your **nuxt.config.js**: +## Configuration + +Configure the module in your `nuxt.config.js`: ```JavaScript -modules: [ - // .... - ['@yabhq/nuxt-fathom', { +export default { + buildModules: [ + '@yabhq/nuxt-fathom' + ], + fathom: { siteId: 'XXXXXX', // required // Advanced configuration @@ -40,7 +44,35 @@ modules: [ spa: 'auto', // optional, defaults to 'auto' - }], - // .... + }, ] ``` + +### Runtime Config + +You can also use the [Runtime Config](https://nuxtjs.org/guide/runtime-config): + +```JavaScript +export default { + // ... + publicRuntimeConfig: { + fathom: { + siteId: process.env.FATHOM_SITE_ID, + } + } +} +``` + +### TypeScript + +You can benefit from types inside your `nuxt.config.ts` by adding the module to your +`tsconfig.json`: + +```json + "types": [ + "@nuxt/types", + "@types/node", + // ... + "@yabhq/nuxt-fathom" + ] +``` \ No newline at end of file diff --git a/lib/module.js b/lib/module.js index 93dcff1..6fdd46b 100644 --- a/lib/module.js +++ b/lib/module.js @@ -1,32 +1,24 @@ const { resolve } = require('path') -function nuxtFathom(options) { - if ( typeof options.auto !== 'boolean' ) { - options.auto = true - } - - if ( typeof options.canonical !== 'boolean' ) { - options.canonical = true - } - - if ( !options.excludedDomains ) { - options.excludedDomains = '' - } - - if ( typeof options.honorDoNotTrack !== 'boolean' ) { - options.honorDoNotTrack = false - } - - if ( !options.includedDomains ) { - options.includedDomains = '' - } - - if ( !options.spa ) { - options.spa = 'auto' - } - - if ( !options.scriptSrc ) { - options.scriptSrc = 'https://cdn.usefathom.com/tracker.js' +function nuxtFathom(moduleOptions) { + const { nuxt } = this + + const defaultOptions = { + auto: true, + canonical: true, + excludedDomains: '', + honorDoNotTrack: false, + includedDomains: '', + spa: 'auto', + scriptSrc: 'https://cdn.usefathom.com/tracker.js' + } + + // Combine options + const options = { + ...defaultOptions, + ...nuxt.options.fathom, + ...moduleOptions, + ...(nuxt.options.publicRuntimeConfig && nuxt.options.publicRuntimeConfig.fathom) } // Disable if siteId missing diff --git a/package-lock.json b/package-lock.json index 871aa63..5f70964 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { "name": "@yabhq/nuxt-fathom", - "version": "1.1.0", + "version": "2.1.0", "lockfileVersion": 1 } diff --git a/package.json b/package.json index d535a6a..995783e 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "@yabhq/nuxt-fathom", - "version": "2.0.0", + "version": "2.1.0", "description": "NuxtJS module for Fathom Analytics", "main": "lib/module.js", + "types": "types/index.d.ts", "directories": { "lib": "lib" }, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..664df9f --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,48 @@ +declare module '@nuxt/types' { + interface NuxtOptions { + fathom?: { + /** + * ID of your site as assigned by Fathom Analytics. Optional here, but you have to set it + * either by module options or runtime config. + */ + siteId?: string + + /** + * Automatically track page views. Optional, defaults to `true`. + */ + auto?: boolean + + /** + * Use the canonical URL (if any) instead of the current URL. Optional, defaults to `true`. + */ + canonical?: boolean + + /** + * Blacklist one or multiple domains (separate by comma) to exclude them from tracking. + * Optional. + */ + excludedDomains?: string + + /** + * Fathom does not track anything personal or identifiable. If you want to honor DNT regardless, + * you can. Optional, defaults to `false`. + */ + honorDoNotTrack?: boolean + + /** + * Whitelist one or multiple domains (separate by comma) to only track there. Optional. + */ + includedDomains?: string + + /** + * Set the navigation mode. Optional, defaults to `'auto'`. + */ + spa?: 'auto' | 'history' | 'hash' + + /** + * Support custom domains. Optional, defaults to `'https://cdn.usefathom.com/tracker.js'`. + */ + scriptSrc?: string + } + } +} \ No newline at end of file From a07b31f48a92c21bdbbf6b1232bcb9f96432b656 Mon Sep 17 00:00:00 2001 From: Till Sanders Date: Fri, 26 Feb 2021 00:16:54 +0100 Subject: [PATCH 2/2] Fix invalid JSON in readme Signed-off-by: Till Sanders --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index abfbda4..1d243f1 100644 --- a/README.md +++ b/README.md @@ -70,9 +70,6 @@ You can benefit from types inside your `nuxt.config.ts` by adding the module to ```json "types": [ - "@nuxt/types", - "@types/node", - // ... "@yabhq/nuxt-fathom" ] ``` \ No newline at end of file