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.
This commit is contained in:
Lalit Singh 2024-11-12 12:59:25 +05:30 committed by Tim Abbott
parent 5b70775ab6
commit 8eab7591a3
6 changed files with 43 additions and 13 deletions

View File

@ -100,6 +100,7 @@
"@types/autosize": "^4.0.1", "@types/autosize": "^4.0.1",
"@types/blueimp-md5": "^2.18.0", "@types/blueimp-md5": "^2.18.0",
"@types/co-body": "^6.1.3", "@types/co-body": "^6.1.3",
"@types/gtag.js": "^0.0.20",
"@types/is-url": "^1.2.32", "@types/is-url": "^1.2.32",
"@types/jquery": "^3.3.31", "@types/jquery": "^3.3.31",
"@types/jquery.validation": "^1.16.7", "@types/jquery.validation": "^1.16.7",

View File

@ -311,6 +311,9 @@ importers:
'@types/co-body': '@types/co-body':
specifier: ^6.1.3 specifier: ^6.1.3
version: 6.1.3 version: 6.1.3
'@types/gtag.js':
specifier: ^0.0.20
version: 0.0.20
'@types/is-url': '@types/is-url':
specifier: ^1.2.32 specifier: ^1.2.32
version: 1.2.32 version: 1.2.32
@ -2338,6 +2341,9 @@ packages:
'@types/geojson@7946.0.14': '@types/geojson@7946.0.14':
resolution: {integrity: sha512-WCfD5Ht3ZesJUsONdhvm84dmzWOiOzOAqOncN0++w0lBw1o8OuDNJF2McvvCef/yBqb/HYRahp1BYtODFQ8bRg==} resolution: {integrity: sha512-WCfD5Ht3ZesJUsONdhvm84dmzWOiOzOAqOncN0++w0lBw1o8OuDNJF2McvvCef/yBqb/HYRahp1BYtODFQ8bRg==}
'@types/gtag.js@0.0.20':
resolution: {integrity: sha512-wwAbk3SA2QeU67unN7zPxjEHmPmlXwZXZvQEpbEUQuMCRGgKyE1m6XDuTUA9b6pCGb/GqJmdfMOY5LuDjJSbbg==}
'@types/hast@3.0.4': '@types/hast@3.0.4':
resolution: {integrity: sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==} resolution: {integrity: sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==}
@ -11229,6 +11235,8 @@ snapshots:
'@types/geojson@7946.0.14': {} '@types/geojson@7946.0.14': {}
'@types/gtag.js@0.0.20': {}
'@types/hast@3.0.4': '@types/hast@3.0.4':
dependencies: dependencies:
'@types/unist': 3.0.3 '@types/unist': 3.0.3

View File

@ -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 # historical commits sharing the same major version, in which case a
# minor version bump suffices. # 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

16
web/src/portico/ga-gtag.d.ts vendored Normal file
View File

@ -0,0 +1,16 @@
/// <reference types="gtag.js" />
/* 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;
}

View File

@ -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 = () => {};
}

View File

@ -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.
};
}