Skip to content

Commit

Permalink
feat: add analytics events to track permission changes
Browse files Browse the repository at this point in the history
  • Loading branch information
raviteja83 committed Feb 3, 2025
1 parent 3496d29 commit 612539c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
10 changes: 10 additions & 0 deletions packages/hms-video-store/src/analytics/AnalyticsEventFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ export default class AnalyticsEventFactory {
});
}

static permissionChange(type: 'audio' | 'video', status: PermissionState) {
return new AnalyticsEvent({
name: 'permissionChanged',
level: AnalyticsEventLevel.INFO,
properties: {
message: `Perrmission for ${type} changed to ${status}`,
},
});
}

static deviceChange({
isUserSelection,
selection,
Expand Down
3 changes: 0 additions & 3 deletions packages/hms-video-store/src/error/ErrorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ export const ErrorCodes = {

// Track is publishing with no data, can happen when a whatsapp call is ongoing before 100ms call in mweb
NO_DATA: 3015,

// Permissions granted initially but later revoked
PERMISSION_DENIED_LATER: 3016,
},

WebrtcErrors: {
Expand Down
11 changes: 0 additions & 11 deletions packages/hms-video-store/src/error/ErrorFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ export const ErrorFactory = {
);
},

PermissionDeniedLater(action: HMSAction, deviceInfo: string, description = '') {
return new HMSTrackException(
ErrorCodes.TracksErrors.PERMISSION_DENIED_LATER,
'CantAccessCaptureDevice - permission denied later',
action,
`User denied permission to access capture device - ${deviceInfo}`,
description,
deviceInfo as HMSTrackExceptionTrackType,
);
},

DeviceNotAvailable(action: HMSAction, deviceInfo: string, description = '') {
return new HMSTrackException(
ErrorCodes.TracksErrors.DEVICE_NOT_AVAILABLE,
Expand Down
14 changes: 14 additions & 0 deletions packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class HMSLocalAudioTrack extends HMSAudioTrack {
super(stream, track, source);
stream.tracks.push(this);
this.addTrackEventListeners(track);
this.trackPermissions();

this.settings = settings;
// Replace the 'default' or invalid deviceId with the actual deviceId
Expand Down Expand Up @@ -315,6 +316,19 @@ export class HMSLocalAudioTrack extends HMSAudioTrack {
track.removeEventListener('unmute', this.handleTrackUnmute);
}

private trackPermissions = () => {
if (!navigator.permissions) {
HMSLogger.d(this.TAG, 'Permissions API not supported');
return;
}
// @ts-ignore
navigator.permissions.query({ name: 'microphone' }).then(permission => {
permission.onchange = () => {
this.eventBus.analytics.publish(AnalyticsEventFactory.permissionChange('audio', permission.state));
};
});
};

private handleTrackMute = () => {
HMSLogger.d(this.TAG, 'muted natively');
const reason = document.visibilityState === 'hidden' ? 'visibility-change' : 'incoming-call';
Expand Down
14 changes: 14 additions & 0 deletions packages/hms-video-store/src/media/tracks/HMSLocalVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class HMSLocalVideoTrack extends HMSVideoTrack {
) {
super(stream, track, source);
this.addTrackEventListeners(track);
this.trackPermissions();
stream.tracks.push(this);
this.setVideoHandler(new VideoElementManager(this));
this.settings = settings;
Expand Down Expand Up @@ -526,6 +527,19 @@ export class HMSLocalVideoTrack extends HMSVideoTrack {
track.removeEventListener('unmute', this.handleTrackUnmuteNatively);
}

private trackPermissions = () => {
if (!navigator.permissions) {
HMSLogger.d(this.TAG, 'Permissions API not supported');
return;
}
// @ts-ignore
navigator.permissions.query({ name: 'camera' }).then(permission => {
permission.onchange = () => {
this.eventBus.analytics.publish(AnalyticsEventFactory.permissionChange('video', permission.state));
};
});
};

private handleTrackMute = () => {
HMSLogger.d(this.TAG, 'muted natively', document.visibilityState);
const reason = document.visibilityState === 'hidden' ? 'visibility-change' : 'incoming-call';
Expand Down

0 comments on commit 612539c

Please sign in to comment.