Skip to content

Commit

Permalink
feat: options param for useGtagConsent
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Aug 14, 2023
1 parent 7db46cd commit d93e785
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/runtime/composables/useGtagConsent.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type { UseGtagConsentOptions } from '../types'
import { useHead, useRuntimeConfig } from '#imports'

export function useGtagConsent(
hasConsent: boolean,
{ id }: {
/**
* In case you want to initialize a custom Gtag ID. Make sure to set
* `initialConsent` to `false` in the module options beforehand.
*/
id?: string
} = {},
options?: Omit<UseGtagConsentOptions, 'hasConsent'>,
): void
export function useGtagConsent(options: UseGtagConsentOptions): void
export function useGtagConsent(
arg1: boolean | UseGtagConsentOptions,
arg2?: Omit<UseGtagConsentOptions, 'hasConsent'>,
) {
const hasConsent = typeof arg1 === 'boolean' ? arg1 : arg1.hasConsent ?? true
const { id } = typeof arg1 === 'boolean' ? arg2 ?? {} : arg1

const { gtag: { id: defaultId } } = useRuntimeConfig().public

if (process.client && ('dataLayer' in window)) {
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ export interface Gtag {
(command: 'consent', consentArg: string, consentParams: Record<string, any>): void
(command: 'js', config: Date): void
}

export interface UseGtagConsentOptions {
/**
* Whether to accept or decline the consent.
*
* @default true
*/
hasConsent?: boolean
/**
* In case you want to initialize a custom Gtag ID. Make sure to set
* `initialConsent` to `false` in the module options beforehand.
*/
id?: string
}

0 comments on commit d93e785

Please # to comment.