-
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
Merged
daveajrussell
merged 7 commits into
neo4j:master
from
daveajrussell:preview-analytics-events
Nov 1, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a3386df
add analytics events for preview
daveajrussell 83eb87f
refactor preview events to use redux
daveajrussell 9d212e7
revert changes in app and move to udcduck
daveajrussell 3ce6ec8
refactor previewduck
daveajrussell 924814e
add pageload effecthook to app
daveajrussell e9a0971
move trackpageload call to dbmetaepics
daveajrussell 39fc3e7
remove placeholder fn, replace with null
daveajrussell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 'PREVIEW_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: 'PREVIEW_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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 'PREVIEW_UI_SWITCH', | ||
data: { | ||
switchedTo: 'preview', | ||
timeSinceLastSwitch: timeSinceLastSwitch | ||
} | ||
} | ||
} | ||
|
||
export const trackPageLoad = (): PreviewEventAction => { | ||
const hasTriedPreviewUI = localStorage.getItem('hasTriedPreviewUI') === 'true' | ||
|
||
return { | ||
type: PREVIEW_EVENT, | ||
label: 'PREVIEW_PAGE_LOAD', | ||
data: { previewUI: false, hasTriedPreviewUI } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
interesting that it works even with a timeout of
0
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.
indeed - it's where I start to get a bit iffy with the eventloop/callback queue - but I guess the callback just going to the back of the queue is enough for react's internals to finish and resolve props in the app