Consent API (#1)

# Changelog
- fix typo in supressDnt to suppressDnt setting,
- expose recordConsent and revokeConsent API calls,
- expose Wide Angle API as `useWideAngle()` and remove obsolete `useWaaEvent`

Reviewed-on: https://cloud.inputobjects.eu/forge/forge/wideangle/wideangle-nuxt/pulls/1
Co-authored-by: Jarek Rozanski <jrozanski@inputobjects.eu>
Co-committed-by: Jarek Rozanski <jrozanski@inputobjects.eu>
This commit is contained in:
Jarek Rozanski 2025-01-29 20:28:22 +00:00 committed by Jarek Rozanski
parent 787a1fd57f
commit f621c33de9
13 changed files with 191 additions and 116 deletions

View file

@ -1,6 +1,5 @@
import { defineNuxtModule, addPlugin, addImports, createResolver, useLogger } from '@nuxt/kit'
import { defineNuxtModule, addPlugin, addImportsDir, createResolver, useLogger } from '@nuxt/kit'
import { defu } from 'defu'
import { fileURLToPath } from 'url'
const logger = useLogger('nuxt:wideangle')
@ -8,10 +7,11 @@ export interface ModuleOptions {
siteId?: string
domain: string
fingerprint: boolean
supressDnt: boolean
suppressDnt: boolean
includeParams: string[]
excludePaths: string[]
ignoreHash: boolean
consentMarker: string
}
export default defineNuxtModule<ModuleOptions>({
@ -19,39 +19,32 @@ export default defineNuxtModule<ModuleOptions>({
name: 'wideangle',
configKey: 'wideangle',
compatibility: {
nuxt: '>=3'
}
nuxt: '>=3',
},
},
defaults: {
domain: "stats.wideangle.co",
domain: 'stats.wideangle.co',
fingerprint: false,
supressDnt: false,
suppressDnt: false,
includeParams: [],
excludePaths: [],
ignoreHash: false
ignoreHash: false,
consentMarker: undefined,
},
setup (options, nuxt) {
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
nuxt.options.runtimeConfig.public.wideangle = defu(
nuxt.options.runtimeConfig.public.wideangle,
options,
)
nuxt.options.build.transpile.push(runtimeDir);
const resolver = createResolver(import.meta.url);
nuxt.options.build.transpile.push(resolver.resolve('./runtime'))
logger.info('Adding Wide Angle Analytics runtime plugin');
logger.info('Adding Wide Angle Analytics (useWideAngle) import')
addImportsDir(resolver.resolve('./runtime/composables'))
addImports({
name: "useWaaEvent",
as: "useWaaEvent",
from: resolver.resolve('./runtime/composables/useWaaEvent')
});
logger.info('Adding Wide Angle Analytics runtime plugin')
addPlugin(resolver.resolve('./runtime/plugin.client'))
addPlugin({
src: resolver.resolve('./runtime/plugin.client')
});
}
},
})

View file

@ -1,11 +0,0 @@
import { useNuxtApp } from '#imports';
export function useWaaEvent (name: string, params?: Record<string, any>, values?: Record<string, number>) {
const waa = useNuxtApp().$waa
console.debug(`[WAA] Attempting to send Wide Angle event: ${name}`);
if(waa && waa.value) {
waa.value.dispatchEvent(name, params, values);
} else {
console.debug("[WAA] Wide Angle Analytics is not yest initialized");
}
}

View file

@ -0,0 +1,27 @@
import {useNuxtApp} from '#imports'
import type {WideAngleApi} from "~/src/types";
class NoOpWideAngleAnalyticsApi implements WideAngleApi {
dispatchEvent(name: string, params: any, values: any): void {
console.debug(`[WideAngleApi#dispatchEvent] Defaulting to NoOp Wide Angle call with name "${name}", params: ${JSON.stringify(params)}, values: ${JSON.stringify(params)}`);
}
recordConsent(subjectsId: string): void {
console.debug(`[WideAngleApi#recordConsent] Defaulting to NoOp Wide Angle call with ${subjectsId}`);
}
revokeConsent() {
console.debug(`[WideAngleApi#revokeConsent] Defaulting to NoOp Wide Angle call`);
}
}
const noOpWideAngleApi = new NoOpWideAngleAnalyticsApi();
export function useWideAngle() {
const { $waa } = useNuxtApp()
if ($waa) {
return $waa.value;
} else {
return noOpWideAngleApi;
}
}

View file

@ -1,30 +1,36 @@
import { defineNuxtPlugin, useRuntimeConfig } from '#imports';
import { ref } from 'vue';
import { initWideAngle } from 'wideangle-vuejs';
import { type Ref, ref} from 'vue'
import { initWideAngle } from 'wideangle-vuejs'
import { defineNuxtPlugin, type NuxtApp } from '#app'
import { useRuntimeConfig } from "#imports";
import type { WideAngleApi } from "~/src/types";
export default defineNuxtPlugin(() => {
if(import.meta.server) {
console.warn("[WAA] Plugin will not be enabled on server side.");
return;
export default defineNuxtPlugin(async (_nuxtApp) => {
if (import.meta.server) {
console.warn('[WAA] Plugin will not be enabled on server side.')
return
}
const { wideangle: options } = useRuntimeConfig().public
console.debug(`[WAA] Initializing Wide Angle Analytics with: ${JSON.stringify(options)}`);
if(options.siteId == null) {
throw new Error("[WAA] Wide Angle Analytics requires the site ID.");
console.debug(`[WAA] Initializing Wide Angle Analytics with: ${JSON.stringify(options)}`)
if (options.siteId == null) {
throw new Error('[WAA] Wide Angle Analytics requires the site ID.')
}
const waa = ref()
initWideAngle(options)
.then(waaInstance => {
waa.value = waaInstance;
console.debug("[WAA] Wide Angle Analytics instance available");
}).catch(e => { console.error("[WAA] Failed to load Wide Angle Plugin", e)});
.then((waaInstance) => {
waa.value = waaInstance
console.debug('[WAA] Wide Angle Analytics instance available')
}).catch((e) => {
console.error('[WAA] Failed to load Wide Angle Plugin', e)
})
return {
provide: {
waa
}
waa: waa as Ref<WideAngleApi>,
},
}
})

5
src/types.ts Normal file
View file

@ -0,0 +1,5 @@
export interface WideAngleApi {
dispatchEvent(name: string, params: any, values: any) : void;
recordConsent(subjectsId: string) : void;
revokeConsent() : void;
}