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!: update module options #62

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script setup lang="ts">
const { gtag: gtagOpts } = useRuntimeConfig().public
const gtagOpts = useRuntimeConfig().public.gtag

const { gtag, initialize, enableAnalytics, disableAnalytics } = useGtag()
const isInitialized = ref(gtagOpts.enabled)
const isInitialized = ref(gtagOpts.initMode === 'auto')
const isAnalyticsActive = ref(true)

onMounted(() => {
Expand Down
3 changes: 2 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export default defineNuxtConfig({
modules: ['../src/module.ts'],

gtag: {
enabled: false,
enabled: true,
initMode: 'manual',
id: 'G-ZZZZZZZZZZ',
initCommands: [
// Setup up consent mode
Expand Down
21 changes: 17 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ import type { GoogleTagOptions } from './runtime/types'

export interface ModuleOptions {
/**
* Whether to initialize the Google tag script immediately after the page has loaded.
* Whether to enable the module.
*
* @remarks
* Set this to `false` to delay the initialization until you call the `enable` function manually.
* Allows for enabling the module conditionally.
*
* @default true
*/
enabled?: boolean

/**
* Whether to initialize the Google tag script immediately after the page has loaded.
*
* @remarks
* Set this to `manual` to delay the initialization until you call the `initialize` function manually.
*
* @default true
*/
initMode?: 'auto' | 'manual'

/**
* The Google tag ID to initialize.
*
Expand Down Expand Up @@ -104,12 +114,15 @@ export default defineNuxtModule<ModuleOptions>({
url: 'https://www.googletagmanager.com/gtag/js',
},
setup(options, nuxt) {
if (!options.enabled) {
return
}

const { resolve } = createResolver(import.meta.url)

// Add module options to public runtime config
// @ts-expect-error: Loosely typed runtime config
nuxt.options.runtimeConfig.public.gtag = defu(
nuxt.options.runtimeConfig.public.gtag as Required<ModuleOptions>,
nuxt.options.runtimeConfig.public.gtag,
options,
)

Expand Down
6 changes: 4 additions & 2 deletions src/runtime/plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export default defineNuxtPlugin({
const options = useRuntimeConfig().public.gtag as Required<ModuleOptions>
const tags = resolveTags(options)

if (!tags.length)
if (!tags.length) {
return
}

initGtag({ tags })

if (!options.enabled)
if (options.initMode === 'manual') {
return
}

// Sanitize loading strategy to be either `async` or `defer`
const strategy = options.loadingStrategy === 'async' ? 'async' : 'defer'
Expand Down