re-add nav analytics (#6634)

* track page views

* improve navigate logic

* move scratchpad init to main

---------

Co-authored-by: gatzjames <jamesgatzos@gmail.com>
This commit is contained in:
Jack Kavanagh 2023-10-03 16:57:38 +02:00 committed by GitHub
parent 93226cecf7
commit 9ff6e9f66f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 34 deletions

View File

@ -6,7 +6,7 @@ import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-insta
import path from 'path';
import { userDataFolder } from '../config/config.json';
import { changelogUrl, getAppVersion, isDevelopment, isMac } from './common/constants';
import { changelogUrl, getAppVersion, getProductName, isDevelopment, isMac } from './common/constants';
import { database } from './common/database';
import log, { initializeLogging } from './common/log';
import { registerInsomniaStreamProtocol } from './main/api.protocol';
@ -219,6 +219,21 @@ const _launchApp = async () => {
async function _createModelInstances() {
await models.stats.get();
await models.settings.getOrCreate();
try {
const scratchpadProject = await models.project.getById(models.project.SCRATCHPAD_PROJECT_ID);
const scratchPad = await models.workspace.getById(models.workspace.SCRATCHPAD_WORKSPACE_ID);
if (!scratchpadProject) {
console.log('Initializing Scratch Pad Project');
await models.project.create({ _id: models.project.SCRATCHPAD_PROJECT_ID, name: getProductName(), remoteId: null, parentId: models.organization.SCRATCHPAD_ORGANIZATION_ID });
}
if (!scratchPad) {
console.log('Initializing Scratch Pad');
await models.workspace.create({ _id: models.workspace.SCRATCHPAD_WORKSPACE_ID, name: 'Scratch Pad', parentId: models.project.SCRATCHPAD_PROJECT_ID, scope: 'collection' });
}
} catch (err) {
console.warn('Failed to create default project. It probably already exists', err);
}
}
async function _trackStats() {

View File

@ -99,9 +99,9 @@ function getInitialEntry() {
return '/auth/login';
}
return '/scratchpad';
return '/organization/org_scratchpad/project/proj_scratchpad/workspace/wrk_scratchpad/debug';
} catch (e) {
return '/scratchpad';
return '/organization/org_scratchpad/project/proj_scratchpad/workspace/wrk_scratchpad/debug';
}
}
@ -117,10 +117,6 @@ const router = createMemoryRouter(
loader: async (...args) => (await import('./routes/root')).loader(...args),
errorElement: <ErrorRoute />,
children: [
{
path: '/scratchpad',
loader: async (...args) => (await import('./routes/scratchpad')).loader(...args),
},
{
path: 'onboarding/*',
element: <Onboarding />,
@ -903,7 +899,7 @@ const router = createMemoryRouter(
);
// Store the last location in local storage
router.subscribe(({ location }) => {
router.subscribe(({ location, navigation }) => {
const match = matchPath(
{
path: '/organization/:organizationId',
@ -911,8 +907,18 @@ router.subscribe(({ location }) => {
},
location.pathname
);
const nextRoute = navigation.location?.pathname;
const currentRoute = location.pathname;
// Use navigation send tracking events on page change
const bothHaveValueButNotEqual = nextRoute && currentRoute && nextRoute !== currentRoute;
if (bothHaveValueButNotEqual) {
// transforms /organization/:org_* to /organization/:org_id
const routeWithoutUUID = nextRoute.replace(/_[a-f0-9]{32}/g, '_id');
// console.log('Tracking page view', { name: routeWithoutUUID });
window.main.trackPageView({ name: routeWithoutUUID });
}
match?.params.organizationId && localStorage.setItem(`locationHistoryEntry:${match?.params.organizationId}`, location.pathname);
match?.params.organizationId && localStorage.setItem(`locationHistoryEntry:${match?.params.organizationId}`, currentRoute);
});
async function renderApp() {

View File

@ -171,7 +171,7 @@ const Login = () => {
<div className='flex gap-[--padding-md] justify-between'>
<Link
aria-label='Use the Scratch Pad'
to={'/scratchpad'}
to={'/organization/org_scratchpad/project/proj_scratchpad/workspace/wrk_scratchpad/debug'}
className='flex outline-none transition-colors justify-center text-[rgba(var(--color-font-rgb),0.8)] text-sm gap-[--padding-xs] hover:text-[--color-font] focus:text-[--color-font]'
>
<div>
@ -214,7 +214,7 @@ const Login = () => {
You can use Insomnia without an account and without connecting to the cloud by using the local Scratch Pad.
</p>
<Link
to="/scratchpad"
to="/organization/org_scratchpad/project/proj_scratchpad/workspace/wrk_scratchpad/debug"
aria-label='Go to Scratch Pad'
className="px-4 py-1 outline-none font-semibold border border-solid border-[--hl-md] flex items-center justify-center gap-2 aria-pressed:bg-[--hl-sm] rounded-sm text-[--color-font] hover:bg-[--hl-xs] focus:ring-inset ring-1 ring-transparent focus:ring-[--hl-md] transition-all text-base"
>

View File

@ -179,6 +179,7 @@ export const indexLoader: LoaderFunction = async ({ params }) => {
const { organizationId } = params;
invariant(organizationId, 'Organization ID is required');
// When org icon is clicked this ensures we remember the last visited page
const prevOrganizationLocation = localStorage.getItem(
`locationHistoryEntry:${organizationId}`
);

View File

@ -1,23 +0,0 @@
import { LoaderFunction, redirect } from 'react-router-dom';
import { getProductName } from '../../common/constants';
import * as models from '../../models';
export const loader: LoaderFunction = async () => {
try {
const scratchpadProject = await models.project.getById(models.project.SCRATCHPAD_PROJECT_ID);
const scratchPad = await models.workspace.getById(models.workspace.SCRATCHPAD_WORKSPACE_ID);
if (!scratchpadProject) {
console.log('Initializing Scratch Pad Project');
await models.project.create({ _id: models.project.SCRATCHPAD_PROJECT_ID, name: getProductName(), remoteId: null, parentId: models.organization.SCRATCHPAD_ORGANIZATION_ID });
}
if (!scratchPad) {
console.log('Initializing Scratch Pad');
await models.workspace.create({ _id: models.workspace.SCRATCHPAD_WORKSPACE_ID, name: 'Scratch Pad', parentId: models.project.SCRATCHPAD_PROJECT_ID, scope: 'collection' });
}
} catch (err) {
console.warn('Failed to create default project. It probably already exists', err);
}
return redirect(`/organization/${models.organization.SCRATCHPAD_ORGANIZATION_ID}/project/${models.project.SCRATCHPAD_PROJECT_ID}/workspace/${models.workspace.SCRATCHPAD_WORKSPACE_ID}/debug`);
};