-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add telemetry scaffolding to Studio (#5503)
* feat(core): add telemetry scaffolding to Studio * fix(telemetry): log whether document was published for the first time, and published immediately after created
- Loading branch information
Showing
7 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/sanity/src/core/studio/StudioTelemetryProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {createBatchedStore, createSessionId} from '@sanity/telemetry' | ||
import {TelemetryProvider} from '@sanity/telemetry/react' | ||
import React, {ReactNode, useEffect, useMemo} from 'react' | ||
import arrify from 'arrify' | ||
import {SANITY_VERSION} from '../version' | ||
import {Config} from '../config' | ||
import {useClient} from '../hooks' | ||
|
||
const sessionId = createSessionId() | ||
|
||
// Wrap the app in a TelemetryProvider | ||
// This will enable usage of the `useTelemetry()` hook | ||
export function StudioTelemetryProvider(props: {children: ReactNode; config: Config}) { | ||
const client = useClient({apiVersion: 'v2023-12-18'}) | ||
|
||
const projectId = client.config().projectId | ||
const store = useMemo(() => { | ||
return createBatchedStore(sessionId, { | ||
// submit any pending events every <n> ms | ||
flushInterval: 30000, | ||
|
||
// implements user consent resolving | ||
resolveConsent: () => client.request({uri: '/intake/telemetry-status'}), | ||
|
||
// implements sending events to backend | ||
sendEvents: (batch) => | ||
client.request({ | ||
uri: '/intake/batch', | ||
method: 'POST', | ||
json: true, | ||
body: {projectId, batch}, | ||
}), | ||
// opts into a different strategy for sending events when the browser close, reload or navigate away from the current page | ||
sendBeacon: (batch) => | ||
navigator.sendBeacon(client.getUrl('/intake/batch'), JSON.stringify({projectId, batch})), | ||
}) | ||
}, [client, projectId]) | ||
|
||
useEffect(() => { | ||
store.logger.updateUserProperties({ | ||
studioVersion: SANITY_VERSION, | ||
plugins: arrify(props.config).flatMap( | ||
(config) => | ||
config.plugins?.flatMap((plugin) => ({ | ||
name: plugin.name, | ||
})) || [], | ||
), | ||
}) | ||
}, [props.config, store.logger]) | ||
|
||
return <TelemetryProvider store={store}>{props.children}</TelemetryProvider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/sanity/src/structure/documentActions/__telemetry__/documentActions.telemetry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {defineEvent} from '@sanity/telemetry' | ||
|
||
interface DocumentPublishedInfo { | ||
/** | ||
* The document was created and published straight away | ||
*/ | ||
publishedImmediately: boolean | ||
|
||
/** | ||
* The document had a previously published version when it was published | ||
*/ | ||
previouslyPublished: boolean | ||
} | ||
export const DocumentPublished = defineEvent<DocumentPublishedInfo>({ | ||
name: 'Document Published', | ||
version: 1, | ||
description: 'User clicked the "Publish" button in the document pane', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66b88f7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
performance-studio – ./
performance-studio-git-next.sanity.build
performance-studio.sanity.build
66b88f7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
test-studio – ./
test-studio-git-next.sanity.build
test-studio.sanity.build