Skip to content

Commit

Permalink
feat!: update module options
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianGlowala committed Apr 19, 2024
1 parent 4f6a634 commit 8755ba1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
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

0 comments on commit 8755ba1

Please sign in to comment.