From 8eab7591a3e8675bbcdb36438d656ef5fa2e1219 Mon Sep 17 00:00:00 2001 From: Lalit Singh Date: Tue, 12 Nov 2024 12:59:25 +0530 Subject: [PATCH] ts: Convert `portico/google_analytics.js` to TypeScript. Created a custom type declaration file `ga-gtag.d.ts` for `ga-gtag` npm module. `@types/gtag.js` is installed as a devDependency so that the type for `gtag` function can be provided. --- package.json | 1 + pnpm-lock.yaml | 8 ++++++++ version.py | 2 +- web/src/portico/ga-gtag.d.ts | 16 ++++++++++++++++ web/src/portico/google-analytics.js | 12 ------------ web/src/portico/google-analytics.ts | 17 +++++++++++++++++ 6 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 web/src/portico/ga-gtag.d.ts delete mode 100644 web/src/portico/google-analytics.js create mode 100644 web/src/portico/google-analytics.ts diff --git a/package.json b/package.json index dd7c418d58..08a77eef81 100644 --- a/package.json +++ b/package.json @@ -100,6 +100,7 @@ "@types/autosize": "^4.0.1", "@types/blueimp-md5": "^2.18.0", "@types/co-body": "^6.1.3", + "@types/gtag.js": "^0.0.20", "@types/is-url": "^1.2.32", "@types/jquery": "^3.3.31", "@types/jquery.validation": "^1.16.7", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 76c064478c..f5ea0f0703 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -311,6 +311,9 @@ importers: '@types/co-body': specifier: ^6.1.3 version: 6.1.3 + '@types/gtag.js': + specifier: ^0.0.20 + version: 0.0.20 '@types/is-url': specifier: ^1.2.32 version: 1.2.32 @@ -2338,6 +2341,9 @@ packages: '@types/geojson@7946.0.14': resolution: {integrity: sha512-WCfD5Ht3ZesJUsONdhvm84dmzWOiOzOAqOncN0++w0lBw1o8OuDNJF2McvvCef/yBqb/HYRahp1BYtODFQ8bRg==} + '@types/gtag.js@0.0.20': + resolution: {integrity: sha512-wwAbk3SA2QeU67unN7zPxjEHmPmlXwZXZvQEpbEUQuMCRGgKyE1m6XDuTUA9b6pCGb/GqJmdfMOY5LuDjJSbbg==} + '@types/hast@3.0.4': resolution: {integrity: sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==} @@ -11229,6 +11235,8 @@ snapshots: '@types/geojson@7946.0.14': {} + '@types/gtag.js@0.0.20': {} + '@types/hast@3.0.4': dependencies: '@types/unist': 3.0.3 diff --git a/version.py b/version.py index 9b46db1973..1a62d30157 100644 --- a/version.py +++ b/version.py @@ -49,4 +49,4 @@ API_FEATURE_LEVEL = 319 # Last bumped for message-link class # historical commits sharing the same major version, in which case a # minor version bump suffices. -PROVISION_VERSION = (301, 0) # bumped 2024-11-10 to remove commander, minimist, yargs +PROVISION_VERSION = (301, 1) # bumped 2024-11-12 to add types for Google analytics diff --git a/web/src/portico/ga-gtag.d.ts b/web/src/portico/ga-gtag.d.ts new file mode 100644 index 0000000000..eaf7ea5756 --- /dev/null +++ b/web/src/portico/ga-gtag.d.ts @@ -0,0 +1,16 @@ +/// + +/* global Gtag */ + +/** * npm module name: "ga-gtag" + * version: "1.2.0" + */ + +declare module "ga-gtag" { + export type ConfigParams = Gtag.GtagCommands["config"][1]; + + // Type reference: https://github.com/idmadj/ga-gtag/blob/eb7a97d153cbfedbc81344fd59123f737b8a5cb8/src/index.js + export const install: (trackingId: string, additionalConfigInfo?: ConfigParams) => void; + + export const gtag: Gtag.Gtag; +} diff --git a/web/src/portico/google-analytics.js b/web/src/portico/google-analytics.js deleted file mode 100644 index ad0171f17a..0000000000 --- a/web/src/portico/google-analytics.js +++ /dev/null @@ -1,12 +0,0 @@ -import {gtag, install} from "ga-gtag"; - -import {page_params} from "../base_page_params"; - -export let config; - -if (page_params.google_analytics_id !== undefined) { - install(page_params.google_analytics_id); - config = (info) => gtag("config", page_params.google_analytics_id, info); -} else { - config = () => {}; -} diff --git a/web/src/portico/google-analytics.ts b/web/src/portico/google-analytics.ts new file mode 100644 index 0000000000..5e2a9f2d7a --- /dev/null +++ b/web/src/portico/google-analytics.ts @@ -0,0 +1,17 @@ +import {gtag, install} from "ga-gtag"; +import type {ConfigParams} from "ga-gtag"; + +import {page_params} from "../base_page_params"; + +export let config: (info: ConfigParams) => void; + +if (page_params.google_analytics_id !== undefined) { + install(page_params.google_analytics_id); + config = (info) => { + gtag("config", page_params.google_analytics_id!, info); + }; +} else { + config = () => { + // No Google Analytics tracking configured. + }; +}