Skip to content

Commit f70c885

Browse files
refactor(types,shared,elements): Assign telemetry property to main Clerk interface (#3329)
* refactor(types,shared): Create separate type interface for `TelemetryCollector` * fix(shared): Remove `TelemetryEvent` interfaces * fix(elements): Remove assertion for `telemetry` * docs(types): Add JSDocs on telemetry types to indicate internal usage * refactor(elements): Remove `useTelemetry` hook * Update .changeset/heavy-kings-fix.md --------- Co-authored-by: Lennart <lekoarts@gmail.com>
1 parent f5804a2 commit f70c885

File tree

19 files changed

+94
-73
lines changed

19 files changed

+94
-73
lines changed

.changeset/heavy-kings-fix.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@clerk/shared': patch
3+
'@clerk/types': patch
4+
'@clerk/elements': patch
5+
---
6+
7+
The following are all internal changes and not relevant to any end-user:
8+
9+
Create type interface for `TelemetryCollector` on `@clerk/types`. This allows to assign `telemetry` on the main Clerk SDK object, while inheriting from the actual `TelemetryCollector` implementation.

packages/clerk-js/src/core/clerk.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class Clerk implements ClerkInterface {
151151
public organization: OrganizationResource | null | undefined;
152152
public user: UserResource | null | undefined;
153153
public __internal_country?: string | null;
154-
public telemetry?: TelemetryCollector;
154+
public telemetry: TelemetryCollector | undefined;
155155

156156
protected internal_last_error: ClerkAPIError | null = null;
157157

packages/elements/src/react/common/form/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useClerk } from '@clerk/clerk-react';
12
import { eventComponentMounted } from '@clerk/shared/telemetry';
23
import type { Autocomplete } from '@clerk/types';
34
import { composeEventHandlers } from '@radix-ui/primitive';
@@ -35,7 +36,6 @@ import {
3536
} from '~/internals/machines/form/form.context';
3637
import { usePassword } from '~/react/hooks/use-password.hook';
3738
import type { ErrorMessagesKey } from '~/react/utils/generate-password-error-text';
38-
import { useTelemetry } from '~/react/utils/telemetry';
3939

4040
import type { OTPInputProps } from './otp';
4141
import { OTP_LENGTH_DEFAULT, OTPInput } from './otp';
@@ -530,9 +530,9 @@ type FormInputProps =
530530
*/
531531
const Input = React.forwardRef<React.ElementRef<typeof RadixControl>, FormInputProps>(
532532
(props: FormInputProps, forwardedRef) => {
533-
const telemetry = useTelemetry();
533+
const clerk = useClerk();
534534

535-
telemetry?.record(
535+
clerk.telemetry?.record(
536536
eventComponentMounted('Elements_Input', {
537537
type: props.type ?? false,
538538
// @ts-expect-error - Depending on type the props can be different

packages/elements/src/react/common/loading.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useClerk } from '@clerk/clerk-react';
12
import { eventComponentMounted } from '@clerk/shared/telemetry';
23
import type { OAuthProvider, SamlStrategy } from '@clerk/types';
34
import * as React from 'react';
@@ -18,7 +19,6 @@ import type { TSignUpStep } from '~/react/sign-up/step';
1819
import { SIGN_UP_STEPS } from '~/react/sign-up/step';
1920
import { SignUpVerificationCtx } from '~/react/sign-up/verifications';
2021
import { mapScopeToStrategy } from '~/react/utils/map-scope-to-strategy';
21-
import { useTelemetry } from '~/react/utils/telemetry';
2222

2323
type Strategy = OAuthProvider | SamlStrategy | 'metamask';
2424
type LoadingScope<T extends TSignInStep | TSignUpStep> = 'global' | `step:${T}` | `provider:${Strategy}` | undefined;
@@ -82,9 +82,9 @@ function isSignUpScope(scope: LoadingScope<TSignInStep | TSignUpStep>): scope is
8282
* </SignIn.Step>
8383
*/
8484
export function Loading({ children, scope }: LoadingProps) {
85-
const telemetry = useTelemetry();
85+
const clerk = useClerk();
8686

87-
telemetry?.record(eventComponentMounted('Elements_Loading', { scope: scope ?? false }));
87+
clerk.telemetry?.record(eventComponentMounted('Elements_Loading', { scope: scope ?? false }));
8888

8989
const signInRouterRef = SignInRouterCtx.useActorRef(true);
9090
const signUpRouterRef = SignUpRouterCtx.useActorRef(true);

packages/elements/src/react/sign-in/root.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { SignInRouterMachine } from '~/internals/machines/sign-in';
1010
import { consoleInspector } from '~/internals/utils/inspector';
1111
import { Router, useClerkRouter, useNextRouter } from '~/react/router';
1212
import { SignInRouterCtx } from '~/react/sign-in/context';
13-
import { useTelemetry } from '~/react/utils/telemetry';
1413

1514
import { Form } from '../common/form';
1615

@@ -83,9 +82,9 @@ export function SignInRoot({
8382
fallback = null,
8483
exampleMode,
8584
}: SignInRootProps): JSX.Element | null {
86-
const telemetry = useTelemetry();
85+
const clerk = useClerk();
8786

88-
telemetry?.record(
87+
clerk.telemetry?.record(
8988
eventComponentMounted('Elements_SignInRoot', {
9089
path,
9190
fallback: Boolean(fallback),

packages/elements/src/react/sign-in/step/step.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { useClerk } from '@clerk/clerk-react';
12
import { eventComponentMounted } from '@clerk/shared/telemetry';
23

34
import { ClerkElementsRuntimeError } from '~/internals/errors';
4-
import { useTelemetry } from '~/react/utils/telemetry';
55

66
import type { SignInChooseStrategyProps } from '../choose-strategy';
77
import { SignInChooseStrategy, SignInForgotPassword } from '../choose-strategy';
@@ -46,9 +46,9 @@ export type SignInStepProps =
4646
* </SignIn.Root>
4747
*/
4848
export function SignInStep(props: SignInStepProps) {
49-
const telemetry = useTelemetry();
49+
const clerk = useClerk();
5050

51-
telemetry?.record(eventComponentMounted('Elements_SignInStep', { name: props.name }));
51+
clerk.telemetry?.record(eventComponentMounted('Elements_SignInStep', { name: props.name }));
5252

5353
switch (props.name) {
5454
case SIGN_IN_STEPS['start']:

packages/elements/src/react/sign-up/root.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { SignUpRouterMachine } from '~/internals/machines/sign-up';
1111
import { consoleInspector } from '~/internals/utils/inspector';
1212
import { Router, useClerkRouter, useNextRouter } from '~/react/router';
1313
import { SignUpRouterCtx } from '~/react/sign-up/context';
14-
import { useTelemetry } from '~/react/utils/telemetry';
1514

1615
import { Form } from '../common/form';
1716

@@ -80,9 +79,9 @@ export function SignUpRoot({
8079
fallback = null,
8180
exampleMode,
8281
}: SignUpRootProps): JSX.Element | null {
83-
const telemetry = useTelemetry();
82+
const clerk = useClerk();
8483

85-
telemetry?.record(
84+
clerk.telemetry?.record(
8685
eventComponentMounted('Elements_SignUpRoot', {
8786
path,
8887
fallback: Boolean(fallback),

packages/elements/src/react/sign-up/step.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { useClerk } from '@clerk/clerk-react';
12
import { eventComponentMounted } from '@clerk/shared/telemetry';
23

34
import { ClerkElementsRuntimeError } from '~/internals/errors';
4-
import { useTelemetry } from '~/react/utils/telemetry';
55

66
import type { SignUpContinueProps } from './continue';
77
import { SignUpContinue } from './continue';
@@ -39,9 +39,9 @@ export type SignUpStepProps =
3939
* </SignUp.Root>
4040
*/
4141
export function SignUpStep(props: SignUpStepProps) {
42-
const telemetry = useTelemetry();
42+
const clerk = useClerk();
4343

44-
telemetry?.record(eventComponentMounted('Elements_SignUpStep', { name: props.name }));
44+
clerk.telemetry?.record(eventComponentMounted('Elements_SignUpStep', { name: props.name }));
4545

4646
switch (props.name) {
4747
case SIGN_UP_STEPS.start:

packages/elements/src/react/utils/telemetry.ts

-8
This file was deleted.

packages/shared/src/telemetry/collector.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@
1010
*
1111
* For more information, please see the telemetry documentation page: https://clerk.com/docs/telemetry
1212
*/
13-
import type { InstanceType } from '@clerk/types';
13+
import type {
14+
InstanceType,
15+
TelemetryCollector as TelemetryCollectorInterface,
16+
TelemetryEvent,
17+
TelemetryEventRaw,
18+
} from '@clerk/types';
1419

1520
import { parsePublishableKey } from '../keys';
1621
import { isTruthy } from '../underscore';
1722
import { TelemetryEventThrottler } from './throttler';
18-
import type { TelemetryCollectorOptions, TelemetryEvent, TelemetryEventRaw } from './types';
23+
import type { TelemetryCollectorOptions } from './types';
1924

2025
type TelemetryCollectorConfig = Pick<
2126
TelemetryCollectorOptions,
@@ -42,7 +47,7 @@ const DEFAULT_CONFIG: Partial<TelemetryCollectorConfig> = {
4247
endpoint: 'https://clerk-telemetry.com',
4348
};
4449

45-
export class TelemetryCollector {
50+
export class TelemetryCollector implements TelemetryCollectorInterface {
4651
#config: Required<TelemetryCollectorConfig>;
4752
#eventThrottler: TelemetryEventThrottler;
4853
#metadata: TelemetryMetadata = {} as TelemetryMetadata;

packages/shared/src/telemetry/events/component-mounted.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TelemetryEventRaw } from '../types';
1+
import type { TelemetryEventRaw } from '@clerk/types';
22

33
const EVENT_COMPONENT_MOUNTED = 'COMPONENT_MOUNTED' as const;
44
const EVENT_SAMPLING_RATE = 0.1;

packages/shared/src/telemetry/events/method-called.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TelemetryEventRaw } from '../types';
1+
import type { TelemetryEventRaw } from '@clerk/types';
22

33
const EVENT_METHOD_CALLED = 'METHOD_CALLED' as const;
44

packages/shared/src/telemetry/throttler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TelemetryEvent } from './types';
1+
import type { TelemetryEvent } from '@clerk/types';
22

33
type TtlInMilliseconds = number;
44

-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { InstanceType } from '@clerk/types';
2-
31
export type TelemetryCollectorOptions = {
42
/**
53
* If true, telemetry will not be collected.
@@ -38,38 +36,3 @@ export type TelemetryCollectorOptions = {
3836
*/
3937
sdkVersion?: string;
4038
};
41-
42-
export type TelemetryEvent = {
43-
event: string;
44-
/**
45-
* publishableKey
46-
*/
47-
pk?: string;
48-
/**
49-
* secretKey
50-
*/
51-
sk?: string;
52-
/**
53-
* instanceType
54-
*/
55-
it: InstanceType;
56-
/**
57-
* clerkVersion
58-
*/
59-
cv: string;
60-
/**
61-
* SDK
62-
*/
63-
sdk?: string;
64-
/**
65-
* SDK Version
66-
*/
67-
sdkv?: string;
68-
payload: Record<string, string | number | boolean>;
69-
};
70-
71-
export type TelemetryEventRaw<Payload = TelemetryEvent['payload']> = {
72-
event: TelemetryEvent['event'];
73-
eventSamplingRate?: number;
74-
payload: Payload;
75-
};

packages/types/src/clerk.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { TelemetryCollector } from 'telemetry';
2+
13
import type {
24
Appearance,
35
CreateOrganizationTheme,
@@ -11,6 +13,7 @@ import type {
1113
} from './appearance';
1214
import type { ClientResource } from './client';
1315
import type { CustomPage } from './customPages';
16+
import type { InstanceType } from './instance';
1417
import type { DisplayThemeJSON } from './json';
1518
import type { LocalizationResource } from './localization';
1619
import type { OAuthProvider, OAuthScope } from './oauth';
@@ -30,8 +33,6 @@ import type { ActiveSessionResource } from './session';
3033
import type { UserResource } from './user';
3134
import type { Autocomplete, DeepPartial, DeepSnakeToCamel } from './utils';
3235

33-
export type InstanceType = 'production' | 'development';
34-
3536
export type SDKMetadata = {
3637
name: string;
3738
version: string;
@@ -114,6 +115,8 @@ export interface Clerk {
114115
/** Current User. */
115116
user: UserResource | null | undefined;
116117

118+
telemetry: TelemetryCollector | undefined;
119+
117120
/**
118121
* Signs out the current user on single-session instances, or all users on multi-session instances
119122
* @param signOutCallback - Optional A callback that runs after sign out completes.

packages/types/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './factors';
1515
export * from './identificationLink';
1616
export * from './identifiers';
1717
export * from './image';
18+
export * from './instance';
1819
export * from './json';
1920
export * from './jwt';
2021
export * from './key';
@@ -45,6 +46,7 @@ export * from './strategies';
4546
export * from './theme';
4647
export * from './token';
4748
export * from './totp';
49+
export * from './telemetry';
4850
export * from './user';
4951
export * from './userOrganizationInvitation';
5052
export * from './userSettings';

packages/types/src/instance.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type InstanceType = 'production' | 'development';

packages/types/src/key.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { InstanceType } from './clerk';
1+
import type { InstanceType } from './instance';
22

33
export type PublishableKey = {
44
frontendApi: string;

packages/types/src/telemetry.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { InstanceType } from 'instance';
2+
3+
/**
4+
* @internal
5+
*/
6+
export type TelemetryEvent = {
7+
event: string;
8+
/**
9+
* publishableKey
10+
*/
11+
pk?: string;
12+
/**
13+
* secretKey
14+
*/
15+
sk?: string;
16+
/**
17+
* instanceType
18+
*/
19+
it: InstanceType;
20+
/**
21+
* clerkVersion
22+
*/
23+
cv: string;
24+
/**
25+
* SDK
26+
*/
27+
sdk?: string;
28+
/**
29+
* SDK Version
30+
*/
31+
sdkv?: string;
32+
payload: Record<string, string | number | boolean>;
33+
};
34+
35+
/**
36+
* @internal
37+
*/
38+
export type TelemetryEventRaw<Payload = TelemetryEvent['payload']> = {
39+
event: TelemetryEvent['event'];
40+
eventSamplingRate?: number;
41+
payload: Payload;
42+
};
43+
44+
export interface TelemetryCollector {
45+
isEnabled: boolean;
46+
isDebug: boolean;
47+
record(event: TelemetryEventRaw): void;
48+
}

0 commit comments

Comments
 (0)