analytics icon indicating copy to clipboard operation
analytics copied to clipboard

Nextjs and Typescript @analytics/google-tag-manager error after import

Open santyas opened this issue 3 years ago • 3 comments

Hello! Is there any support/package for that plugin with typescript?

Thank you! S

santyas avatar Jul 23 '22 21:07 santyas

No types are included with this package. You will need to do something like this https://github.com/DavidWells/analytics/issues/99#issuecomment-736153120

DavidWells avatar Jul 24 '22 19:07 DavidWells

The docs currently are indicating that TypeScript is supported. Is this something you are working on or have plans for?

https://github.com/DavidWells/analytics#typescript-support

dobesv avatar Aug 09 '22 06:08 dobesv

Can confirm, yarn wasn't be able to manage typescript definitions after installing this package.

Could not find a declaration file for module '@analytics/google-analytics'. 'C:/gorazdo/squads/node_modules/@analytics/google-analytics/lib/analytics-plugin-ga.cjs.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/analytics__google-analytics` if it exists or add a new declaration (.d.ts) file containing `declare module '@analytics/google-analytics';`

For the google purposes ^

PaulCarduelis avatar Sep 02 '22 10:09 PaulCarduelis

Same with me.

xrow avatar Nov 17 '22 13:11 xrow

I have the same issue and I have fixed it as per advice from several answers

For my analytics file in type script I have

import googleTagManager from '@analytics/google-tag-manager'
import Analytics from 'analytics'

const analytics = Analytics({
    app: process.env.NEXT_PUBLIC_ANALYTICS_APP_NAME,
    debug: true,
    plugins: [
        googleTagManager({
            containerId: process.env.NEXT_PUBLIC_GOOGLE_TAG_MANAGER_ID || ""
        }),
    ]
})

export default analytics

and I have a file for extra type definitions additional.d.ts where I put the following

declare module '@analytics/google-tag-manager' {
    export interface googleTagManagerProps {
      containerId: string
      dataLayerName?: string
      customScriptSrc?: string
      preview?: string
      auth?: string
      execution?: string
    }
  
    function googleTagManager({
      containerId,
      dataLayerName,
      customScriptSrc,
      preview,
      auth,
      execution
    }: googleTagManagerProps)
  
    export = googleTagManager
  }

engmsaleh avatar Dec 22 '22 14:12 engmsaleh

Great! Where have you created that additional.d.ts file?

santyas avatar Dec 26 '22 14:12 santyas

Great! Where have you created that additional.d.ts file?

You can add this in the following directory if using nextjs.

src/additional.d.ts

The nextjs default tsconfig file will autoamtically pick up definitions from anywhere in src.

bradiosd avatar May 11 '23 01:05 bradiosd