-
Notifications
You must be signed in to change notification settings - Fork 1
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 #206 from ecency/feature/page-views
Added statistics for posts
- Loading branch information
Showing
22 changed files
with
391 additions
and
185 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { EcencyQueriesManager, QueryIdentifiers } from "@/core/react-query"; | ||
import { appAxios } from "@/api/axios"; | ||
|
||
export interface StatsResponse { | ||
results: [ | ||
{ | ||
metrics: number[]; | ||
dimensions: string[]; | ||
} | ||
]; | ||
query: { | ||
site_id: string; | ||
metrics: string[]; | ||
date_range: string[]; | ||
filters: unknown[]; | ||
}; | ||
} | ||
|
||
export function useGetStatsQuery( | ||
url: string, | ||
dimensions: string[] = [], | ||
metrics = ["visitors", "pageviews", "visit_duration"] | ||
) { | ||
return EcencyQueriesManager.generateClientServerQuery({ | ||
queryKey: [QueryIdentifiers.PAGE_STATS, url, dimensions, metrics], | ||
queryFn: async () => { | ||
const response = await appAxios.post<StatsResponse>(`/api/stats`, { | ||
metrics, | ||
url: encodeURIComponent(url), | ||
dimensions | ||
}); | ||
return response.data; | ||
}, | ||
enabled: !!url | ||
}); | ||
} |
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 @@ | ||
export * from "./get-page-stats-query"; |
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
57 changes: 57 additions & 0 deletions
57
...Pages)/entry/[category]/[author]/[permlink]/_components/entry-page-stats-by-countries.tsx
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,57 @@ | ||
import i18next from "i18next"; | ||
import { useMemo } from "react"; | ||
import { useGetStatsQuery } from "@/api/queries"; | ||
import { AnimatePresence, motion } from "framer-motion"; | ||
|
||
interface Props { | ||
totalViews: number; | ||
cleanedPathname: string; | ||
} | ||
|
||
export function EntryPageStatsByCountries({ totalViews, cleanedPathname }: Props) { | ||
const { data: stats } = useGetStatsQuery(cleanedPathname, [ | ||
"visit:country_name" | ||
]).useClientQuery(); | ||
|
||
const countries = useMemo( | ||
() => | ||
stats?.results?.reduce<Record<string, number>>((acc, result) => { | ||
const country = result.dimensions[0]; | ||
const views = +result.metrics[1]; | ||
return { ...acc, [country]: (acc[country] ?? 0) + views }; | ||
}, {}) ?? {}, | ||
[stats?.results] | ||
); | ||
const countriesList = useMemo( | ||
() => Object.entries(countries).sort((a, b) => b[1] - a[1]), | ||
[countries] | ||
); | ||
|
||
return countriesList.length > 0 ? ( | ||
<div className="flex flex-col w-full gap-2 text-sm"> | ||
<div className="text-sm opacity-50 pb-2">{i18next.t("entry.stats.countries")}</div> | ||
<AnimatePresence mode="popLayout"> | ||
{countriesList.map(([country, views]) => ( | ||
<motion.div | ||
initial={{ opacity: 0 }} | ||
animate={{ opacity: 1 }} | ||
className="rounded-md overflow-hidden relative w-full flex items-center justify-between bg-gray-100 dark:bg-gray-900" | ||
key={country} | ||
transition={{ delay: 0.2 }} | ||
> | ||
<motion.div | ||
initial={{ width: 0 }} | ||
animate={{ width: `${(views * 100) / totalViews}%` }} | ||
transition={{ delay: 0.3 }} | ||
className="absolute h-full bg-gray-200 dark:bg-dark-default" | ||
/> | ||
<div className="relative pl-2 py-1">{country}</div> | ||
<div className="relative pr-2 text-blue-dark-sky font-semibold">{views}</div> | ||
</motion.div> | ||
))} | ||
</AnimatePresence> | ||
</div> | ||
) : ( | ||
<></> | ||
); | ||
} |
52 changes: 52 additions & 0 deletions
52
...icPages)/entry/[category]/[author]/[permlink]/_components/entry-page-stats-by-devices.tsx
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 i18next from "i18next"; | ||
import { useMemo } from "react"; | ||
import { useGetStatsQuery } from "@/api/queries"; | ||
import { AnimatePresence, motion } from "framer-motion"; | ||
|
||
interface Props { | ||
totalViews: number; | ||
cleanedPathname: string; | ||
} | ||
|
||
export function EntryPageStatsByDevices({ totalViews, cleanedPathname }: Props) { | ||
const { data: stats } = useGetStatsQuery(cleanedPathname, ["visit:device"]).useClientQuery(); | ||
|
||
const devices = useMemo( | ||
() => | ||
stats?.results?.reduce<Record<string, number>>((acc, result) => { | ||
const country = result.dimensions[0]; | ||
const views = +result.metrics[1]; | ||
return { ...acc, [country]: (acc[country] ?? 0) + views }; | ||
}, {}) ?? {}, | ||
[stats?.results] | ||
); | ||
const devicesList = useMemo(() => Object.entries(devices).sort((a, b) => b[1] - a[1]), [devices]); | ||
|
||
return devicesList.length > 0 ? ( | ||
<div className="flex flex-col w-full gap-2 text-sm"> | ||
<div className="text-sm opacity-50 pb-2">{i18next.t("entry.stats.devices")}</div> | ||
<AnimatePresence mode="popLayout"> | ||
{devicesList.map(([country, views]) => ( | ||
<motion.div | ||
initial={{ opacity: 0 }} | ||
animate={{ opacity: 1 }} | ||
className="rounded-md overflow-hidden relative w-full flex items-center justify-between bg-gray-100 dark:bg-gray-900" | ||
key={country} | ||
transition={{ delay: 0.2 }} | ||
> | ||
<motion.div | ||
initial={{ width: 0 }} | ||
animate={{ width: `${(views * 100) / totalViews}%` }} | ||
transition={{ delay: 0.3 }} | ||
className="absolute h-full bg-gray-200 dark:bg-dark-default" | ||
/> | ||
<div className="relative pl-2 py-1">{country}</div> | ||
<div className="relative pr-2 text-blue-dark-sky font-semibold">{views}</div> | ||
</motion.div> | ||
))} | ||
</AnimatePresence> | ||
</div> | ||
) : ( | ||
<></> | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
...(dynamicPages)/entry/[category]/[author]/[permlink]/_components/entry-page-stats-item.tsx
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,21 @@ | ||
import { motion } from "framer-motion"; | ||
import { ReactNode } from "react"; | ||
|
||
interface Props { | ||
count: ReactNode; | ||
label: ReactNode; | ||
} | ||
|
||
export function EntryPageStatsItem({ count, label }: Props) { | ||
return ( | ||
<motion.div | ||
initial={{ opacity: 0, scale: 0.875 }} | ||
animate={{ opacity: 1, scale: 1 }} | ||
transition={{ delay: 0.2 }} | ||
className="flex flex-col items-center gap-1" | ||
> | ||
<div className="text-xl lg:text-2xl text-blue-dark-sky">{count}</div> | ||
<div>{label}</div> | ||
</motion.div> | ||
); | ||
} |
Oops, something went wrong.