-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First push on this branch. At about 29 minutes in the video for Issue #…
…71.
- Loading branch information
Showing
9 changed files
with
119 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 @@ | ||
const Page = ()=>{ | ||
return <p>Hello world</p> | ||
} | ||
|
||
export default 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 |
---|---|---|
@@ -1,33 +1,3 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
:root { | ||
--foreground-rgb: 0, 0, 0; | ||
--background-start-rgb: 214, 219, 220; | ||
--background-end-rgb: 255, 255, 255; | ||
} | ||
|
||
@media (prefers-color-scheme: dark) { | ||
:root { | ||
--foreground-rgb: 255, 255, 255; | ||
--background-start-rgb: 0, 0, 0; | ||
--background-end-rgb: 0, 0, 0; | ||
} | ||
} | ||
|
||
body { | ||
color: rgb(var(--foreground-rgb)); | ||
background: linear-gradient( | ||
to bottom, | ||
transparent, | ||
rgb(var(--background-end-rgb)) | ||
) | ||
rgb(var(--background-start-rgb)); | ||
} | ||
|
||
@layer utilities { | ||
.text-balance { | ||
text-wrap: balance; | ||
} | ||
} |
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,10 @@ | ||
// NOTE: @about 17:00 - minutes | ||
|
||
import { Redis } from "@upstash/redis"; | ||
|
||
export const redis = new Redis({ | ||
url: "https://us1-sharing-oryx-41733.upstash.io", | ||
token: process.env.REDIS_KEY!, | ||
}); | ||
|
||
// const data = await redis.set("foo", "bar"); |
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,28 @@ | ||
// NOTE: @about 6:00 minutes | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { analytics } from "./utils/analytics"; | ||
|
||
export default async function middleware(req: NextRequest) { | ||
// Anytime the url is `http://localhost:3000/`, `req.nextUrl.pathname === "/"` will evaluates to `true`, it is truthy. | ||
if (req.nextUrl.pathname === "/") { | ||
// track analytics event | ||
// console.log("Tracking"); | ||
// NOTE: @about ?:00 - minutes | ||
try { | ||
// what are we going to track? | ||
// pageview is our namespace that we are going to track | ||
// namespace fancy word for event name | ||
analytics.track("pageview", { page: "/", country: req.geo?.country }); | ||
} catch (error) { | ||
// fail silently | ||
console.log(error); | ||
} | ||
} | ||
return NextResponse.next(); | ||
} | ||
|
||
// See NextJS documentation for why this is needed. | ||
// https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher | ||
export const matcher = { | ||
matcher: ["/"], | ||
}; |
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,35 @@ | ||
// NOTE: @about 11:00 - minutes | ||
|
||
import { redis } from "@/lib/redis"; | ||
import { getDate } from "@/utils/"; | ||
|
||
type AnalyticsArgs = { | ||
retention?: number; | ||
}; | ||
|
||
type TrackOptions = { persist?: boolean }; | ||
|
||
export class Analytics { | ||
private retention: number = 60 * 60 * 24 * 7; | ||
|
||
constructor(opts?: AnalyticsArgs) { | ||
if (opts?.retention) this.retention = opts.retention; | ||
} | ||
|
||
async track(namespace: string, event: object = {}, opts?: TrackOptions) { | ||
let key = `analytics::${namespace}`; | ||
|
||
if (!opts?.persist) { | ||
key += `::${getDate()}`; | ||
} | ||
|
||
// db call to persist this event | ||
await redis.hincrby(key, JSON.stringify(event), 1); | ||
|
||
if (!opts?.persist) await redis.expire(key, this.retention); | ||
} | ||
} | ||
|
||
export const analytics = new Analytics(); | ||
|
||
// analytics.track() |
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,9 @@ | ||
// NOTE: @about 25:00 - minutes | ||
|
||
import { format, subDays } from "date-fns"; | ||
|
||
export const getDate = (sub: number = 0) => { | ||
const dateXDaysAgo = subDays(new Date(), sub); | ||
|
||
return format(dateXDaysAgo, "dd/MM/yyyy"); | ||
}; |