Skip to content

Commit a78540a

Browse files
committed
Merge pull request #32770 from storybookjs/shilman/preview-first-load
Core: Add `preview-first-load` telemetry (cherry picked from commit 124987d)
1 parent a7e9934 commit a78540a

File tree

8 files changed

+51
-9
lines changed

8 files changed

+51
-9
lines changed

code/core/src/core-events/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ enum events {
5858
// A global was just updated
5959
GLOBALS_UPDATED = 'globalsUpdated',
6060
REGISTER_SUBSCRIPTION = 'registerSubscription',
61+
// Preview initialized for first-load-event
62+
PREVIEW_INITIALIZED = 'previewInitialized',
6163
// Tell the manager that the user pressed a key in the preview
6264
PREVIEW_KEYDOWN = 'previewKeydown',
6365
// Tell the preview that the builder is in progress
@@ -111,6 +113,7 @@ export const {
111113
PLAY_FUNCTION_THREW_EXCEPTION,
112114
UNHANDLED_ERRORS_WHILE_PLAYING,
113115
PRELOAD_ENTRIES,
116+
PREVIEW_INITIALIZED,
114117
PREVIEW_BUILDER_PROGRESS,
115118
PREVIEW_KEYDOWN,
116119
REGISTER_SUBSCRIPTION,

code/core/src/core-server/presets/common-preset.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { dedent } from 'ts-dedent';
2929

3030
import { initCreateNewStoryChannel } from '../server-channel/create-new-story-channel';
3131
import { initFileSearchChannel } from '../server-channel/file-search-channel';
32+
import { initPreviewInitializedChannel } from '../server-channel/preview-initialized-channel';
3233
import { defaultStaticDirs } from '../utils/constants';
3334
import { initializeSaveStory } from '../utils/save-story/save-story';
3435
import { parseStaticDir } from '../utils/server-statics';
@@ -257,6 +258,7 @@ export const experimental_serverChannel = async (
257258

258259
initFileSearchChannel(channel, options, coreOptions);
259260
initCreateNewStoryChannel(channel, options, coreOptions);
261+
initPreviewInitializedChannel(channel, options, coreOptions);
260262

261263
return channel;
262264
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Channel } from 'storybook/internal/channels';
2+
import { PREVIEW_INITIALIZED } from 'storybook/internal/core-events';
3+
import { telemetry } from 'storybook/internal/telemetry';
4+
import type { CoreConfig, Options } from 'storybook/internal/types';
5+
6+
import { getLastEvents } from '../../telemetry/event-cache';
7+
import { getSessionId } from '../../telemetry/session-id';
8+
9+
export function initPreviewInitializedChannel(
10+
channel: Channel,
11+
options: Options,
12+
_coreConfig: CoreConfig
13+
) {
14+
channel.on(PREVIEW_INITIALIZED, async ({ userAgent }) => {
15+
if (!options.disableTelemetry) {
16+
try {
17+
const sessionId = await getSessionId();
18+
const lastEvents = await getLastEvents();
19+
const lastInit = lastEvents.init;
20+
const lastPreviewFirstLoad = lastEvents['preview-first-load'];
21+
if (!lastPreviewFirstLoad) {
22+
const isInitSession = lastInit?.body.sessionId === sessionId;
23+
const timeSinceInit = lastInit ? Date.now() - lastInit.body.timestamp : undefined;
24+
telemetry('preview-first-load', { timeSinceInit, isInitSession, userAgent });
25+
}
26+
} catch (e) {
27+
// do nothing
28+
}
29+
}
30+
});
31+
}

code/core/src/manager/globals/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ export default {
618618
'PLAY_FUNCTION_THREW_EXCEPTION',
619619
'PRELOAD_ENTRIES',
620620
'PREVIEW_BUILDER_PROGRESS',
621+
'PREVIEW_INITIALIZED',
621622
'PREVIEW_KEYDOWN',
622623
'REGISTER_SUBSCRIPTION',
623624
'REQUEST_WHATS_NEW_DATA',

code/core/src/preview-api/modules/preview-web/Preview.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
FORCE_REMOUNT,
1010
FORCE_RE_RENDER,
1111
GLOBALS_UPDATED,
12+
PREVIEW_INITIALIZED,
1213
RESET_STORY_ARGS,
1314
type RequestData,
1415
type ResponseData,
@@ -128,6 +129,9 @@ export class Preview<TRenderer extends Renderer> {
128129
const projectAnnotations = await this.getProjectAnnotationsOrRenderError();
129130
await this.runBeforeAllHook(projectAnnotations);
130131
await this.initializeWithProjectAnnotations(projectAnnotations);
132+
// eslint-disable-next-line compat/compat
133+
const userAgent = globalThis?.navigator?.userAgent;
134+
await this.channel.emit(PREVIEW_INITIALIZED, { userAgent });
131135
} catch (err) {
132136
this.rejectStoreInitializationPromise(err as Error);
133137
}

code/core/src/telemetry/event-cache.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ export const set = async (eventType: EventType, body: any) => {
2424
};
2525

2626
export const get = async (eventType: EventType) => {
27-
const lastEvents = await cache.get('lastEvents');
28-
return lastEvents?.[eventType];
27+
const lastEvents = await getLastEvents();
28+
return lastEvents[eventType];
29+
};
30+
31+
export const getLastEvents = async () => {
32+
return (await cache.get('lastEvents')) || {};
2933
};
3034

3135
const upgradeFields = (event: any): UpgradeSummary => {

code/core/src/telemetry/telemetry.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,13 @@ export async function sendTelemetry(
9797
try {
9898
request = prepareRequest(data, context, options);
9999
tasks.push(request);
100-
if (options.immediate) {
101-
await Promise.all(tasks);
102-
} else {
103-
await request;
104-
}
105100

106101
const sessionId = await getSessionId();
107102
const eventId = nanoid();
108103
const body = { ...rest, eventType, eventId, sessionId, metadata, payload, context };
109104

110-
await saveToCache(eventType, body);
105+
const waitFor = options.immediate ? tasks : [request];
106+
await Promise.all([...waitFor, saveToCache(eventType, body)]);
111107
} catch (err) {
112108
//
113109
} finally {

code/core/src/telemetry/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export type EventType =
3131
| 'test-run'
3232
| 'addon-onboarding'
3333
| 'onboarding-survey'
34-
| 'mocking';
34+
| 'mocking'
35+
| 'preview-first-load';
3536

3637
export interface Dependency {
3738
version: string | undefined;

0 commit comments

Comments
 (0)