From 070d44802f2fd27f01c64a51eeb9477c1e8a646e Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Tue, 20 Sep 2022 15:06:31 +0100 Subject: [PATCH] Chore: Type Rudderstack and AppInsights window APIs (#55162) * Chore: Type Rudderstack and AppInsights window APIs * remove rudder sdk to dev deps --- .betterer.results | 29 ++++------------ package.json | 1 + .../analytics/ApplicationInsightsBackend.ts | 20 +++++++++-- .../backends/analytics/RudderstackBackend.ts | 34 ++++++++++++++----- yarn.lock | 8 +++++ 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/.betterer.results b/.betterer.results index f8c40592b7b..48903832942 100644 --- a/.betterer.results +++ b/.betterer.results @@ -2900,34 +2900,17 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "6"] ], "public/app/core/services/echo/backends/analytics/ApplicationInsightsBackend.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"], - [0, 0, 0, "Unexpected any. Specify a different type.", "1"], - [0, 0, 0, "Do not use any type assertions.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"], - [0, 0, 0, "Do not use any type assertions.", "4"], - [0, 0, 0, "Unexpected any. Specify a different type.", "5"], - [0, 0, 0, "Do not use any type assertions.", "6"], - [0, 0, 0, "Unexpected any. Specify a different type.", "7"], - [0, 0, 0, "Do not use any type assertions.", "8"], - [0, 0, 0, "Unexpected any. Specify a different type.", "9"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"], + [0, 0, 0, "Do not use any type assertions.", "1"], + [0, 0, 0, "Unexpected any. Specify a different type.", "2"], + [0, 0, 0, "Do not use any type assertions.", "3"], + [0, 0, 0, "Unexpected any. Specify a different type.", "4"] ], "public/app/core/services/echo/backends/analytics/RudderstackBackend.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Do not use any type assertions.", "2"], - [0, 0, 0, "Unexpected any. Specify a different type.", "3"], - [0, 0, 0, "Do not use any type assertions.", "4"], - [0, 0, 0, "Unexpected any. Specify a different type.", "5"], - [0, 0, 0, "Do not use any type assertions.", "6"], - [0, 0, 0, "Unexpected any. Specify a different type.", "7"], - [0, 0, 0, "Do not use any type assertions.", "8"], - [0, 0, 0, "Unexpected any. Specify a different type.", "9"], - [0, 0, 0, "Do not use any type assertions.", "10"], - [0, 0, 0, "Unexpected any. Specify a different type.", "11"], - [0, 0, 0, "Do not use any type assertions.", "12"], - [0, 0, 0, "Unexpected any. Specify a different type.", "13"], - [0, 0, 0, "Do not use any type assertions.", "14"], - [0, 0, 0, "Unexpected any. Specify a different type.", "15"] + [0, 0, 0, "Unexpected any. Specify a different type.", "3"] ], "public/app/core/services/echo/backends/sentry/transports/CustomEndpointTransport.test.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], diff --git a/package.json b/package.json index 1f67e84a0bc..aa78b053e5f 100644 --- a/package.json +++ b/package.json @@ -223,6 +223,7 @@ "react-test-renderer": "17.0.2", "redux-mock-store": "1.5.4", "rimraf": "3.0.2", + "rudder-sdk-js": "^2.13.0", "sass": "1.54.0", "sass-loader": "13.0.2", "sinon": "14.0.0", diff --git a/public/app/core/services/echo/backends/analytics/ApplicationInsightsBackend.ts b/public/app/core/services/echo/backends/analytics/ApplicationInsightsBackend.ts index 88722555e09..6430c22fd1b 100644 --- a/public/app/core/services/echo/backends/analytics/ApplicationInsightsBackend.ts +++ b/public/app/core/services/echo/backends/analytics/ApplicationInsightsBackend.ts @@ -9,6 +9,19 @@ import { PageviewEchoEvent, } from '@grafana/runtime'; +interface ApplicationInsights { + trackPageView: () => void; + trackEvent: (event: { name: string; properties?: Record }) => void; +} + +declare global { + interface Window { + // We say all methods are undefined because we can't be sure they're there + // and we should be extra cautious + applicationInsights?: Partial; + } +} + export interface ApplicationInsightsBackendOptions { connectionString: string; endpointUrl?: string; @@ -29,22 +42,23 @@ export class ApplicationInsightsBackend implements EchoBackend { - if (!(window as any).applicationInsights) { + if (!window.applicationInsights) { return; } if (isPageviewEvent(e)) { - (window as any).applicationInsights.trackPageView(); + window.applicationInsights.trackPageView?.(); } if (isInteractionEvent(e)) { - (window as any).applicationInsights.trackEvent({ + window.applicationInsights.trackEvent?.({ name: e.payload.interactionName, properties: e.payload.properties, }); diff --git a/public/app/core/services/echo/backends/analytics/RudderstackBackend.ts b/public/app/core/services/echo/backends/analytics/RudderstackBackend.ts index 00c6403bf10..b169e3c862c 100644 --- a/public/app/core/services/echo/backends/analytics/RudderstackBackend.ts +++ b/public/app/core/services/echo/backends/analytics/RudderstackBackend.ts @@ -1,4 +1,5 @@ import $ from 'jquery'; +import type { identify, load, page, track } from 'rudder-sdk-js'; // SDK is loaded dynamically from config, so we only import types from the SDK package import { CurrentUserDTO } from '@grafana/data'; import { @@ -12,6 +13,21 @@ import { import { getUserIdentifier } from '../../utils'; +interface Rudderstack { + identify: typeof identify; + load: typeof load; + page: typeof page; + track: typeof track; +} + +declare global { + interface Window { + // We say all methods are undefined because we can't be sure they're there + // and we should be extra cautious + rudderanalytics?: Partial; + } +} + export interface RudderstackBackendOptions { writeKey: string; dataPlaneUrl: string; @@ -32,7 +48,7 @@ export class RudderstackBackend implements EchoBackend)[method] = (function (methodName) { + (tempRudderstack as Record)[method] = (function (methodName) { return function () { // @ts-ignore - rds.push([methodName].concat(Array.prototype.slice.call(arguments))); + tempRudderstack.push([methodName].concat(Array.prototype.slice.call(arguments))); }; })(method); } - (rds as any).load(options.writeKey, options.dataPlaneUrl, { configUrl: options.configUrl }); + window.rudderanalytics?.load?.(options.writeKey, options.dataPlaneUrl, { configUrl: options.configUrl }); if (options.user) { const identifier = getUserIdentifier(options.user); - (rds as any).identify(identifier, { + window.rudderanalytics?.identify?.(identifier, { email: options.user.email, orgId: options.user.orgId, }); @@ -70,20 +86,20 @@ export class RudderstackBackend implements EchoBackend { - if (!(window as any).rudderanalytics) { + if (!window.rudderanalytics) { return; } if (isPageviewEvent(e)) { - (window as any).rudderanalytics.page(); + window.rudderanalytics.page?.(); } if (isInteractionEvent(e)) { - (window as any).rudderanalytics.track(e.payload.interactionName, e.payload.properties); + window.rudderanalytics.track?.(e.payload.interactionName, e.payload.properties); } if (isExperimentViewEvent(e)) { - (window as any).rudderanalytics.track('experiment_viewed', { + window.rudderanalytics.track?.('experiment_viewed', { experiment_id: e.payload.experimentId, experiment_group: e.payload.experimentGroup, experiment_variant: e.payload.experimentVariant, diff --git a/yarn.lock b/yarn.lock index a08e0dd3c53..4d8982c8adc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22429,6 +22429,7 @@ __metadata: reselect: 4.1.6 rimraf: 3.0.2 rst2html: "github:thoward/rst2html#990cb89f2a300cdd9151790be377c4c0840df809" + rudder-sdk-js: ^2.13.0 rxjs: 7.5.6 sass: 1.54.0 sass-loader: 13.0.2 @@ -34038,6 +34039,13 @@ __metadata: languageName: node linkType: hard +"rudder-sdk-js@npm:^2.13.0": + version: 2.13.0 + resolution: "rudder-sdk-js@npm:2.13.0" + checksum: 36c5200d7f49b4871ebe082498cf9e3d358821fdda0e51d4a1f916dd26fc6e820eea7077e42b837cf7edcea81cce0b7b95f69bc81496c43b9f22957d42747c94 + languageName: node + linkType: hard + "run-async@npm:^2.4.0": version: 2.4.1 resolution: "run-async@npm:2.4.1"