From 77e5b2f93841056dd70d95843807ac64bf17b9ab Mon Sep 17 00:00:00 2001 From: jffarge Date: Thu, 24 Jun 2021 16:43:38 +0200 Subject: [PATCH 1/5] feat: :bug: add amplitude analytics Signed-off-by: jffarge --- website/package.json | 1 + website/src/api/github.js | 3 +- website/src/theme/DocPage/index.js | 23 ++++++++++---- website/yarn.lock | 49 +++++++++++++++++++++++++++++- 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/website/package.json b/website/package.json index e8ef3030..305b6576 100644 --- a/website/package.json +++ b/website/package.json @@ -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", diff --git a/website/src/api/github.js b/website/src/api/github.js index 439c975d..ea39bc49 100644 --- a/website/src/api/github.js +++ b/website/src/api/github.js @@ -61,6 +61,7 @@ export async function checkUserCollaboratorStatus(code) { }) return { - userPermission: isUserCollaborator.data + permission: isUserCollaborator.data, + login } } \ No newline at end of file diff --git a/website/src/theme/DocPage/index.js b/website/src/theme/DocPage/index.js index 9be128a0..527fc125 100644 --- a/website/src/theme/DocPage/index.js +++ b/website/src/theme/DocPage/index.js @@ -41,6 +41,7 @@ function DocPageContent({ currentDocRoute, versionMetadata, children }) { setHiddenSidebarContainer(!hiddenSidebarContainer); }, [hiddenSidebar]); + return ( { - 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 - if (userAccessStatus === false) { + if (userAccessStatus?.permission === false) { return } @@ -170,6 +171,16 @@ function DocPage(props) { ) } + + // 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 diff --git a/website/yarn.lock b/website/yarn.lock index 74f26e22..e4653276 100644 --- a/website/yarn.lock +++ b/website/yarn.lock @@ -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== From af7df8c421865aec52f10b2b7672cd7912016d0b Mon Sep 17 00:00:00 2001 From: jffarge Date: Thu, 24 Jun 2021 16:56:17 +0200 Subject: [PATCH 2/5] fix: :bug: change netlify context to deploy with auth Signed-off-by: jffarge --- website/netlify.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/website/netlify.toml b/website/netlify.toml index 703a4e4a..decad0cf 100644 --- a/website/netlify.toml +++ b/website/netlify.toml @@ -17,6 +17,12 @@ ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../docs/" command = "yarn build:withoutAuth" +[context.docs-analytics] + # Do not build the site if there's no site-related changes since the last + # deploy. + ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../docs/" + command = "yarn build" + [[redirects]] from = "/github-proxy/*" to = "https://github.com/:splat" From 0e2372be94ab7ee9931447744d86527fa3dcdf1e Mon Sep 17 00:00:00 2001 From: jffarge Date: Thu, 24 Jun 2021 17:05:00 +0200 Subject: [PATCH 3/5] docs: :bug: add proxy redirect Signed-off-by: jffarge --- website/netlify.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/website/netlify.toml b/website/netlify.toml index decad0cf..1345f8a3 100644 --- a/website/netlify.toml +++ b/website/netlify.toml @@ -38,6 +38,11 @@ from = "/docs-access/*" to = "https://j20f3pfq11.execute-api.us-east-1.amazonaws.com/Prod/u/:splat" status = 200 +[[redirects]] +from = "/t" +to = "https://api.amplitude.com" +status = 200 + [[redirects]] from = "/reference" to = "/reference/universe/alpine" From e838fdb6a3360eda8f486f791afab0d789454113 Mon Sep 17 00:00:00 2001 From: jffarge Date: Thu, 24 Jun 2021 17:17:38 +0200 Subject: [PATCH 4/5] docs: :bug: fix amplitude logEvent Signed-off-by: jffarge --- website/src/theme/DocPage/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/src/theme/DocPage/index.js b/website/src/theme/DocPage/index.js index 527fc125..83cdeb05 100644 --- a/website/src/theme/DocPage/index.js +++ b/website/src/theme/DocPage/index.js @@ -175,10 +175,10 @@ function DocPage(props) { // 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, { + var amplitudeInstance = amplitude.getInstance().init(process.env.REACT_APP_AMPLITUDE_ID, userAccessStatus?.login.toLowerCase(), { apiEndpoint: `${window.location.hostname}/t` }); - amplitude.getInstance().logEvent('page view', window.location.pathname); + amplitude.getInstance().logEvent('Docs View', { "path": window.location.pathname }); } }) } From 598df7350b8715180a749948a80eb980224eaa59 Mon Sep 17 00:00:00 2001 From: jffarge Date: Thu, 24 Jun 2021 17:39:55 +0200 Subject: [PATCH 5/5] docs: :bug: remove branch context + add amplitude option Signed-off-by: jffarge --- website/netlify.toml | 6 ------ website/src/theme/DocPage/index.js | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/website/netlify.toml b/website/netlify.toml index 1345f8a3..6fccec8d 100644 --- a/website/netlify.toml +++ b/website/netlify.toml @@ -17,12 +17,6 @@ ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../docs/" command = "yarn build:withoutAuth" -[context.docs-analytics] - # Do not build the site if there's no site-related changes since the last - # deploy. - ignore = "git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF . ../docs/" - command = "yarn build" - [[redirects]] from = "/github-proxy/*" to = "https://github.com/:splat" diff --git a/website/src/theme/DocPage/index.js b/website/src/theme/DocPage/index.js index 83cdeb05..eae8cfc2 100644 --- a/website/src/theme/DocPage/index.js +++ b/website/src/theme/DocPage/index.js @@ -178,7 +178,7 @@ function DocPage(props) { var amplitudeInstance = amplitude.getInstance().init(process.env.REACT_APP_AMPLITUDE_ID, userAccessStatus?.login.toLowerCase(), { apiEndpoint: `${window.location.hostname}/t` }); - amplitude.getInstance().logEvent('Docs View', { "path": window.location.pathname }); + amplitude.getInstance().logEvent('Docs View', { "hostname": window.location.hostname, "path": window.location.pathname }); } }) }