-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathanalytics.tsx
More file actions
60 lines (56 loc) · 1.87 KB
/
analytics.tsx
File metadata and controls
60 lines (56 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import ReactGA from "react-ga";
import {
ADD_SCHOOL_ATTR,
REM_SCHOOL_ATTR,
UPDATE_SEARCH_TEXT,
UPDATE_RANGE_FILTER,
CHANGE_MY_SCHEDULE,
RENAME_SCHEDULE,
CREATE_SCHEDULE_ON_FRONTEND,
DELETE_SCHEDULE_ON_FRONTEND,
SET_PRIMARY_SCHEDULE_ID_ON_FRONTEND,
} from "../actions";
import { SWITCH_ACTIVE_FRIEND } from "../actions/friendshipUtil";
export const initGA = () => {
ReactGA.initialize("UA-21029575-15");
};
export const logPageView = () => {
ReactGA.set({ page: window.location.pathname });
ReactGA.pageview(window.location.pathname);
};
export const logEvent = (category = "", action = "", label = "") => {
if (category && action) {
ReactGA.event({ category, action, label });
}
};
export const logException = (description = "", fatal = false) => {
if (description) {
ReactGA.exception({ description, fatal });
}
};
const filterActions = [ADD_SCHOOL_ATTR, REM_SCHOOL_ATTR, UPDATE_RANGE_FILTER];
const schedActions = [
CHANGE_MY_SCHEDULE,
RENAME_SCHEDULE,
CREATE_SCHEDULE_ON_FRONTEND,
DELETE_SCHEDULE_ON_FRONTEND,
];
const schedShareActions = [
SET_PRIMARY_SCHEDULE_ID_ON_FRONTEND, // Changing schedule to share
SWITCH_ACTIVE_FRIEND // Viewing friend's schedule
]
// TODO: confirm type of store, next, and action
export const analyticsMiddleware = (store: any) => (next: (_: any) => void) => (
action: any
) => {
if (filterActions.includes(action.type)) {
logEvent("filter", action.type, JSON.stringify(action));
} else if (schedActions.includes(action.type)) {
logEvent("schedule", action.type, JSON.stringify(action));
} else if (schedShareActions.includes(action.type)) {
logEvent("schedule share", action.type, JSON.stringify(action));
} else if (action.type === UPDATE_SEARCH_TEXT) {
logEvent("search", action.s);
}
return next(action);
};