Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add telemetry for toolbar apps #9816

Merged
merged 8 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/astro/src/events/toolbar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const EVENT_TOOLBAR_APP_TOGGLED = 'ASTRO_TOOLBAR_APP_TOGGLED';

interface AppToggledEventPayload {
app: string;
}

export function eventAppToggled(options: {
// eslint-disable-next-line @typescript-eslint/ban-types
appName: 'other' | (string & {});
}): { eventName: string; payload: AppToggledEventPayload }[] {
const payload: AppToggledEventPayload = {
app: options.appName,
};

return [{ eventName: EVENT_TOOLBAR_APP_TOGGLED, payload }];
}
6 changes: 6 additions & 0 deletions packages/astro/src/runtime/client/dev-toolbar/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ export class AstroDevToolbar extends HTMLElement {
// was to close that app, so no action needed.
if (app !== activeApp) {
await this.setAppStatus(app, true);

if (import.meta.hot) {
import.meta.hot.send('astro:devtoolbar:app:toggled', {
app: app,
});
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type * as vite from 'vite';
import type { AstroPluginOptions } from '../@types/astro.js';
import { telemetry } from '../events/index.js';
import { eventAppToggled } from '../events/toolbar.js';

const VIRTUAL_MODULE_ID = 'astro:dev-toolbar';
const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;

export default function astroDevToolbar({ settings, logger }: AstroPluginOptions): vite.Plugin {
let telemetryTimeout: ReturnType<typeof setTimeout>;

return {
name: 'astro:dev-toolbar',
config() {
Expand Down Expand Up @@ -34,6 +38,23 @@ export default function astroDevToolbar({ settings, logger }: AstroPluginOptions
`Failed to initialize dev toolbar app ${args.app.name} (${args.app.id}):\n${args.error}`
);
});

server.ws.on('astro:devtoolbar:app:toggled', (args) => {
// Debounce telemetry to avoid recording events when the user is rapidly toggling apps for debugging
clearTimeout(telemetryTimeout);
telemetryTimeout = setTimeout(() => {
let nameToRecord = args?.app?.name;
// Only record apps names for apps that are built-in
if (!nameToRecord || !nameToRecord.startsWith('astro:')) {
nameToRecord = 'other';
}
telemetry.record(
eventAppToggled({
appName: nameToRecord,
})
);
}, 200);
});
},
async load(id) {
if (id === resolvedVirtualModuleId) {
Expand Down
Loading