feat: 🐛 add amplitude analytics

Signed-off-by: jffarge <jf@dagger.io>
This commit is contained in:
jffarge
2021-06-24 16:43:38 +02:00
parent aa6335246b
commit 77e5b2f938
4 changed files with 68 additions and 8 deletions

View File

@@ -61,6 +61,7 @@ export async function checkUserCollaboratorStatus(code) {
})
return {
userPermission: isUserCollaborator.data
permission: isUserCollaborator.data,
login
}
}

View File

@@ -41,6 +41,7 @@ function DocPageContent({ currentDocRoute, versionMetadata, children }) {
setHiddenSidebarContainer(!hiddenSidebarContainer);
}, [hiddenSidebar]);
return (
<Layout
key={isClient}
@@ -144,15 +145,15 @@ function DocPage(props) {
const [redirectState, setRedirectState] = useState()
const authQuery = qs.parse(location.search);
const [userAccessStatus, setUserAccessStatus] = useState((() => {
if (typeof window !== "undefined") return window.localStorage.getItem('user-github-isAllowed')
if (typeof window !== "undefined") return JSON.parse(window.localStorage.getItem('user'))
})())
useEffect(async () => {
if (!isEmpty(authQuery) && userAccessStatus === null) { //callback after successful auth with github
const isUserCollaborator = await checkUserCollaboratorStatus(authQuery.code);
setUserAccessStatus(isUserCollaborator?.userPermission)
if (isUserCollaborator?.userPermission) {
if (typeof window !== "undefined") window.localStorage.setItem('user-github-isAllowed', isUserCollaborator?.userPermission);
const user = await checkUserCollaboratorStatus(authQuery.code);
if (user?.permission) {
setUserAccessStatus(user?.permission)
if (typeof window !== "undefined") window.localStorage.setItem('user', JSON.stringify(user));
}
}
setIsLoading(false)
@@ -161,7 +162,7 @@ function DocPage(props) {
if (isLoading) return <Spinner />
if (userAccessStatus === false) {
if (userAccessStatus?.permission === false) {
return <DocPageRedirect />
}
@@ -170,6 +171,16 @@ function DocPage(props) {
<DocPageAuthentication />
)
}
// TODO: DISABLE FOR LOCALHOST ENV
import('amplitude-js').then(amplitude => {
if (userAccessStatus?.login) {
var amplitudeInstance = amplitude.getInstance().init(process.env.REACT_APP_AMPLITUDE_ID, userAccessStatus?.login, {
apiEndpoint: `${window.location.hostname}/t`
});
amplitude.getInstance().logEvent('page view', window.location.pathname);
}
})
}
// END CUSTOM DOCPAGE