react-native-clarity 3.1.1 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -59
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/microsoft/clarity/reactnative/ClarityEmitter.kt +55 -0
- package/android/src/main/java/com/microsoft/clarity/reactnative/ClarityModule.kt +12 -22
- package/android/src/main/java/com/microsoft/clarity/reactnative/ClarityPackage.kt +4 -1
- package/ios/Clarity.m +38 -22
- package/ios/Clarity.xcodeproj/project.pbxproj +6 -0
- package/ios/ClarityEmitter.h +6 -0
- package/ios/ClarityEmitter.m +50 -0
- package/lib/commonjs/index.js +198 -84
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/index.js +195 -85
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/index.d.ts +130 -68
- package/lib/typescript/index.d.ts.map +1 -1
- package/package.json +4 -3
- package/react-native-clarity.podspec +1 -1
- package/src/index.tsx +268 -120
package/src/index.tsx
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
NativeModules,
|
|
3
|
+
NativeEventEmitter,
|
|
4
|
+
Platform,
|
|
5
|
+
EmitterSubscription,
|
|
6
|
+
} from 'react-native';
|
|
2
7
|
|
|
3
8
|
const LINKING_ERROR =
|
|
4
9
|
`The package 'react-native-clarity' doesn't seem to be linked. Make sure: \n\n` +
|
|
@@ -9,44 +14,58 @@ const LINKING_ERROR =
|
|
|
9
14
|
'- You rebuilt the app after installing the package\n' +
|
|
10
15
|
'- You are not using Expo Go\n';
|
|
11
16
|
|
|
17
|
+
const ReferToDocs =
|
|
18
|
+
'Please check the docs at https://www.npmjs.com/package/react-native-clarity for assistance.';
|
|
19
|
+
|
|
12
20
|
const Clarity = NativeModules.Clarity;
|
|
21
|
+
const ClarityEmitter = new NativeEventEmitter(NativeModules.ClarityEmitter);
|
|
13
22
|
|
|
14
|
-
const SupportedPlatforms = ['android'];
|
|
23
|
+
const SupportedPlatforms = ['android', 'ios'];
|
|
15
24
|
|
|
16
25
|
let SupportWarningWasShown = false;
|
|
17
26
|
|
|
18
27
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* @param userId [OPTIONAL default = null] A custom identifier for the current user. If passed as null, the user id
|
|
22
|
-
* will be auto generated. The user id in general is sticky across sessions.
|
|
23
|
-
* The provided user id must follow these conditions:
|
|
24
|
-
* 1. Cannot be an empty string.
|
|
25
|
-
* 2. Should be base36 and smaller than "1Z141Z4".
|
|
26
|
-
* @param logLevel [OPTIONAL default = LogLevel.None] The level of logging to show in the device logcat stream.
|
|
27
|
-
* @param allowMeteredNetworkUsage [OPTIONAL default = false] Allows uploading session data to the servers on device metered network.
|
|
28
|
-
* @param enableWebViewCapture [OPTIONAL default = true] Allows Clarity to capture the web views DOM content.
|
|
29
|
-
* @param allowedDomains [OPTIONAL default = ["*"]] The whitelisted domains to allow Clarity to capture their DOM content.
|
|
30
|
-
* If it contains "*" as an element, all domains will be captured.
|
|
31
|
-
* Note: iOS currently does not support allowedDomains feature.
|
|
32
|
-
* @param disableOnLowEndDevices [OPTIONAL default = false] Disable Clarity on low-end devices.
|
|
33
|
-
* @param maximumDailyNetworkUsageInMB [OPTIONAL default = null] Maximum daily network usage for Clarity (null = No limit). When the limit is reached, Clarity will turn on lean mode.
|
|
34
|
-
* Note: iOS currently does not support limiting network usage.
|
|
35
|
-
* @param enableIOS_experimental [OPTIONAL default = false] Experimental flag to enable Clarity on iOS platform.
|
|
28
|
+
* A class that allows you to configure the Clarity SDK behavior.
|
|
36
29
|
*/
|
|
37
30
|
export interface ClarityConfig {
|
|
38
|
-
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated This property is deprecated and would be removed in a future major version. Use {@linkcode setCustomSessionId} instead.
|
|
33
|
+
*
|
|
34
|
+
* The unique identifier associated with the application user. This ID persists across multiple sessions on the same device.
|
|
35
|
+
*
|
|
36
|
+
* **Notes:**
|
|
37
|
+
* - If {@linkcode userId} isn't provided, a random one is generated automatically.
|
|
38
|
+
* - Must be a base-36 string smaller than `1Z141Z4`.
|
|
39
|
+
* - If an invalid {@linkcode userId} is supplied:
|
|
40
|
+
* - If `customUserId` isn't specified, {@linkcode userId} acts as the `customUserId`, and a new random {@linkcode userId} is assigned.
|
|
41
|
+
* - If `customUserId` is specified, the invalid {@linkcode userID} is ignored.
|
|
42
|
+
* - For more flexibility in user identification, consider using the {@linkcode setCustomUserId} API. However, keep in mind that `customUserId` length must be between 1 and 255 characters.
|
|
43
|
+
*/
|
|
44
|
+
userId?: string;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The level of logging to show in the device’s logging stream while debugging. By default, the SDK logs nothing.
|
|
48
|
+
*/
|
|
39
49
|
logLevel?: LogLevel;
|
|
40
|
-
allowMeteredNetworkUsage?: boolean;
|
|
41
|
-
enableWebViewCapture?: boolean;
|
|
42
|
-
allowedDomains?: string[];
|
|
43
|
-
disableOnLowEndDevices?: Boolean;
|
|
44
|
-
maximumDailyNetworkUsageInMB?: number;
|
|
45
|
-
enableIOS_experimental?: boolean;
|
|
46
50
|
}
|
|
47
51
|
|
|
52
|
+
const ValidConfigKeys = new Set(
|
|
53
|
+
Object.keys({
|
|
54
|
+
userId: undefined, // Deprecated.
|
|
55
|
+
logLevel: undefined,
|
|
56
|
+
} as ClarityConfig)
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const RemovedDynamicConfigKeys = new Set([
|
|
60
|
+
'allowMeteredNetworkUsage',
|
|
61
|
+
'enableWebViewCapture',
|
|
62
|
+
'allowedDomains',
|
|
63
|
+
'disableOnLowEndDevices',
|
|
64
|
+
'maximumDailyNetworkUsageInMB',
|
|
65
|
+
]);
|
|
66
|
+
|
|
48
67
|
/**
|
|
49
|
-
* The level of logging to show in the device
|
|
68
|
+
* The level of logging to show in the device logging stream.
|
|
50
69
|
*/
|
|
51
70
|
export enum LogLevel {
|
|
52
71
|
Verbose = 'Verbose',
|
|
@@ -57,15 +76,19 @@ export enum LogLevel {
|
|
|
57
76
|
None = 'None',
|
|
58
77
|
}
|
|
59
78
|
|
|
79
|
+
class ClarityError extends Error {
|
|
80
|
+
constructor(message: string) {
|
|
81
|
+
super(message);
|
|
82
|
+
this.name = 'ClarityError';
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
60
86
|
function isClarityUnavailable(): boolean {
|
|
61
87
|
if (!SupportedPlatforms.includes(Platform.OS)) {
|
|
62
|
-
let warningMessage = 'Clarity supports ' + SupportedPlatforms.join(', ') + ' only for now.';
|
|
63
|
-
if (Platform.OS === 'ios') {
|
|
64
|
-
warningMessage = `${warningMessage} To enable experimental iOS support, set the 'enableIOS_experimental' config value to true.`;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
88
|
if (!SupportWarningWasShown) {
|
|
68
|
-
console.warn(
|
|
89
|
+
console.warn(
|
|
90
|
+
'Clarity supports ' + SupportedPlatforms.join(', ') + ' only for now.'
|
|
91
|
+
);
|
|
69
92
|
SupportWarningWasShown = true;
|
|
70
93
|
}
|
|
71
94
|
|
|
@@ -80,177 +103,302 @@ function isClarityUnavailable(): boolean {
|
|
|
80
103
|
return false;
|
|
81
104
|
}
|
|
82
105
|
|
|
106
|
+
function validateClarityConfig(config?: ClarityConfig) {
|
|
107
|
+
if (typeof config === 'undefined') {
|
|
108
|
+
// Allow using defaults when config is `null` or `undefined`.
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (typeof config !== 'object') {
|
|
113
|
+
throw new ClarityError(
|
|
114
|
+
`Invalid Clarity initialization \`config\` argument. Expected an object, but received ${typeof config}. ${ReferToDocs}`
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (config?.logLevel && !Object.values(LogLevel).includes(config.logLevel)) {
|
|
119
|
+
console.error(
|
|
120
|
+
`Invalid ClarityConfig \`logLevel\` property value: ${
|
|
121
|
+
config.logLevel
|
|
122
|
+
}. Valid values are: ${Object.values(LogLevel).join(
|
|
123
|
+
', '
|
|
124
|
+
)}. ${ReferToDocs}`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const key in config) {
|
|
129
|
+
if (RemovedDynamicConfigKeys.has(key)) {
|
|
130
|
+
throw new ClarityError(
|
|
131
|
+
`The ClarityConfig \`${key}\` property is removed and it's configurable from Clarity dashboard.`
|
|
132
|
+
);
|
|
133
|
+
} else if (key === 'enableIOS_experimental') {
|
|
134
|
+
const errorMessage =
|
|
135
|
+
'The ClarityConfig `enableIOS_experimental` property is removed and iOS is supported by default.';
|
|
136
|
+
if ((config as any).enableIOS_experimental) {
|
|
137
|
+
// Proceed with initialization if the user intended to enable iOS support.
|
|
138
|
+
console.error(errorMessage);
|
|
139
|
+
} else {
|
|
140
|
+
throw new ClarityError(errorMessage);
|
|
141
|
+
}
|
|
142
|
+
} else if (key === 'userId') {
|
|
143
|
+
console.warn(
|
|
144
|
+
'The ClarityConfig `userId` property is deprecated and would be removed in a future major version. Use `setCustomUserId()` instead.'
|
|
145
|
+
);
|
|
146
|
+
} else if (!ValidConfigKeys.has(key)) {
|
|
147
|
+
console.warn(`Invalid ClarityConfig property: \`${key}\`.`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
83
152
|
/**
|
|
84
|
-
*
|
|
85
|
-
* @param
|
|
86
|
-
*
|
|
153
|
+
* @param projectId - The unique identifier assigned to your Clarity project. You can find it on the **Settings** page of Clarity dashboard. This ID is essential for routing data to the correct Clarity project (required).
|
|
154
|
+
* @param config - Configuration of Clarity that tunes the SDK behavior (for example, which log level to use, and so on).
|
|
155
|
+
*
|
|
156
|
+
* **Notes:**
|
|
157
|
+
* - The initialization function is asynchronous, meaning it returns before Clarity is fully initialized.
|
|
158
|
+
* - For actions that require Clarity to be fully initialized, it's recommended to use the {@linkcode setOnSessionStartedCallback} function.
|
|
87
159
|
*/
|
|
88
160
|
export function initialize(projectId: string, config?: ClarityConfig) {
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
) {
|
|
93
|
-
throw Error(
|
|
94
|
-
'Invalid Clarity initialization arguments. Please check the docs for assitance.'
|
|
161
|
+
if (typeof projectId !== 'string') {
|
|
162
|
+
throw new ClarityError(
|
|
163
|
+
`Invalid Clarity initialization \`projectId\` argument. Expected a string, but received ${typeof projectId}. ${ReferToDocs}`
|
|
95
164
|
);
|
|
96
165
|
}
|
|
97
166
|
|
|
167
|
+
validateClarityConfig(config);
|
|
168
|
+
|
|
98
169
|
// applying default values
|
|
99
|
-
let {
|
|
100
|
-
userId = null,
|
|
101
|
-
logLevel = LogLevel.None,
|
|
102
|
-
allowMeteredNetworkUsage = false,
|
|
103
|
-
enableWebViewCapture = true,
|
|
104
|
-
allowedDomains = ['*'],
|
|
105
|
-
disableOnLowEndDevices = false,
|
|
106
|
-
maximumDailyNetworkUsageInMB = null,
|
|
107
|
-
enableIOS_experimental = false,
|
|
108
|
-
} = config ?? {};
|
|
109
|
-
|
|
110
|
-
if (enableIOS_experimental === true && !SupportedPlatforms.includes('ios')) {
|
|
111
|
-
SupportedPlatforms.push('ios');
|
|
112
|
-
}
|
|
170
|
+
let { userId = null, logLevel = LogLevel.None } = config ?? {};
|
|
113
171
|
|
|
114
172
|
if (isClarityUnavailable()) {
|
|
115
173
|
return;
|
|
116
174
|
}
|
|
117
175
|
|
|
118
|
-
|
|
119
|
-
let enableDailyNetworkUsageLimit = maximumDailyNetworkUsageInMB != null;
|
|
120
|
-
let refinedMaximumDailyNetworkUsageInMB = maximumDailyNetworkUsageInMB ?? 0;
|
|
121
|
-
|
|
122
|
-
Clarity.initialize(
|
|
123
|
-
projectId,
|
|
124
|
-
userId,
|
|
125
|
-
logLevel,
|
|
126
|
-
allowMeteredNetworkUsage,
|
|
127
|
-
enableWebViewCapture,
|
|
128
|
-
allowedDomains,
|
|
129
|
-
disableOnLowEndDevices,
|
|
130
|
-
enableDailyNetworkUsageLimit,
|
|
131
|
-
refinedMaximumDailyNetworkUsageInMB
|
|
132
|
-
);
|
|
176
|
+
Clarity.initialize(projectId, userId, logLevel);
|
|
133
177
|
}
|
|
134
178
|
|
|
135
179
|
/**
|
|
136
|
-
* Pauses the Clarity capturing
|
|
180
|
+
* Pauses the Clarity session capturing until a call to the {@linkcode resume} function is made.
|
|
181
|
+
*
|
|
182
|
+
* @returns {Promise<boolean>} A Promise that resolves with `true` if Clarity session capturing was paused successfully; otherwise `false`.
|
|
137
183
|
*/
|
|
138
|
-
export function pause(): Promise<boolean
|
|
184
|
+
export function pause(): Promise<boolean> {
|
|
139
185
|
if (isClarityUnavailable()) {
|
|
140
|
-
return Promise.resolve(
|
|
186
|
+
return Promise.resolve(false);
|
|
141
187
|
}
|
|
142
188
|
|
|
143
189
|
return Clarity.pause();
|
|
144
190
|
}
|
|
145
191
|
|
|
146
192
|
/**
|
|
147
|
-
* Resumes the Clarity capturing
|
|
148
|
-
*
|
|
193
|
+
* Resumes the Clarity session capturing only if it was previously paused by a call to the {@linkcode pause} function.
|
|
194
|
+
*
|
|
195
|
+
* @returns {Promise<boolean>} A Promise that resolves with `true` if Clarity session capturing was resumed successfully; otherwise `false`.
|
|
149
196
|
*/
|
|
150
|
-
export function resume(): Promise<boolean
|
|
197
|
+
export function resume(): Promise<boolean> {
|
|
151
198
|
if (isClarityUnavailable()) {
|
|
152
|
-
return Promise.resolve(
|
|
199
|
+
return Promise.resolve(false);
|
|
153
200
|
}
|
|
154
201
|
|
|
155
202
|
return Clarity.resume();
|
|
156
203
|
}
|
|
157
204
|
|
|
158
205
|
/**
|
|
159
|
-
*
|
|
206
|
+
* Checks if Clarity session capturing is currently paused based on an earlier call to the {@linkcode pause} function.
|
|
207
|
+
*
|
|
208
|
+
* @returns {Promise<boolean>} A Promise that resolves with `true` if Clarity session capturing is currently in the paused state based on an earlier call to the {@linkcode pause} function; otherwise `false`.
|
|
160
209
|
*/
|
|
161
|
-
export function isPaused(): Promise<
|
|
210
|
+
export function isPaused(): Promise<boolean> {
|
|
162
211
|
if (isClarityUnavailable()) {
|
|
163
|
-
return Promise.resolve(
|
|
212
|
+
return Promise.resolve(false);
|
|
164
213
|
}
|
|
165
214
|
|
|
166
215
|
return Clarity.isPaused();
|
|
167
216
|
}
|
|
168
217
|
|
|
169
218
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
219
|
+
* Forces Clarity to start a new session asynchronously.
|
|
220
|
+
*
|
|
221
|
+
* @param callback - A callback that is invoked when the new session starts. The callback receives the new session ID as a string parameter.
|
|
222
|
+
*
|
|
223
|
+
* **Notes:**
|
|
224
|
+
* - This function is asynchronous, meaning it returns before the new session is started.
|
|
225
|
+
* - Use the {@linkcode callback} parameter to execute logic that needs to run after the new session begins.
|
|
226
|
+
* - Events that occur before invoking the callback are associated with the previous session.
|
|
227
|
+
* - To ensure proper association of custom tags, user ID, or session ID with the new session, set them within the callback.
|
|
178
228
|
*/
|
|
179
|
-
export function
|
|
229
|
+
export function startNewSession(callback: (sessionId: String) => void): void {
|
|
180
230
|
if (isClarityUnavailable()) {
|
|
181
|
-
return
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
Clarity.startNewSession(callback);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Sets a custom user ID for the current session. This ID can be used to filter sessions on the Clarity dashboard.
|
|
239
|
+
*
|
|
240
|
+
* @param customUserId - The custom user ID to associate with the current session. The value must be a nonempty string, with a maximum length of 255 characters, and can't consist only of whitespace.
|
|
241
|
+
* @returns {Promise<boolean>} A Promise that resolves with `true` if the custom user ID was set successfully; otherwise `false`.
|
|
242
|
+
*
|
|
243
|
+
* **Notes:**
|
|
244
|
+
* - To ensure that the custom user ID is associated with the correct session, it's recommended to call this function within the callbacks of {@linkcode setOnSessionStartedCallback} or {@linkcode startNewSession}.
|
|
245
|
+
* - Unlike the `userID`, the {@linkcode customUserId} value has fewer restrictions.
|
|
246
|
+
* - We recommend **not** to set any Personally Identifiable Information (PII) values inside this field.
|
|
247
|
+
*/
|
|
248
|
+
export function setCustomUserId(customUserId: string): Promise<boolean> {
|
|
249
|
+
if (isClarityUnavailable()) {
|
|
250
|
+
return Promise.resolve(false);
|
|
182
251
|
}
|
|
183
252
|
|
|
184
253
|
return Clarity.setCustomUserId(customUserId);
|
|
185
254
|
}
|
|
186
255
|
|
|
187
256
|
/**
|
|
188
|
-
* Sets a custom session
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
257
|
+
* Sets a custom session ID for the current session. This ID can be used to filter sessions on the Clarity dashboard.
|
|
258
|
+
*
|
|
259
|
+
* @param customSessionId - The custom session ID to associate with the current session. The value must be a nonempty string, with a maximum length of 255 characters, and can't consist only of whitespace.
|
|
260
|
+
* @returns {Promise<boolean>} A Promise that resolves with `true` if the custom session ID was set successfully; otherwise `false`.
|
|
261
|
+
*
|
|
262
|
+
* **Notes:**
|
|
263
|
+
* - To ensure that the custom session ID is associated with the correct session, it's recommended to call this function within the callbacks of {@linkcode setOnSessionStartedCallback} or {@linkcode startNewSession}.
|
|
193
264
|
*/
|
|
194
|
-
export function setCustomSessionId(customSessionId: string): Promise<boolean
|
|
265
|
+
export function setCustomSessionId(customSessionId: string): Promise<boolean> {
|
|
195
266
|
if (isClarityUnavailable()) {
|
|
196
|
-
return Promise.resolve(
|
|
267
|
+
return Promise.resolve(false);
|
|
197
268
|
}
|
|
198
269
|
|
|
199
270
|
return Clarity.setCustomSessionId(customSessionId);
|
|
200
271
|
}
|
|
201
272
|
|
|
202
273
|
/**
|
|
203
|
-
* Sets a custom tag for the current session.
|
|
204
|
-
*
|
|
205
|
-
* @param
|
|
274
|
+
* Sets a custom tag for the current session. This tag can be used to filter sessions on the Clarity dashboard.
|
|
275
|
+
*
|
|
276
|
+
* @param key - The key for the custom tag. The value must be a nonempty string, with a maximum length of 255 characters, and can't consist only of whitespace.
|
|
277
|
+
* @param value - The value for the custom tag. The value must be a nonempty string, with a maximum length of 255 characters, and can't consist only of whitespace.
|
|
278
|
+
* @returns {Promise<boolean>} A Promise that resolves with `true` if the custom tag was set successfully; otherwise `false`.
|
|
279
|
+
*
|
|
280
|
+
* **Notes:**
|
|
281
|
+
* - To ensure that the custom tag is associated with the correct session, it's recommended to call this function within the callbacks of {@linkcode setOnSessionStartedCallback} or {@linkcode startNewSession}.
|
|
206
282
|
*/
|
|
207
|
-
export function setCustomTag(key: string, value: string): Promise<boolean
|
|
283
|
+
export function setCustomTag(key: string, value: string): Promise<boolean> {
|
|
208
284
|
if (isClarityUnavailable()) {
|
|
209
|
-
return Promise.resolve(
|
|
285
|
+
return Promise.resolve(false);
|
|
210
286
|
}
|
|
211
287
|
|
|
212
288
|
return Clarity.setCustomTag(key, value);
|
|
213
289
|
}
|
|
214
290
|
|
|
215
291
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
292
|
+
* Sends a custom event to the current Clarity session. These custom events can be used to track specific user interactions or actions that Clarity's built-in event tracking doesn't capture.
|
|
293
|
+
*
|
|
294
|
+
* @param value - The name of the custom event. The value must be a nonempty string, with a maximum length of 254 characters, and can't consist only of whitespace.
|
|
295
|
+
* @returns {Promise<boolean>} A Promise that resolves with `true` if the custom event was sent successfully; otherwise `false`.
|
|
296
|
+
*
|
|
297
|
+
* **Notes:**
|
|
298
|
+
* - This API can be called multiple times per page to track various user actions.
|
|
299
|
+
* - Each custom event is logged individually and can be filtered, viewed, and analyzed on the Clarity dashboard.
|
|
300
|
+
*/
|
|
301
|
+
export function sendCustomEvent(value: string): Promise<boolean> {
|
|
302
|
+
if (isClarityUnavailable()) {
|
|
303
|
+
return Promise.resolve(false);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return Clarity.sendCustomEvent(value);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
let onSessionStartedSubscription: EmitterSubscription | null = null;
|
|
310
|
+
var onSessionStartedCallback: ((sessionId: string) => void) | null = null;
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Sets a callback function that's invoked whenever a new Clarity session starts or an existing session is resumed at app startup.
|
|
314
|
+
*
|
|
315
|
+
* @param callback - The callback to be invoked whenever a Clarity session starts. The callback receives the new or resumed session ID as a string parameter.
|
|
316
|
+
* @returns {boolean} `true` if the callback was set successfully; otherwise `false`.
|
|
317
|
+
*
|
|
318
|
+
* **Notes:**
|
|
319
|
+
* - If the callback is set after a session has already started, the callback is invoked right away with the current session ID.
|
|
320
|
+
* - The specified callback is guaranteed to run on the main thread.
|
|
321
|
+
*/
|
|
322
|
+
export function setOnSessionStartedCallback(
|
|
323
|
+
callback: (sessionId: String) => void
|
|
324
|
+
): boolean {
|
|
325
|
+
if (isClarityUnavailable()) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
onSessionStartedCallback = callback;
|
|
330
|
+
|
|
331
|
+
if (onSessionStartedSubscription === null) {
|
|
332
|
+
onSessionStartedSubscription = ClarityEmitter.addListener(
|
|
333
|
+
'sessionStarted',
|
|
334
|
+
({ sessionId }) => {
|
|
335
|
+
if (onSessionStartedCallback !== null) {
|
|
336
|
+
onSessionStartedCallback(sessionId);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* This function allows you to provide a custom screen name tag that's added as a suffix to the base screen name. The base name is automatically generated based on the current activity in Android, or the currently presented view controller's title or type in iOS.
|
|
347
|
+
*
|
|
348
|
+
* @param screenName - The desired screen name tag. The value must be a nonempty string, with a maximum length of 255 characters, and can't consist only of whitespace nor contain `/` character. Set to `null` to reset.
|
|
349
|
+
* @returns {Promise<boolean>} A Promise that resolves with `true` if the specified screen name tag was set successfully; otherwise `false`.
|
|
350
|
+
*
|
|
351
|
+
* **Example:**
|
|
352
|
+
* - If the presented iOS view controller is a `UIViewController`, and `setCurrentScreenName("Settings")` is called, the screen name is tracked as "UIViewController/Settings".
|
|
353
|
+
* - If `setCurrentScreenName(nil)` is called on the same view controller, the screen name is tracked as "UIViewController".
|
|
354
|
+
*
|
|
355
|
+
* **Notes:**
|
|
356
|
+
* - Clarity starts a new page whenever either the base screen name (generated from the Android activity or iOS view controller) or the custom name tag changes.
|
|
357
|
+
* - To mask or disallow a screen, specify the base screen name without the custom tag suffix. For example, to mask the iOS view controller in the previous example, mask the "UIViewController" screen instead of "UIViewController/Settings".
|
|
358
|
+
* - The screen name tag is set globally and persists across all subsequent Android activities or iOS view controllers until explicitly reset.
|
|
221
359
|
*/
|
|
222
360
|
export function setCurrentScreenName(
|
|
223
361
|
screenName: string | null
|
|
224
|
-
): Promise<boolean
|
|
362
|
+
): Promise<boolean> {
|
|
225
363
|
if (isClarityUnavailable()) {
|
|
226
|
-
return Promise.resolve(
|
|
364
|
+
return Promise.resolve(false);
|
|
227
365
|
}
|
|
228
366
|
|
|
229
367
|
return Clarity.setCurrentScreenName(screenName);
|
|
230
368
|
}
|
|
231
369
|
|
|
232
370
|
/**
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
371
|
+
* @deprecated This function is deprecated and would be removed in a future major version. Use {@linkcode getCurrentSessionUrl} instead.
|
|
372
|
+
*
|
|
373
|
+
* Returns the ID of the currently active Clarity session if a session has already started; otherwise `undefined`.
|
|
374
|
+
*
|
|
375
|
+
* @returns {Promise<string | undefined>} A Promise that resolves with a string representing the ID of the currently active Clarity session if a session has already started; otherwise `undefined`.
|
|
376
|
+
*
|
|
377
|
+
* **Note:**
|
|
378
|
+
* - The session ID can be used to correlate Clarity sessions with other telemetry services.
|
|
379
|
+
* - Initially, this function might return `undefined` until a Clarity session begins.
|
|
380
|
+
* - To ensure a valid session ID, use this method within the callbacks of {@linkcode setOnSessionStartedCallback} or {@linkcode startNewSession}.
|
|
236
381
|
*/
|
|
237
|
-
export function getCurrentSessionId(): Promise<string> {
|
|
382
|
+
export function getCurrentSessionId(): Promise<string | undefined> {
|
|
238
383
|
if (isClarityUnavailable()) {
|
|
239
|
-
return Promise.resolve(
|
|
384
|
+
return Promise.resolve(undefined);
|
|
240
385
|
}
|
|
241
386
|
|
|
242
387
|
return Clarity.getCurrentSessionId();
|
|
243
388
|
}
|
|
244
389
|
|
|
245
390
|
/**
|
|
246
|
-
* Returns the
|
|
247
|
-
* analytics tools that the developer may be using.
|
|
391
|
+
* Returns the URL of the current Clarity session recording on the Clarity dashboard if a session has already started; otherwise `undefined`.
|
|
248
392
|
*
|
|
249
|
-
* @returns
|
|
393
|
+
* @returns {Promise<string | undefined>} A Promise that resolves with a string representing the URL of the current Clarity session recording on the Clarity dashboard if a session has already started; otherwise `undefined`.
|
|
394
|
+
*
|
|
395
|
+
* **Notes:**
|
|
396
|
+
* - Initially, this function might return `undefined` until a Clarity session begins.
|
|
397
|
+
* - To ensure a valid session URL, use this method within the callbacks of {@linkcode setOnSessionStartedCallback} or {@linkcode startNewSession}.
|
|
250
398
|
*/
|
|
251
|
-
export function getCurrentSessionUrl(): Promise<string> {
|
|
399
|
+
export function getCurrentSessionUrl(): Promise<string | undefined> {
|
|
252
400
|
if (isClarityUnavailable()) {
|
|
253
|
-
return Promise.resolve(
|
|
401
|
+
return Promise.resolve(undefined);
|
|
254
402
|
}
|
|
255
403
|
|
|
256
404
|
return Clarity.getCurrentSessionUrl();
|