-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(website): tweaks pageview (#212)
- Loading branch information
1 parent
c6ba12e
commit 22efeb8
Showing
3 changed files
with
55 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,53 @@ | ||
const DEFAULT_PAGEVIEW_RESPONSE = { viewer: 0, visited: 0 }; | ||
|
||
export interface PageViewData { | ||
viewer: number; | ||
visited: number; | ||
} | ||
|
||
export const updatePageview = async ( | ||
pathname: string | ||
): Promise<PageViewData> => { | ||
const hasVisited = window.localStorage.getItem("visited"); | ||
const hasVisitedCurrentPath = window.localStorage.getItem( | ||
`visited-${pathname}` | ||
); | ||
|
||
if (!hasVisited) { | ||
window.localStorage.setItem("visited", "true"); | ||
} | ||
|
||
if (!hasVisitedCurrentPath) { | ||
window.localStorage.setItem(`visited-${pathname}`, "true"); | ||
} | ||
|
||
try { | ||
const { statusCode, data } = await fetch( | ||
`/api/public/viewer?isNew=${!hasVisited}&isNewByPath=${!hasVisitedCurrentPath}`, | ||
{ method: "POST" } | ||
).then((res) => res.json()); | ||
|
||
return statusCode === 233 ? DEFAULT_PAGEVIEW_RESPONSE : data; | ||
} catch (err) { | ||
console.log(err); | ||
throw err; | ||
} | ||
}; | ||
|
||
export const addViewerWithApiRoute = async () => { | ||
let isNew = true; | ||
if (window.localStorage.getItem("visited")) { | ||
isNew = false; | ||
} else { | ||
window.localStorage.setItem("visited", "true"); | ||
} | ||
try { | ||
const url = `/api/viewer?isNew=${isNew}`; | ||
const res = await fetch(url); | ||
const data = await res.json(); | ||
return data; | ||
} catch (err) { | ||
console.log(err); | ||
throw err; | ||
} | ||
}; |
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