Chore: Type Rudderstack and AppInsights window APIs (#55162)

* Chore: Type Rudderstack and AppInsights window APIs

* remove rudder sdk to dev deps
This commit is contained in:
Josh Hunt 2022-09-20 15:06:31 +01:00 committed by GitHub
parent 4711454b81
commit 070d44802f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 35 deletions

View File

@ -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"],

View File

@ -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",

View File

@ -9,6 +9,19 @@ import {
PageviewEchoEvent,
} from '@grafana/runtime';
interface ApplicationInsights {
trackPageView: () => void;
trackEvent: (event: { name: string; properties?: Record<string, any> }) => 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<ApplicationInsights>;
}
}
export interface ApplicationInsightsBackendOptions {
connectionString: string;
endpointUrl?: string;
@ -29,22 +42,23 @@ export class ApplicationInsightsBackend implements EchoBackend<PageviewEchoEvent
endpointUrl: options.endpointUrl,
},
};
const init = new (window as any).Microsoft.ApplicationInsights.ApplicationInsights(applicationInsightsOpts);
(window as any).applicationInsights = init.loadAppInsights();
});
}
addEvent = (e: PageviewEchoEvent | InteractionEchoEvent) => {
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,
});

View File

@ -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<Rudderstack>;
}
}
export interface RudderstackBackendOptions {
writeKey: string;
dataPlaneUrl: string;
@ -32,7 +48,7 @@ export class RudderstackBackend implements EchoBackend<PageviewEchoEvent, Rudder
cache: true,
});
const rds = ((window as any).rudderanalytics = []);
const tempRudderstack = ((window as any).rudderanalytics = []);
const methods = [
'load',
@ -49,20 +65,20 @@ export class RudderstackBackend implements EchoBackend<PageviewEchoEvent, Rudder
for (let i = 0; i < methods.length; i++) {
const method = methods[i];
(rds as Record<string, any>)[method] = (function (methodName) {
(tempRudderstack as Record<string, any>)[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<PageviewEchoEvent, Rudder
}
addEvent = (e: PageviewEchoEvent) => {
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,

View File

@ -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"