Skip to content

Commit 549f570

Browse files
christianalfoniCompuIves
authored andcommitted
refactored analytics, introduced vero and managing anonymous ids (codesandbox#2997)
* refactored analytics, introduced vero and managing anonymous ids * Rename DNT var * Remove unused loadTour * Handle pageviews * Also sign in state when loading app
1 parent 3993409 commit 549f570

File tree

15 files changed

+508
-368
lines changed

15 files changed

+508
-368
lines changed

packages/app/src/app/overmind/effects/analytics.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import track, {
22
identify,
3+
setAnonymousId,
34
setUserId,
45
} from '@codesandbox/common/lib/utils/analytics';
56

@@ -16,6 +17,7 @@ export default (() => {
1617
track(event, data);
1718
},
1819
identify,
20+
setAnonymousId,
1921
setUserId,
2022
};
2123
})();

packages/app/src/app/overmind/effects/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export { default as browser } from './browser';
55
export { default as connection } from './connection';
66
export { default as fsSync } from './fsSync';
77
export { default as jsZip } from './jsZip';
8-
export { default as jwt } from './jwt';
98
export { default as keybindingManager } from './keybindingManager';
109
export { default as live } from './live';
1110
export { default as moduleRecover } from './moduleRecover';
@@ -25,4 +24,4 @@ export { default as codesandboxApi } from './codesandboxApi';
2524
export { default as themes } from './themes';
2625
export { default as executor } from './executor';
2726
export { default as stripe } from './stripe';
28-
export { default as chameleon } from './chameleon';
27+
export { default as jwt } from './jwt';

packages/app/src/app/overmind/effects/jwt.ts

100755100644
File mode changed.

packages/app/src/app/overmind/factories.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Contributor } from '@codesandbox/common/lib/types';
2-
import { json, IState, IDerive } from 'overmind';
2+
import { IDerive, IState, json } from 'overmind';
3+
34
import { AsyncAction } from '.';
45

56
export const withLoadApp = <T>(
@@ -30,6 +31,8 @@ export const withLoadApp = <T>(
3031
state.user = await effects.api.getCurrentUser();
3132
actions.internal.setPatronPrice();
3233
actions.internal.setSignedInCookie();
34+
effects.analytics.identify('signed_in', true);
35+
effects.analytics.setUserId(state.user.id);
3336
actions.internal.showUserSurveyIfNeeded();
3437
effects.live.connect();
3538
actions.userNotifications.internal.initialize();
@@ -39,9 +42,11 @@ export const withLoadApp = <T>(
3942
'Your session seems to be expired, please log in again...'
4043
);
4144
effects.jwt.reset();
45+
effects.analytics.setAnonymousId();
4246
}
4347
} else {
4448
effects.jwt.reset();
49+
effects.analytics.setAnonymousId();
4550
}
4651

4752
if (continueAction) {

packages/app/src/app/overmind/internalActions.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {
77
ServerStatus,
88
TabType,
99
} from '@codesandbox/common/lib/types';
10-
import { identify, setUserId } from '@codesandbox/common/lib/utils/analytics';
11-
1210
import { NotificationStatus } from '@codesandbox/notifications';
11+
1312
import { createOptimisticModule } from './utils/common';
1413
import getItems from './utils/items';
1514
import { defaultOpenedModule, mainModule } from './utils/main-module';
@@ -28,6 +27,8 @@ export const signIn: AsyncAction<{ useExtraScopes: boolean }> = async (
2827
state.user = await effects.api.getCurrentUser();
2928
actions.internal.setPatronPrice();
3029
actions.internal.setSignedInCookie();
30+
effects.analytics.identify('signed_in', true);
31+
effects.analytics.setUserId(state.user.id);
3132
actions.internal.setStoredSettings();
3233
effects.live.connect();
3334
actions.userNotifications.internal.initialize(); // Seemed a bit differnet originally?
@@ -65,8 +66,6 @@ export const setPatronPrice: Action = ({ state }) => {
6566

6667
export const setSignedInCookie: Action = ({ state }) => {
6768
document.cookie = 'signedIn=true; Path=/;';
68-
identify('signed_in', true);
69-
setUserId(state.user.id);
7069
};
7170

7271
export const showUserSurveyIfNeeded: Action = ({ state, effects, actions }) => {

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { resolveModule } from '@codesandbox/common/lib/sandbox/modules';
2-
import { getHashedUserId } from '@codesandbox/common/lib/utils/analytics';
32
import {
43
EnvironmentVariable,
54
ModuleCorrection,
@@ -122,8 +121,6 @@ export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
122121
}
123122

124123
state.editor.isLoading = false;
125-
126-
effects.chameleon.loadTour(state.user && getHashedUserId(state.user.id));
127124
});
128125

129126
export const contentMounted: Action = ({ state, effects }) => {

packages/app/src/app/pages/index.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DragDropContext } from 'react-dnd';
44
import _debug from '@codesandbox/common/lib/utils/debug';
55
import { Toasts, NotificationStatus } from '@codesandbox/notifications';
66
import { notificationState } from '@codesandbox/common/lib/utils/notifications';
7-
import send, { DNT } from '@codesandbox/common/lib/utils/analytics';
7+
import { DNT, trackPageview } from '@codesandbox/common/lib/utils/analytics';
88
import theme from '@codesandbox/common/lib/theme';
99
import { Button } from '@codesandbox/common/lib/components/Button';
1010
import Loadable from 'app/utils/Loadable';
@@ -79,16 +79,10 @@ const RoutesComponent: React.FC = () => {
7979
render={({ location }) => {
8080
if (process.env.NODE_ENV === 'production') {
8181
routeDebugger(
82-
`Sending '${location.pathname + location.search}' to ga.`
82+
`Sending '${location.pathname + location.search}' to analytics.`
8383
);
84-
if (typeof (window as any).ga === 'function' && !DNT) {
85-
(window as any).ga(
86-
'set',
87-
'page',
88-
location.pathname + location.search
89-
);
90-
91-
send('pageview', { path: location.pathname + location.search });
84+
if (!DNT) {
85+
trackPageview();
9286
}
9387
}
9488
return null;

0 commit comments

Comments
 (0)