-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add analytics events for browser preview #1984
Changes from 4 commits
a3386df
83eb87f
9d212e7
3ce6ec8
924814e
e9a0971
39fc3e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { createBus, createReduxMiddleware } from 'suber' | ||
import configureMockStore, { MockStoreEnhanced } from 'redux-mock-store' | ||
import { | ||
PREVIEW_EVENT, | ||
trackNavigateToPreview, | ||
trackPageLoad | ||
} from './previewDuck' | ||
|
||
describe('previewDuck tests', () => { | ||
let store: MockStoreEnhanced<unknown, unknown> | ||
const bus = createBus() | ||
const mockStore = configureMockStore([createReduxMiddleware(bus)]) | ||
|
||
beforeAll(() => { | ||
store = mockStore() | ||
}) | ||
|
||
afterEach(() => { | ||
bus.reset() | ||
store.clearActions() | ||
localStorage.clear() | ||
}) | ||
|
||
test('trackNavigateToPreview sends a PREVIEW_EVENT', done => { | ||
const action = trackNavigateToPreview() | ||
|
||
bus.take(PREVIEW_EVENT, () => { | ||
// Then | ||
const [action] = store.getActions() | ||
expect(action).toEqual({ | ||
type: PREVIEW_EVENT, | ||
label: 'ui-switch', | ||
data: { | ||
switchedTo: 'preview', | ||
timeSinceLastSwitch: null | ||
} | ||
}) | ||
done() | ||
}) | ||
|
||
// When | ||
store.dispatch(action) | ||
}) | ||
|
||
test('trackNavigateToPreview sets hasTriedPreviewUI', done => { | ||
localStorage.setItem('hasTriedPreviewUI', 'false') | ||
const action = trackNavigateToPreview() | ||
|
||
bus.take(PREVIEW_EVENT, () => { | ||
// Then | ||
const hasTriedPreviewUI = localStorage.getItem('hasTriedPreviewUI') | ||
expect(hasTriedPreviewUI).toBe('true') | ||
done() | ||
}) | ||
|
||
// When | ||
store.dispatch(action) | ||
}) | ||
|
||
test('trackNavigateToPreview sends correct timeSinceLastSwitch when timeSinceLastSwitchMs is unset', done => { | ||
const action = trackNavigateToPreview() | ||
|
||
bus.take(PREVIEW_EVENT, () => { | ||
// Then | ||
const [action] = store.getActions() | ||
expect(action.data.timeSinceLastSwitch).toBeNull() | ||
done() | ||
}) | ||
|
||
// When | ||
store.dispatch(action) | ||
}) | ||
|
||
test('trackNavigateToPreview sends correct timeSinceLastSwitch when timeSinceLastSwitchMs has been set', done => { | ||
localStorage.setItem('timeSinceLastSwitchMs', Date.now().toString()) | ||
const action = trackNavigateToPreview() | ||
|
||
bus.take(PREVIEW_EVENT, () => { | ||
// Then | ||
const [action] = store.getActions() | ||
expect(action.data.timeSinceLastSwitch).not.toBeNull() | ||
done() | ||
}) | ||
|
||
// When | ||
store.dispatch(action) | ||
}) | ||
|
||
test('trackPageLoad sends a PREVIEW_EVENT', done => { | ||
const action = trackPageLoad() | ||
|
||
bus.take(PREVIEW_EVENT, () => { | ||
// Then | ||
const [action] = store.getActions() | ||
expect(action).toEqual({ | ||
type: PREVIEW_EVENT, | ||
label: 'page-load', | ||
data: { previewUI: false, hasTriedPreviewUI: false } | ||
}) | ||
done() | ||
}) | ||
|
||
// When | ||
store.dispatch(action) | ||
}) | ||
|
||
test('trackPageLoad sends correct hasTriedPreviewUI value when flag is unset', done => { | ||
const action = trackPageLoad() | ||
|
||
bus.take(PREVIEW_EVENT, () => { | ||
// Then | ||
const [action] = store.getActions() | ||
expect(action.data.hasTriedPreviewUI).toBeFalsy() | ||
done() | ||
}) | ||
|
||
// When | ||
store.dispatch(action) | ||
}) | ||
|
||
test('trackPageLoad sends correct hasTriedPreviewUI value when flag is set', done => { | ||
localStorage.setItem('hasTriedPreviewUI', 'true') | ||
const action = trackPageLoad() | ||
|
||
bus.take(PREVIEW_EVENT, () => { | ||
// Then | ||
const [action] = store.getActions() | ||
expect(action.data.hasTriedPreviewUI).toBeTruthy() | ||
done() | ||
}) | ||
|
||
// When | ||
store.dispatch(action) | ||
}) | ||
}) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this previewDuck module to create redux actions for our preview switch and page load events, containing logic for setting properties of each action |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
export const PREVIEW_EVENT = 'preview/PREVIEW_EVENT' | ||
|
||
interface PreviewEventAction { | ||
type: typeof PREVIEW_EVENT | ||
label: string | ||
data: | ||
| { | ||
switchedTo: 'preview' | 'classic' | ||
timeSinceLastSwitch: number | null | ||
} | ||
| { | ||
previewUI: boolean | ||
hasTriedPreviewUI: boolean | ||
} | ||
} | ||
|
||
export const trackNavigateToPreview = (): PreviewEventAction => { | ||
const now = Date.now() | ||
localStorage.setItem('hasTriedPreviewUI', 'true') | ||
|
||
const timeSinceLastSwitchMs = localStorage.getItem('timeSinceLastSwitchMs') | ||
localStorage.setItem('timeSinceLastSwitchMs', now.toString()) | ||
|
||
let timeSinceLastSwitch = null | ||
if (timeSinceLastSwitchMs !== null) { | ||
timeSinceLastSwitch = now - parseInt(timeSinceLastSwitchMs) | ||
} | ||
|
||
return { | ||
type: PREVIEW_EVENT, | ||
label: 'ui-switch', | ||
data: { | ||
switchedTo: 'preview', | ||
timeSinceLastSwitch: timeSinceLastSwitch | ||
} | ||
} | ||
} | ||
|
||
export const trackPageLoad = (): PreviewEventAction => { | ||
const hasTriedPreviewUI = localStorage.getItem('hasTriedPreviewUI') === 'true' | ||
|
||
return { | ||
type: PREVIEW_EVENT, | ||
label: 'page-load', | ||
data: { previewUI: false, hasTriedPreviewUI } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ import { | |
TRACK_CANNY_FEATURE_REQUEST | ||
} from 'shared/modules/sidebar/sidebarDuck' | ||
import cmdHelper from 'shared/services/commandInterpreterHelper' | ||
import { PREVIEW_EVENT } from '../preview/previewDuck' | ||
|
||
// Action types | ||
export const NAME = 'udc' | ||
|
@@ -324,3 +325,13 @@ export const trackErrorFramesEpic: Epic<Action, GlobalState> = ( | |
} | ||
}) | ||
.ignoreElements() | ||
|
||
export const trackPreviewEpic: Epic<Action, GlobalState> = action$ => { | ||
return action$.ofType(PREVIEW_EVENT).map((action: any) => { | ||
return metricsEvent({ | ||
category: 'preview', | ||
label: action.label, | ||
data: action.data | ||
}) | ||
}) | ||
} | ||
Comment on lines
+329
to
+337
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding this epic for tracking preview events here so that we can consume them in the existing subscriber in the App and dispatch to segment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can just pass null here instead of a placeholder function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that works, thanks