johannschopplich / nuxt-gtag

🔸 Google Analytics & Ads integration made easy
https://developers.google.com/tag-platform/gtagjs
MIT License
302 stars 9 forks source link

useTrackEvent typing improvement #28

Closed nandi95 closed 1 year ago

nandi95 commented 1 year ago

Describe the feature

useTrackEvent doesn't have the same typing as the useGtag()('event', ....nicely typed args)

I tried to implement this but didn't get far with it. Not easy when I only have the GTag type to work with.

import type { Gtag } from '../types'
import { useGtag } from './useGtag'

type Event = Gtag | ((command: 'event') => never)
type OmitCommandParameter<T> = T extends (command: 'event', ...rest: infer R) => void ? (...args: R) => void : never

export function useTrackEvent(
  eventName: Parameters<OmitCommandParameter<Event>>[0],
  eventParams?: Parameters<OmitCommandParameter<Event>>[1],
) {
  const { gtag } = useGtag()
  gtag('event', eventName, eventParams)
}

Perhaps other types can be exported too? It would make sense in case someone constructs the arguments outside of a function call.

Additional information

Final checks

johannschopplich commented 1 year ago

Valid point! Thanks for the hint and verbose issue description. 🙂