nuxt-umami
Advanced tools
Comparing version 2.0.3 to 2.1.0
@@ -21,14 +21,12 @@ import type { PreflightResult } from './types'; | ||
function screamer(id: ErrorId, raw?: any) { | ||
const { level, text } = warnings[id]; | ||
// eslint-disable-next-line no-console | ||
console[level](`[UMAMI]: ${text}`, '\n'); | ||
// eslint-disable-next-line no-console | ||
raw && (console[level](raw)); | ||
} | ||
const helloDebugger = process.env.NODE_ENV === 'production' | ||
? (id: ErrorId, raw?: any) => {} | ||
: (id: ErrorId, raw?: any) => { | ||
const { level, text } = warnings[id]; | ||
// eslint-disable-next-line no-console | ||
console[level](`[UMAMI]: ${text}`, '\n'); | ||
// eslint-disable-next-line no-console | ||
raw && (console[level](raw)); | ||
}; | ||
function silencio(id: ErrorId, raw?: any) {} | ||
const helloDebugger = process.env.NODE_ENV === 'production' ? silencio : screamer; | ||
export { helloDebugger }; |
@@ -26,10 +26,2 @@ interface BasicPayload { | ||
interface PreflightArgs { | ||
ignoreDnt?: boolean | ||
domains?: string | ||
id?: string | ||
host?: string | ||
ignoreLocal?: boolean | ||
} | ||
interface ServerPayload { | ||
@@ -53,5 +45,4 @@ type: PayloadType | ||
ServerPayload, | ||
PreflightArgs, | ||
PreflightResult, | ||
GetPayloadReturn, | ||
}; |
import type { | ||
GetPayloadReturn, | ||
PartialPayload, | ||
PreflightArgs, | ||
PreflightResult, | ||
@@ -14,4 +13,2 @@ ServerPayload, | ||
function assert(value: unknown): asserts value {} | ||
function isInvalidHost(host: unknown): host is string { | ||
@@ -29,5 +26,31 @@ try { | ||
function preflight( | ||
{ ignoreDnt, domains, id, host, ignoreLocal }: PreflightArgs, | ||
): PreflightResult { | ||
const umConfig = computed(() => { | ||
const { public: { umamiHost, umamiId } } = useRuntimeConfig(); | ||
const { | ||
umami: { | ||
host = '', | ||
id = '', | ||
domains = undefined, | ||
ignoreDnt = true, | ||
ignoreLocalhost: ignoreLocal = false, | ||
autoTrack = true, | ||
version = 1, | ||
} = {}, | ||
} = useAppConfig(); | ||
return { | ||
host: umamiHost || host, | ||
id: umamiId || id, | ||
domains, | ||
ignoreDnt, | ||
ignoreLocal, | ||
autoTrack, | ||
version, | ||
}; | ||
}); | ||
function preflight(): PreflightResult { | ||
const { ignoreDnt, domains, id, host, ignoreLocal } = umConfig.value; | ||
if (typeof window === 'undefined') { | ||
@@ -54,5 +77,7 @@ return 'ssr'; | ||
const domainList = isValidString(domains) | ||
? domains.split(',').map(d => d.trim()) | ||
: undefined; | ||
const domainList = (Array.isArray(domains) && domains.length) | ||
? domains | ||
: isValidString(domains) | ||
? domains.split(',').map(d => d.trim()) | ||
: undefined; | ||
@@ -101,3 +126,3 @@ if (domainList && !domainList.includes(hostname)) { | ||
async function collect(load: ServerPayload) { | ||
const { umami: { host, version } } = useAppConfig(); | ||
const { host, version } = umConfig.value; | ||
const root = new URL(host); | ||
@@ -124,2 +149,2 @@ const branch = version === 2 ? 'api/send' : 'api/collect'; | ||
export { preflight, getPayload, assert, collect }; | ||
export { preflight, getPayload, collect, umConfig }; |
@@ -0,5 +1,6 @@ | ||
import { umConfig } from '../internal/utils'; | ||
import { umTrackView } from '../utils/umami'; | ||
export default defineNuxtRouteMiddleware((to) => { | ||
const { umami: { autoTrack } = {} } = useAppConfig(); | ||
const { autoTrack } = umConfig.value; | ||
@@ -6,0 +7,0 @@ if (autoTrack) { |
@@ -13,2 +13,8 @@ export default defineNuxtConfig({ | ||
}, | ||
runtimeConfig: { | ||
public: { | ||
umamiHost: '', | ||
umamiId: '', | ||
}, | ||
}, | ||
}); | ||
@@ -35,9 +41,10 @@ | ||
* Configure the tracker to only run on specific domains. | ||
* Provide a comma delimited list of domains (without 'http'). | ||
* Provide an array or comma delimited list of domains (without 'http'). | ||
* Leave as `undefined` to run on all domains. | ||
* | ||
* @example `mywebsite.com, mywebsite2.com` | ||
* @example ['mywebsite.com', 'mywebsite2.com'] | ||
* @default undefined | ||
*/ | ||
domains?: string | ||
domains?: string | string[] | ||
/** | ||
@@ -77,3 +84,3 @@ * Whether to ignore browsers' Do Not Track setting. | ||
autoTrack?: boolean | ||
domains?: string | ||
domains?: string | string[] | ||
ignoreDnt?: boolean | ||
@@ -80,0 +87,0 @@ ignoreLocalhost?: boolean |
{ | ||
"name": "nuxt-umami", | ||
"type": "module", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "Integrate Umami Analytics into Nuxt", | ||
@@ -6,0 +6,0 @@ "author": "ML <me.mlaure@gmail.com>", |
@@ -26,2 +26,6 @@ # Nuxt Umami <sup>@next<sup> | ||
### 🚀 Try it online | ||
<a href="https://stackblitz.com/edit/nuxt-umami-next"><img src="https://developer.stackblitz.com/img/open_in_stackblitz.svg" alt="Open in StackBlitz"></a> | ||
### Step 1: Install and add to Nuxt | ||
@@ -89,3 +93,3 @@ | ||
Use it? | ||
Use it. | ||
@@ -96,10 +100,22 @@ ## Configuration | ||
|---|---|---|---|---| | ||
| host | string | Your Umami endpoint. This is where your script is hosted. Eg: `https://ijkml.xyz/`. | yes | '' | | ||
| id | string | Unique website-id provided by Umami. | yes | '' | | ||
| domains | string | Limit tracker to specific domains by providing a comma-separated list (without 'http'). Leave blank for all domains. | no | '' | | ||
| ignoreDnt | boolean | Option to ignore browsers' Do Not Track setting. | no | true | | ||
| autoTrack | boolean | Option to automatically track page views. | no | true | | ||
| ignoreLocalhost | boolean | Option to prevent tracking on localhost. | no | false | | ||
| host | `string` | Your Umami endpoint. This is where your script is hosted. Eg: `https://ijkml.xyz/`. | yes | `''` | | ||
| id | `string` | Unique website-id provided by Umami. | yes | `''` | | ||
| domains | `string \| Array<string>` | Limit tracker to specific domains by providing an array or comma-separated list (without 'http'). Leave blank for all domains. | no | `undefined` | | ||
| ignoreDnt | `boolean` | Option to ignore browsers' Do Not Track setting. | no | `true` | | ||
| autoTrack | `boolean` | Option to automatically track page views. | no | `true` | | ||
| ignoreLocalhost | `boolean` | Option to prevent tracking on localhost. | no | `false` | | ||
| version | `1 \| 2` | Umami version (Cloud) | no | `1` | | ||
### Environment Variables | ||
> **Note**: | ||
> Available in `^2.1.0` and takes precedence over `appConfig`. | ||
You can provide the `host` and `id` config as environment variables. Simply add `NUXT_PUBLIC_UMAMI_HOST` and `NUXT_PUBLIC_UMAMI_ID` to your `.env` file, and that's it. | ||
```sh | ||
NUXT_PUBLIC_UMAMI_HOST="https://domain.tld" | ||
NUXT_PUBLIC_UMAMI_ID="abc123-456def-ghi789" | ||
``` | ||
## Usage | ||
@@ -112,4 +128,4 @@ | ||
- `umTrackView(url, referrer)` | ||
- `url`: the path being tracked, eg `/about`, `/contact?by=phone#office`. *Most times*, this can be correctly inferred. Equivalent of `router.fullPath`. | ||
- `referrer`: the page referrer. *Most times*, this can be correctly inferred. Equivalent of `document.referrer`. | ||
- `url`: the path being tracked, eg `/about`, `/contact?by=phone#office`. This can be correctly inferred. Equivalent of `router.fullPath`. | ||
- `referrer`: the page referrer. This can be correctly inferred. Equivalent of `document.referrer`. | ||
@@ -116,0 +132,0 @@ - `umTrackEvent(eventName, eventData)` |
@@ -1,2 +0,2 @@ | ||
import { assert, collect, getPayload, preflight } from '../internal/utils'; | ||
import { collect, getPayload, preflight, umConfig } from '../internal/utils'; | ||
import type { EventData, EventPayload, ViewPayload } from '../internal/types'; | ||
@@ -13,12 +13,4 @@ import { helloDebugger } from '../internal/debug'; | ||
function trackView(url?: string, referrer?: string): void { | ||
const { | ||
umami: { | ||
id, host, domains, | ||
ignoreDnt = false, | ||
ignoreLocalhost: ignoreLocal = false, | ||
} = {}, | ||
} = useAppConfig(); | ||
const check = preflight(); | ||
const check = preflight({ domains, ignoreDnt, id, host, ignoreLocal }); | ||
if (check === 'ssr') { | ||
@@ -33,4 +25,3 @@ return; | ||
assert(typeof id === 'string'); | ||
const { id } = umConfig.value; | ||
const { pageReferrer, pageUrl, payload } = getPayload(); | ||
@@ -58,12 +49,4 @@ | ||
function trackEvent(eventName: string, eventData?: EventData) { | ||
const { | ||
umami: { | ||
id, host, domains, | ||
ignoreDnt = false, | ||
ignoreLocalhost: ignoreLocal = false, | ||
} = {}, | ||
} = useAppConfig(); | ||
const check = preflight(); | ||
const check = preflight({ domains, ignoreDnt, host, id, ignoreLocal }); | ||
if (check === 'ssr') { | ||
@@ -78,4 +61,3 @@ return; | ||
assert(typeof id === 'string'); | ||
const { id } = umConfig.value; | ||
const { payload } = getPayload(); | ||
@@ -82,0 +64,0 @@ const name = eventName || '#unknown-event'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16750
347
157