2022-10-20 09:11:27 +00:00
|
|
|
import { IReduxState } from '../app/types';
|
2019-05-21 10:32:20 +00:00
|
|
|
import {
|
|
|
|
CONFERENCE_JOINED,
|
|
|
|
CONFERENCE_WILL_LEAVE,
|
|
|
|
SET_ROOM
|
2022-09-06 12:51:50 +00:00
|
|
|
} from '../base/conference/actionTypes';
|
|
|
|
import { SET_CONFIG } from '../base/config/actionTypes';
|
|
|
|
import { SET_NETWORK_INFO } from '../base/net-info/actionTypes';
|
|
|
|
import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
|
2019-05-21 10:32:20 +00:00
|
|
|
import {
|
|
|
|
TRACK_ADDED,
|
|
|
|
TRACK_REMOVED,
|
|
|
|
TRACK_UPDATED
|
2022-09-06 12:51:50 +00:00
|
|
|
} from '../base/tracks/actionTypes';
|
|
|
|
import {
|
|
|
|
getLocalAudioTrack,
|
|
|
|
getLocalVideoTrack
|
|
|
|
} from '../base/tracks/functions';
|
2019-05-21 10:32:20 +00:00
|
|
|
|
2019-08-22 15:08:34 +00:00
|
|
|
import { createLocalTracksDurationEvent, createNetworkInfoEvent } from './AnalyticsEvents';
|
2020-05-20 10:57:03 +00:00
|
|
|
import { UPDATE_LOCAL_TRACKS_DURATION } from './actionTypes';
|
2020-01-30 16:57:41 +00:00
|
|
|
import { createHandlers, initAnalytics, resetAnalytics, sendAnalytics } from './functions';
|
2019-05-21 10:32:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the duration of the local tracks.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {Object} - The local tracks duration.
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
function calculateLocalTrackDuration(state: IReduxState) {
|
2019-05-21 10:32:20 +00:00
|
|
|
const now = Date.now();
|
|
|
|
const { localTracksDuration } = state['features/analytics'];
|
|
|
|
const { conference } = state['features/base/conference'];
|
|
|
|
const { audio, video } = localTracksDuration;
|
|
|
|
const { camera, desktop } = video;
|
|
|
|
const tracks = state['features/base/tracks'];
|
|
|
|
const audioTrack = getLocalAudioTrack(tracks);
|
|
|
|
const videoTrack = getLocalVideoTrack(tracks);
|
|
|
|
const newDuration = { ...localTracksDuration };
|
|
|
|
|
|
|
|
if (!audioTrack || audioTrack.muted || !conference) {
|
|
|
|
newDuration.audio = {
|
|
|
|
startedTime: -1,
|
|
|
|
value: audio.value + (audio.startedTime === -1 ? 0 : now - audio.startedTime)
|
|
|
|
};
|
|
|
|
} else if (audio.startedTime === -1) {
|
|
|
|
newDuration.audio.startedTime = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!videoTrack || videoTrack.muted || !conference) {
|
|
|
|
newDuration.video = {
|
|
|
|
camera: {
|
|
|
|
startedTime: -1,
|
|
|
|
value: camera.value + (camera.startedTime === -1 ? 0 : now - camera.startedTime)
|
|
|
|
},
|
|
|
|
desktop: {
|
|
|
|
startedTime: -1,
|
|
|
|
value: desktop.value + (desktop.startedTime === -1 ? 0 : now - desktop.startedTime)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const { videoType } = videoTrack;
|
2017-09-01 19:14:03 +00:00
|
|
|
|
2022-09-06 12:51:50 +00:00
|
|
|
if (video[videoType as keyof typeof video].startedTime === -1) {
|
|
|
|
newDuration.video[videoType as keyof typeof video].startedTime = now;
|
2019-05-21 10:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...localTracksDuration,
|
|
|
|
...newDuration
|
|
|
|
};
|
|
|
|
}
|
2017-09-01 19:14:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware which intercepts config actions to handle evaluating analytics
|
|
|
|
* config based on the config stored in the store.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
2020-01-30 16:57:41 +00:00
|
|
|
switch (action.type) {
|
|
|
|
case SET_CONFIG:
|
2019-01-24 15:37:55 +00:00
|
|
|
if (navigator.product === 'ReactNative') {
|
2021-03-16 15:59:33 +00:00
|
|
|
// Resetting the analytics is currently not needed for web because
|
2019-01-24 15:37:55 +00:00
|
|
|
// the user will be redirected to another page and new instance of
|
|
|
|
// Analytics will be created and initialized.
|
|
|
|
resetAnalytics();
|
|
|
|
}
|
2020-01-30 16:57:41 +00:00
|
|
|
break;
|
|
|
|
case SET_ROOM: {
|
|
|
|
// createHandlers is called before the SET_ROOM action is executed in order for Amplitude to initialize before
|
|
|
|
// the deeplinking logic is executed (after the SET_ROOM action) so that the Amplitude device id is available
|
|
|
|
// if needed.
|
|
|
|
const createHandlersPromise = createHandlers(store);
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
createHandlersPromise.then(handlers => {
|
|
|
|
initAnalytics(store, handlers);
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-05-21 10:32:20 +00:00
|
|
|
}
|
2019-01-24 15:37:55 +00:00
|
|
|
|
2019-05-21 10:32:20 +00:00
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
case CONFERENCE_JOINED: {
|
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: UPDATE_LOCAL_TRACKS_DURATION,
|
|
|
|
localTracksDuration: {
|
|
|
|
...calculateLocalTrackDuration(state),
|
|
|
|
conference: {
|
|
|
|
startedTime: Date.now(),
|
|
|
|
value: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-01-24 15:37:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-05-25 20:30:28 +00:00
|
|
|
|
2019-05-21 10:32:20 +00:00
|
|
|
case CONFERENCE_WILL_LEAVE: {
|
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const state = getState();
|
|
|
|
const { localTracksDuration } = state['features/analytics'];
|
|
|
|
const newLocalTracksDuration = {
|
|
|
|
...calculateLocalTrackDuration(state),
|
|
|
|
conference: {
|
|
|
|
startedTime: -1,
|
|
|
|
value: Date.now() - localTracksDuration.conference.startedTime
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
sendAnalytics(createLocalTracksDurationEvent(newLocalTracksDuration));
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: UPDATE_LOCAL_TRACKS_DURATION,
|
|
|
|
localTracksDuration: newLocalTracksDuration
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2019-08-22 15:08:34 +00:00
|
|
|
case SET_NETWORK_INFO:
|
|
|
|
sendAnalytics(
|
|
|
|
createNetworkInfoEvent({
|
|
|
|
isOnline: action.isOnline,
|
|
|
|
details: action.details,
|
|
|
|
networkType: action.networkType
|
|
|
|
}));
|
|
|
|
break;
|
2019-05-21 10:32:20 +00:00
|
|
|
case TRACK_ADDED:
|
|
|
|
case TRACK_REMOVED:
|
|
|
|
case TRACK_UPDATED: {
|
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const state = getState();
|
|
|
|
const { localTracksDuration } = state['features/analytics'];
|
2018-05-25 20:30:28 +00:00
|
|
|
|
2022-11-08 19:15:49 +00:00
|
|
|
if (localTracksDuration.conference.startedTime === -1) {
|
2019-05-21 10:32:20 +00:00
|
|
|
// We don't want to track the media duration if the conference is not joined yet because otherwise we won't
|
|
|
|
// be able to compare them with the conference duration (from conference join to conference will leave).
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dispatch({
|
|
|
|
type: UPDATE_LOCAL_TRACKS_DURATION,
|
|
|
|
localTracksDuration: {
|
|
|
|
...localTracksDuration,
|
|
|
|
...calculateLocalTrackDuration(state)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2017-09-01 19:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 10:32:20 +00:00
|
|
|
return result;
|
2017-09-01 19:14:03 +00:00
|
|
|
});
|