Skip to content

Commit

Permalink
Fix race condition with custom events
Browse files Browse the repository at this point in the history
  • Loading branch information
max-nav committed Jan 9, 2024
1 parent aa3745f commit 1629ca1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
8 changes: 8 additions & 0 deletions packages/client/src/analytics/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Auth } from '../api';
import { analyticsReadyEvent } from '../events';
import { initAmplitude, logAmplitudeEvent, logPageView } from './amplitude';
import { AnalyticsEventArgs } from './constants';
import {
Expand All @@ -8,6 +10,11 @@ import {
export const initAnalytics = () => {
initAmplitude();
initTaskAnalytics();
window.addEventListener(analyticsReadyEvent, (e) => {
const response = (e as CustomEvent).detail as Auth;
window.logPageView(window.__DECORATOR_DATA__.params, response);
window.startTaskAnalyticsSurvey(window.__DECORATOR_DATA__);
});
};

// Connects to partytown forwarding
Expand Down Expand Up @@ -37,6 +44,7 @@ window.analyticsEvent = function (props: AnalyticsEventArgs) {
'decorator_next',
);
};

window.logPageView = logPageView;
window.logAmplitudeEvent = logAmplitudeEvent;
window.startTaskAnalyticsSurvey = startTaskAnalyticsSurvey;
Expand Down
8 changes: 8 additions & 0 deletions packages/client/src/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Auth } from "./api";

export const analyticsReadyEvent = "decorator-analytics-ready";

export const analyticsReady = (response: Auth) => new CustomEvent(analyticsReadyEvent, {
bubbles: true,
detail: response
})
9 changes: 4 additions & 5 deletions packages/client/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { formatParams } from 'decorator-shared/json';
import { LoginLevel, type Context, type Params } from 'decorator-shared/params';
import Cookies from 'js-cookie';
import 'vite/modulepreload-polyfill';
import { LoginLevel, type Context, type Params } from 'decorator-shared/params'; import Cookies from 'js-cookie'; import 'vite/modulepreload-polyfill';
import * as api from './api';
import { logoutWarningController } from './controllers/logout-warning';
import { onLoadListeners } from './listeners';
Expand All @@ -26,6 +24,7 @@ import './views/search-input';
import './views/search-menu';
import { Auth } from './api';
import { addFaroMetaData } from './faro';
import { analyticsReady } from './events';

import.meta.glob('./styles/*.css', { eager: true });

Expand Down Expand Up @@ -137,14 +136,14 @@ async function populateLoggedInMenu(authObject: Auth) {
const init = async () => {
const response = await api.checkAuth();

window.logPageView(window.__DECORATOR_DATA__.params, response);
window.startTaskAnalyticsSurvey(window.__DECORATOR_DATA__);

if (!response.authenticated) {
return;
}

await populateLoggedInMenu(response);

window.dispatchEvent(analyticsReady(response));
};

init();
Expand Down
7 changes: 1 addition & 6 deletions packages/server/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,10 @@ const getEnvAssets = async () => {
[
'http://localhost:5173/@vite/client',
`http://localhost:5173/${entryPointPath}`,
`http://localhost:5173/${entryPointPathAnalytics}`,
]
.map(script)
.join(''),
[
vendorScripts.taskAnalytics,
hostUrl(`/public/${manifest[entryPointPathAnalytics].file}`),
]
.map(asyncScript)
.join(''),
[inlineVendorScripts.hotjar].map(partytownInlineScript).join(''),
].join(''),
};
Expand Down

0 comments on commit 1629ca1

Please sign in to comment.