-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: provide worspaceId to AnalyticsInitializer (#7642)
* fix: provide worspaceId to AnalyticsInitializer * Refactor Analytics for more independent approach * Speed up initial load for cloud webapp Co-authored-by: Artem Astapenko <jamakase54@gmail.com>
- Loading branch information
Showing
39 changed files
with
390 additions
and
302 deletions.
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
19 changes: 19 additions & 0 deletions
19
airbyte-webapp/src/hooks/services/Analytics/TrackPageAnalytics.tsx
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,19 @@ | ||
import React, { useEffect } from "react"; | ||
|
||
import useRouter from "hooks/useRouter"; | ||
|
||
import { useAnalyticsService } from "./useAnalyticsService"; | ||
import { getPageName } from "./pageNameUtils"; | ||
|
||
export const TrackPageAnalytics: React.FC = () => { | ||
const { pathname } = useRouter(); | ||
const analyticsService = useAnalyticsService(); | ||
useEffect(() => { | ||
const pageName = getPageName(pathname); | ||
if (pageName) { | ||
analyticsService.page(pageName); | ||
} | ||
}, [analyticsService, pathname]); | ||
|
||
return null; | ||
}; |
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,2 @@ | ||
export * from "./TrackPageAnalytics"; | ||
export * from "./useAnalyticsService"; |
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
101 changes: 101 additions & 0 deletions
101
airbyte-webapp/src/hooks/services/Analytics/useAnalyticsService.tsx
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,101 @@ | ||
import React, { useContext, useEffect, useMemo } from "react"; | ||
import { useMap } from "react-use"; | ||
|
||
import { AnalyticsService } from "core/analytics/AnalyticsService"; | ||
|
||
type AnalyticsContext = Record<string, unknown>; | ||
|
||
export type AnalyticsServiceProviderValue = { | ||
analyticsContext: AnalyticsContext; | ||
setContext: (ctx: AnalyticsContext) => void; | ||
addContextProps: (props: AnalyticsContext) => void; | ||
removeContextProps: (props: string[]) => void; | ||
service: AnalyticsService; | ||
}; | ||
|
||
const analyticsServiceContext = React.createContext<AnalyticsServiceProviderValue | null>( | ||
null | ||
); | ||
|
||
function AnalyticsServiceProvider({ | ||
children, | ||
version, | ||
initialContext = {}, | ||
}: { | ||
children: React.ReactNode; | ||
version?: string; | ||
initialContext?: AnalyticsContext; | ||
}) { | ||
const [analyticsContext, { set, setAll, remove }] = useMap(initialContext); | ||
|
||
const analyticsService: AnalyticsService = useMemo( | ||
() => new AnalyticsService(analyticsContext, version), | ||
[version, analyticsContext] | ||
); | ||
|
||
const handleAddContextProps = (props: AnalyticsContext) => { | ||
Object.entries(props).forEach((value) => set(...value)); | ||
}; | ||
|
||
const handleRemoveContextProps = (props: string[]) => props.forEach(remove); | ||
|
||
return ( | ||
<analyticsServiceContext.Provider | ||
value={{ | ||
analyticsContext, | ||
setContext: setAll, | ||
addContextProps: handleAddContextProps, | ||
removeContextProps: handleRemoveContextProps, | ||
service: analyticsService, | ||
}} | ||
> | ||
{children} | ||
</analyticsServiceContext.Provider> | ||
); | ||
} | ||
|
||
export const useAnalyticsService = (): AnalyticsService => { | ||
const analyticsService = useAnalytics(); | ||
|
||
return analyticsService.service; | ||
}; | ||
|
||
export const useAnalytics = (): AnalyticsServiceProviderValue => { | ||
const analyticsContext = useContext(analyticsServiceContext); | ||
|
||
if (!analyticsContext) { | ||
throw new Error( | ||
"analyticsContext must be used within a AnalyticsServiceProvider." | ||
); | ||
} | ||
|
||
return analyticsContext; | ||
}; | ||
|
||
export const useAnalyticsIdentifyUser = (userId?: string): void => { | ||
const analyticsService = useAnalyticsService(); | ||
|
||
useEffect(() => { | ||
if (userId) { | ||
analyticsService.identify(userId); | ||
} | ||
}, [userId]); | ||
}; | ||
|
||
export const useAnalyticsRegisterValues = ( | ||
props?: AnalyticsContext | null | ||
): void => { | ||
const { addContextProps, removeContextProps } = useAnalytics(); | ||
|
||
useEffect(() => { | ||
if (props) { | ||
addContextProps(props); | ||
|
||
return () => removeContextProps(Object.keys(props)); | ||
} | ||
|
||
return; | ||
}, [props]); | ||
}; | ||
|
||
export default React.memo(AnalyticsServiceProvider); |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import NotificationService from "./NotificationService"; | ||
import NotificationService, { | ||
useNotificationService, | ||
} from "./NotificationService"; | ||
|
||
export default NotificationService; | ||
export { NotificationService }; | ||
export { NotificationService, useNotificationService }; |
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
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.