Skip to content

Commit

Permalink
re-add nav analytics (Kong#6634)
Browse files Browse the repository at this point in the history
* track page views

* improve navigate logic

* move scratchpad init to main

---------

Co-authored-by: gatzjames <jamesgatzos@gmail.com>
  • Loading branch information
jackkav and gatzjames committed Nov 24, 2023
1 parent 66be258 commit 1c62bea
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
17 changes: 16 additions & 1 deletion packages/insomnia/src/main.development.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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() {
Expand Down
22 changes: 14 additions & 8 deletions packages/insomnia/src/ui/index.tsx
Expand Up @@ -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';
}
}

Expand All @@ -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 />,
Expand Down Expand Up @@ -903,16 +899,26 @@ const router = createMemoryRouter(
);

// Store the last location in local storage
router.subscribe(({ location }) => {
router.subscribe(({ location, navigation }) => {
const match = matchPath(
{
path: '/organization/:organizationId',
end: false,
},
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() {
Expand Down
4 changes: 2 additions & 2 deletions packages/insomnia/src/ui/routes/auth.login.tsx
Expand Up @@ -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>
Expand Down Expand Up @@ -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"
>
Expand Down
1 change: 1 addition & 0 deletions packages/insomnia/src/ui/routes/project.tsx
Expand Up @@ -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}`
);
Expand Down
23 changes: 0 additions & 23 deletions packages/insomnia/src/ui/routes/scratchpad.tsx

This file was deleted.

0 comments on commit 1c62bea

Please sign in to comment.