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

@ -20,6 +20,7 @@
"@docusaurus/preset-classic": "2.0.0-beta.0",
"@mdx-js/react": "^1.6.21",
"@svgr/webpack": "^5.5.0",
"amplitude-js": "^8.3.1",
"clsx": "^1.1.1",
"docusaurus-plugin-sass": "^0.2.0",
"docusaurus2-dotenv": "^1.4.0",

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

View File

@ -125,6 +125,24 @@
"@algolia/logger-common" "4.9.1"
"@algolia/requester-common" "4.9.1"
"@amplitude/types@^1.6.1":
version "1.6.1"
resolved "https://registry.yarnpkg.com/@amplitude/types/-/types-1.6.1.tgz#ed15be42746526d6f7087bedc916734e13be56fd"
integrity sha512-GSaNbROpG5gaoLQPo9crpCkB+BgfN7ZtK6SPJaQC2IX2xNqvfq/RZwYLhMZyNfZw/Nce0Z6lKYXCoNPyhdL26Q==
"@amplitude/ua-parser-js@0.7.24":
version "0.7.24"
resolved "https://registry.yarnpkg.com/@amplitude/ua-parser-js/-/ua-parser-js-0.7.24.tgz#2ce605af7d2c38d4a01313fb2385df55fbbd69aa"
integrity sha512-VbQuJymJ20WEw0HtI2np7EdC3NJGUWi8+Xdbc7uk8WfMIF308T0howpzkQ3JFMN7ejnrcSM/OyNGveeE3TP3TA==
"@amplitude/utils@^1.0.5":
version "1.6.1"
resolved "https://registry.yarnpkg.com/@amplitude/utils/-/utils-1.6.1.tgz#fb6833bca5a35af01468e0317363520ff2377259"
integrity sha512-lzmjvKOkg2tbr5YvLuxBAOoCvnmAJ5mvuLYy9hth5j4dzeVfE8aoZp9PYpxOGAuonVEPJas3BH0XGTVBDn1zNA==
dependencies:
"@amplitude/types" "^1.6.1"
tslib "^1.9.3"
"@babel/code-frame@7.10.4":
version "7.10.4"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.4.tgz#168da1a36e90da68ae8d49c0f1b48c7c6249213a"
@ -2028,6 +2046,16 @@ alphanum-sort@^1.0.2:
resolved "https://registry.yarnpkg.com/alphanum-sort/-/alphanum-sort-1.0.2.tgz#97a1119649b211ad33691d9f9f486a8ec9fbe0a3"
integrity sha1-l6ERlkmyEa0zaR2fn0hqjsn74KM=
amplitude-js@^8.3.1:
version "8.3.1"
resolved "https://registry.yarnpkg.com/amplitude-js/-/amplitude-js-8.3.1.tgz#8ce73aa0e5bf327eae571b93602b25cc82d3a1f4"
integrity sha512-mo1qm3h5vkKkSNJQqvcg/2d06ay348BBi0ma94nx239iMlzlL3XQJ16xgg4a2z62cOsoqn91CRJDmjD39dx9aQ==
dependencies:
"@amplitude/ua-parser-js" "0.7.24"
"@amplitude/utils" "^1.0.5"
blueimp-md5 "^2.10.0"
query-string "5"
ansi-align@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-3.0.0.tgz#b536b371cf687caaef236c18d3e21fe3797467cb"
@ -2340,6 +2368,11 @@ bluebird@^3.7.1:
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==
blueimp-md5@^2.10.0:
version "2.18.0"
resolved "https://registry.yarnpkg.com/blueimp-md5/-/blueimp-md5-2.18.0.tgz#1152be1335f0c6b3911ed9e36db54f3e6ac52935"
integrity sha512-vE52okJvzsVWhcgUHOv+69OG3Mdg151xyn41aVQN/5W5S+S43qZhxECtYLAEHMSFWX6Mv5IZrzj3T5+JqXfj5Q==
body-parser@1.19.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
@ -6721,6 +6754,15 @@ qs@6.7.0:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==
query-string@5:
version "5.1.1"
resolved "https://registry.yarnpkg.com/query-string/-/query-string-5.1.1.tgz#a78c012b71c17e05f2e3fa2319dd330682efb3cb"
integrity sha512-gjWOsm2SoGlgLEdAGt7a6slVOk9mGiXmPFMqrEhLQ68rhQuBnpfs3+EmlvqKyxnCo9/PPlF+9MtY02S1aFg+Jw==
dependencies:
decode-uri-component "^0.2.0"
object-assign "^4.1.0"
strict-uri-encode "^1.0.0"
querystring@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
@ -7792,6 +7834,11 @@ std-env@^2.2.1:
dependencies:
ci-info "^3.0.0"
strict-uri-encode@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"
integrity sha1-J5siXfHVgrH1TmWt3UNS4Y+qBxM=
string-width@^3.0.0, string-width@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-3.1.0.tgz#22767be21b62af1081574306f69ac51b62203961"
@ -8117,7 +8164,7 @@ ts-essentials@^2.0.3:
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-2.0.12.tgz#c9303f3d74f75fa7528c3d49b80e089ab09d8745"
integrity sha512-3IVX4nI6B5cc31/GFFE+i8ey/N2eA0CZDbo6n0yrz0zDX8ZJ8djmU1p+XRz7G3is0F3bB3pu2pAroFdAWQKU3w==
tslib@^1.9.0:
tslib@^1.9.0, tslib@^1.9.3:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==