Skip to content

Commit

Permalink
fix ga error
Browse files Browse the repository at this point in the history
  • Loading branch information
isstuev committed Jul 24, 2023
1 parent 9faebc1 commit 197acc6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
10 changes: 10 additions & 0 deletions lib/mixpanel/isGoogleAnalyticsLoaded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function wait() {
return new Promise(resolve => setTimeout(resolve, 500));
}

export default function isGoogleAnalyticsLoaded(retries = 3): Promise<boolean> {
if (!retries) {
return Promise.resolve(false);
}
return typeof window.ga?.getAll === 'function' ? Promise.resolve(true) : wait().then(() => isGoogleAnalyticsLoaded(retries - 1));
}
57 changes: 29 additions & 28 deletions lib/mixpanel/useInit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,43 @@ import * as cookies from 'lib/cookies';
import getQueryParamString from 'lib/router/getQueryParamString';

import getGoogleAnalyticsClientId from './getGoogleAnalyticsClientId';
import isGoogleAnalyticsLoaded from './isGoogleAnalyticsLoaded';

export default function useMixpanelInit() {
const [ isInited, setIsInited ] = React.useState(false);
const router = useRouter();
const debugFlagQuery = React.useRef(getQueryParamString(router.query._mixpanel_debug));

React.useEffect(() => {
if (!appConfig.mixpanel.projectToken) {
return;
}

const debugFlagCookie = cookies.get(cookies.NAMES.MIXPANEL_DEBUG);

const config: Partial<Config> = {
debug: Boolean(debugFlagQuery.current || debugFlagCookie),
};
const isAuth = Boolean(cookies.get(cookies.NAMES.API_TOKEN));
const userId = getGoogleAnalyticsClientId();

mixpanel.init(appConfig.mixpanel.projectToken, config);
mixpanel.register({
'Chain id': appConfig.network.id,
Environment: appConfig.isDev ? 'Dev' : 'Prod',
Authorized: isAuth,
'Viewport width': window.innerWidth,
'Viewport height': window.innerHeight,
Language: window.navigator.language,
'User id': userId,
'Device type': _capitalize(deviceType),
isGoogleAnalyticsLoaded().then((isGALoaded) => {
if (!appConfig.mixpanel.projectToken) {
return;
}

const debugFlagCookie = cookies.get(cookies.NAMES.MIXPANEL_DEBUG);

const config: Partial<Config> = {
debug: Boolean(debugFlagQuery.current || debugFlagCookie),
};
const isAuth = Boolean(cookies.get(cookies.NAMES.API_TOKEN));

mixpanel.init(appConfig.mixpanel.projectToken, config);
mixpanel.register({
'Chain id': appConfig.network.id,
Environment: appConfig.isDev ? 'Dev' : 'Prod',
Authorized: isAuth,
'Viewport width': window.innerWidth,
'Viewport height': window.innerHeight,
Language: window.navigator.language,
'User id': isGALoaded ? getGoogleAnalyticsClientId() : undefined,
'Device type': _capitalize(deviceType),
});

setIsInited(true);
if (debugFlagQuery.current && !debugFlagCookie) {
cookies.set(cookies.NAMES.MIXPANEL_DEBUG, 'true');
}
});

setIsInited(true);

if (debugFlagQuery.current && !debugFlagCookie) {
cookies.set(cookies.NAMES.MIXPANEL_DEBUG, 'true');
}
}, []);

return isInited;
Expand Down

0 comments on commit 197acc6

Please sign in to comment.