-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #819 from blockscout/feat/mixpanel
Feature: mixpanel analytics
- Loading branch information
Showing
27 changed files
with
311 additions
and
34 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type CspDev from 'csp-dev'; | ||
|
||
import appConfig from 'configs/app/config'; | ||
|
||
export function mixpanel(): CspDev.DirectiveDescriptor { | ||
if (!appConfig.mixpanel.projectToken) { | ||
return {}; | ||
} | ||
|
||
return { | ||
'connect-src': [ | ||
'*.mixpanel.com', | ||
], | ||
}; | ||
} |
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,3 @@ | ||
export default function getGoogleAnalyticsClientId() { | ||
return window.ga?.getAll()[0].get('clientId'); | ||
} |
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,47 @@ | ||
import type { Route } from 'nextjs-routes'; | ||
|
||
const PAGE_TYPE_DICT: Record<Route['pathname'], string> = { | ||
'/': 'Homepage', | ||
'/txs': 'Transactions', | ||
'/tx/[hash]': 'Transaction details', | ||
'/blocks': 'Blocks', | ||
'/block/[height]': 'Block details', | ||
'/accounts': 'Top accounts', | ||
'/address/[hash]': 'Address details', | ||
'/verified-contracts': 'Verified contracts', | ||
'/address/[hash]/contract_verification': 'Contract verification', | ||
'/tokens': 'Tokens', | ||
'/token/[hash]': 'Token details', | ||
'/token/[hash]/instance/[id]': 'Token Instance', | ||
'/apps': 'Apps', | ||
'/apps/[id]': 'App', | ||
'/stats': 'Stats', | ||
'/api-docs': 'REST API', | ||
'/graphiql': 'GraphQL', | ||
'/search-results': 'Search results', | ||
'/auth/profile': 'Profile', | ||
'/account/watchlist': 'Watchlist', | ||
'/account/api_key': 'API keys', | ||
'/account/custom_abi': 'Custom ABI', | ||
'/account/public_tags_request': 'Public tags', | ||
'/account/tag_address': 'Private tags', | ||
'/account/verified_addresses': 'Verified addresses', | ||
'/withdrawals': 'Withdrawals', | ||
'/visualize/sol2uml': 'Solidity UML diagram', | ||
'/csv-export': 'Export data to CSV file', | ||
'/l2-deposits': 'Deposits (L1 > L2)', | ||
'/l2-output-roots': 'Output roots', | ||
'/l2-txn-batches': 'Tx batches (L2 blocks)', | ||
'/l2-withdrawals': 'Withdrawals (L2 > L1)', | ||
|
||
// service routes, added only to make typescript happy | ||
'/login': 'Login', | ||
'/api/media-type': 'Node API: Media type', | ||
'/api/proxy': 'Node API: Proxy', | ||
'/api/csrf': 'Node API: CSRF token', | ||
'/auth/auth0': 'Auth', | ||
}; | ||
|
||
export default function getPageType(pathname: Route['pathname']) { | ||
return PAGE_TYPE_DICT[pathname] || 'Unknown page'; | ||
} |
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,5 @@ | ||
import _capitalize from 'lodash/capitalize'; | ||
|
||
export default function getTabName(tab: string) { | ||
return tab !== '' ? _capitalize(tab.replaceAll('_', ' ')) : 'Default'; | ||
} |
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,12 @@ | ||
import getPageType from './getPageType'; | ||
import logEvent from './logEvent'; | ||
import useInit from './useInit'; | ||
import useLogPageView from './useLogPageView'; | ||
export * from './utils'; | ||
|
||
export { | ||
useInit, | ||
useLogPageView, | ||
logEvent, | ||
getPageType, | ||
}; |
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,14 @@ | ||
import mixpanel from 'mixpanel-browser'; | ||
|
||
import type { EventTypes, EventPayload } from './utils'; | ||
|
||
type TrackFnArgs = Parameters<typeof mixpanel.track>; | ||
|
||
export default function logEvent<EventType extends EventTypes>( | ||
type: EventType, | ||
properties?: EventPayload<EventType>, | ||
optionsOrCallback?: TrackFnArgs[2], | ||
callback?: TrackFnArgs[3], | ||
) { | ||
mixpanel.track(type, properties, optionsOrCallback, callback); | ||
} |
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,52 @@ | ||
import _capitalize from 'lodash/capitalize'; | ||
import type { Config } from 'mixpanel-browser'; | ||
import mixpanel from 'mixpanel-browser'; | ||
import { useRouter } from 'next/router'; | ||
import React from 'react'; | ||
import { deviceType } from 'react-device-detect'; | ||
|
||
import appConfig from 'configs/app/config'; | ||
import * as cookies from 'lib/cookies'; | ||
import getQueryParamString from 'lib/router/getQueryParamString'; | ||
|
||
import getGoogleAnalyticsClientId from './getGoogleAnalyticsClientId'; | ||
|
||
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), | ||
}); | ||
|
||
setIsInited(true); | ||
|
||
if (debugFlagQuery.current && !debugFlagCookie) { | ||
cookies.set(cookies.NAMES.MIXPANEL_DEBUG, 'true'); | ||
} | ||
}, []); | ||
|
||
return isInited; | ||
} |
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,36 @@ | ||
import { usePathname } from 'next/navigation'; | ||
import { useRouter } from 'next/router'; | ||
import React from 'react'; | ||
|
||
import appConfig from 'configs/app/config'; | ||
import getQueryParamString from 'lib/router/getQueryParamString'; | ||
|
||
import getPageType from './getPageType'; | ||
import getTabName from './getTabName'; | ||
import logEvent from './logEvent'; | ||
import { EventTypes } from './utils'; | ||
|
||
export default function useLogPageView(isInited: boolean) { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
|
||
const tab = getQueryParamString(router.query.tab); | ||
const page = getQueryParamString(router.query.page); | ||
|
||
React.useEffect(() => { | ||
if (!appConfig.mixpanel.projectToken || !isInited) { | ||
return; | ||
} | ||
|
||
logEvent(EventTypes.PAGE_VIEW, { | ||
'Page type': getPageType(router.pathname), | ||
Tab: getTabName(tab), | ||
Page: page || undefined, | ||
}); | ||
// these are only deps that should trigger the effect | ||
// in some scenarios page type is not changing (e.g navigation from one address page to another), | ||
// but we still want to log page view | ||
// so we use pathname from 'next/navigation' instead of router.pathname from 'next/router' as deps | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ isInited, page, pathname, tab ]); | ||
} |
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,18 @@ | ||
export enum EventTypes { | ||
PAGE_VIEW = 'Page view', | ||
SEARCH_QUERY = 'Search query', | ||
} | ||
|
||
export type EventPayload<Type extends EventTypes> = | ||
Type extends EventTypes.PAGE_VIEW ? | ||
{ | ||
'Page type': string; | ||
'Tab': string; | ||
'Page'?: string; | ||
} : | ||
Type extends EventTypes.SEARCH_QUERY ? { | ||
'Search query': string; | ||
'Source page type': string; | ||
'Result URL': string; | ||
} : | ||
undefined; |
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.