Skip to content

Commit

Permalink
Implement action monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dashdashzako committed Oct 10, 2019
1 parent 4be2315 commit 11668e9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<script>
window.aiana = {
"container": '#root',
"loggerEndpoint": "http://qlf-mooc-accessibility.bordeaux.inria.fr/api/log",
"bookmarks": [],
"chapters": {
"language": "fr",
Expand Down
8 changes: 1 addition & 7 deletions src/actions/shared/remote-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DEFAULT_CONFIGURATION_PATH, CONFIGURATION_KEY } from '../../constants';
import { IWidget } from '../../reducers/preferences';
import { initialPreferencesState } from '../../constants/default-preferences-state';
import { CDispatch } from '../../store';
import { ThunkResult } from '../../types';
import { ThunkResult, IQueryString } from '../../types';
import { IPreset } from '../../reducers/presets';
import { BASE_PRESETS } from '../../constants/presets';
import md5 from 'md5';
Expand All @@ -15,12 +15,6 @@ import { createAction } from 'redux-starter-kit';
import i18n from '../../i18n';
import { loadConfiguration } from './configuration';

interface IQueryString {
config?: string;
mid?: string;
src?: string;
}

// TODO: split function
// FIXME: language and defaults handling isn't robust enough.
async function getConfigurationData() {
Expand Down
6 changes: 4 additions & 2 deletions src/components/app/StoreWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import throttle from 'lodash.throttle';
import React from 'react';
import { Provider } from 'react-redux';
import { configureStore } from 'redux-starter-kit';
import { configureStore, getDefaultMiddleware } from 'redux-starter-kit';
import rootReducer, { IAianaState } from '../../reducers';
import ConnectedAiana from './ConnectedAiana';
import i18n from '../../i18n';
Expand All @@ -10,10 +10,12 @@ import {
getLocalPreferencesState
} from '../../reducers/preferences';
import { stateToYAML as playerToYAML } from '../../reducers/player';
import analytics from '../../middleware/analytics';

const store = configureStore({
reducer: rootReducer,
preloadedState: loadState()
preloadedState: loadState(),
middleware: [...getDefaultMiddleware(), analytics]
});

store.subscribe(
Expand Down
42 changes: 42 additions & 0 deletions src/middleware/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Middleware } from 'redux';
import { PayloadAction } from 'redux-starter-kit';
import { loadConfiguration } from '../actions/shared/configuration';
import { setSubtitlesText } from '../actions/subtitles';
import { getUserId } from '../utils/user';
import { IAianaState } from '../reducers';

// TODO: what actions should be monitored?
const loggedActions = [loadConfiguration.type, setSubtitlesText.type];

const analytics: Middleware = (store) => (next) => (
action: PayloadAction<any>
) => {
if (loggedActions.includes(action.type)) {
const state: IAianaState = store.getState();

const playerEvent = {
createdAt: Date.now(),
payload: action.payload,
mediaId: state.player.mediaId,
state,
type: action.type,
userId: getUserId()
};

const strData = JSON.stringify(playerEvent);

if (process.env.NODE_ENV !== 'production') {
console.log(strData);
} else {
const loggerEndpoint = (window as any).loggerEndpoint;

if (loggerEndpoint) {
navigator.sendBeacon(loggerEndpoint, strData);
}
}
}

return next(action);
};

export default analytics;
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ export interface ICueBoundaries {
from: number;
to: number;
}

export interface IQueryString {
config?: string;
mid?: string;
src?: string;
uid?: string;
}
16 changes: 16 additions & 0 deletions src/utils/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import queryString from 'query-string';
import { IQueryString } from '../types';

export function getUserId() {
const parsedQueryString: IQueryString = queryString.parse(
window.location.search
);

const { uid } = parsedQueryString;

if (uid) {
return uid;
}

return null;
}

0 comments on commit 11668e9

Please sign in to comment.