question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript support for @analytics/google-tag-manager

See original GitHub issue

I am using @analytics/google-tag-manager. I saw analytics have TypeScript support while @analytics/google-tag-manager not.

When add

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

got

TS7016: Could not find a declaration file for module ‘@analytics/google-tag-manager’. ‘/hongbomiao.com/client/node_modules/@analytics/google-tag-manager/lib/analytics-plugin-google-tag-manager.cjs.js’ implicitly has an ‘any’ type.   Try npm install @types/analytics__google-tag-manager if it exists or add a new declaration (.d.ts) file containing declare module '@analytics/google-tag-manager';

It would be great to add TypeScript in future. Thanks! : )

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
GeoMarkoucommented, Dec 1, 2020

As a workaround I added this code in a Types.ts file

declare module '@analytics/google-analytics' {
    type GoogleAnalyticsOptions = {
        /** Google Analytics site tracking Id */
        trackingId:string;

        /** Enable Google Analytics debug mode */
        debug?:boolean;

        /** Enable Anonymizing IP addresses sent to Google Analytics. See details below */
        anonymizeIp?:boolean;

        /** Map Custom dimensions to send extra information to Google Analytics. See details below */
        customDimensions?:object;

        /** Reset custom dimensions by key on analytics.page() calls. Useful for single page apps. */
        resetCustomDimensionsOnPage?:object;

        /** Mapped dimensions will be set to the page & sent as properties of all subsequent events on that page. If false, analytics will only pass custom dimensions as part of individual events */
        setCustomDimensionsToPage?:boolean;

        /** Custom tracker name for google analytics. Use this if you need multiple googleAnalytics scripts loaded */
        instanceName?:string;

        /** Custom URL for google analytics script, if proxying calls */
        customScriptSrc?:string;

        /** Additional cookie properties for configuring the ga cookie */
        cookieConfig?:object;

        /** Set custom google analytic tasks */
        tasks?:object;
    };
    
    type AnalyticsPlugin = {
        /** Name of plugin */
        name: string;

        /** exposed events of plugin */
        EVENTS?: any;

        /** Configuration of plugin */
        config?: any;

        /** Load analytics scripts method */
        initialize?: (...params: any[]) => any;

        /** Page visit tracking method */
        page?: (...params: any[]) => any;

        /** Custom event tracking method */
        track?: (...params: any[]) => any;

        /** User identify method */
        identify?: (...params: any[]) => any;

        /** Function to determine if analytics script loaded */
        loaded?: (...params: any[]) => any;

        /** Fire function when plugin ready */
        ready?: (...params: any[]) => any;
    };

    function GoogleAnalytics (options:GoogleAnalyticsOptions):AnalyticsPlugin;
    export default GoogleAnalytics;
}
3reactions
livthomascommented, May 27, 2022

The original question was about @analytics/google-tag-manager. And although @GeoMarkou posted a really good workaround for @analytics/google-analytics, those types are a little bit different.

Here are the types for @analytics/google-tag-manager package:

declare module '@analytics/google-tag-manager' {
  type AnalyticsPlugin = import('analytics').AnalyticsPlugin;

  type GoogleTagManagerConfig = {
    auth?: string;
    containerId: string;
    customScriptSrc?: string;
    dataLayerName?: string;
    debug?: boolean;
    execution?: string;
    preview?: string;
  };

  function googleTagManager(config: GoogleTagManagerConfig): AnalyticsPlugin;
  export default googleTagManager;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

@analytics/google-tag-manager - npm
Google tag manager plugin for 'analytics ' module. Latest version: 0.5.3, last published: 2 months ago.
Read more >
How to use google analytics in typescript and access dataLayer
declare var dataLayer: GoogleAnalyticsCode; //This fails: dataLayer.push({ 'event' : 'LoginSuccess' }); //This is OK with typescript but I'm not ...
Read more >
Using NextJS with Google Analytics and TypeScript
Google Analytics is an incredibly popular free tool used to track users, browsers and devices. NextJS is the best way to create React ......
Read more >
TypeScript and Google Publisher Tags
Since TypeScript is a superset of JavaScript, all working JavaScript code is also TypeScript code. However, TypeScript tooling can help detect ...
Read more >
connect gogle analytics with my next js typescript application
Help Center · Community ... connect gogle analytics with my next js typescript application ... Work with Tag Manager.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found