Skip to content

Commit

Permalink
Merge branch 'main' into fix/create-astro-ts-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Jan 24, 2024
2 parents d6230b1 + 2a44c8f commit bc112de
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-tips-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Adds telemetry for when apps are toggled in the dev toolbar. This data is completely anonymous and only the names of built-in apps are shared with us. This data will help us monitor how much the dev toolbar is used and which apps are used more. For more information on how Astro collects telemetry, visit the following page: https://astro.build/telemetry/
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 && app.id !== 'astro:more') {
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?.id;
// 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

0 comments on commit bc112de

Please sign in to comment.