feat: implement sponsor (#5480)

* feat: implement sponsor

* wip: add view cookie

* Update src/lib/jwt.ts

---------

Co-authored-by: Kamran Ahmed <kamranahmed.se@gmail.com>
pull/5504/head
Arik Chakma 9 months ago committed by GitHub
parent 285f2c05f2
commit a20400f6a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      src/components/PageSponsor.tsx
  2. 16
      src/lib/jwt.ts

@ -1,8 +1,9 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { httpGet } from '../lib/http'; import { httpGet, httpPatch, httpPost } from '../lib/http';
import { sponsorHidden } from '../stores/page'; import { sponsorHidden } from '../stores/page';
import { useStore } from '@nanostores/react'; import { useStore } from '@nanostores/react';
import { X } from 'lucide-react'; import { X } from 'lucide-react';
import { setViewSponsorCookie } from '../lib/jwt';
export type PageSponsorType = { export type PageSponsorType = {
company: string; company: string;
@ -15,6 +16,7 @@ export type PageSponsorType = {
}; };
type V1GetSponsorResponse = { type V1GetSponsorResponse = {
id?: string;
href?: string; href?: string;
sponsor?: PageSponsorType; sponsor?: PageSponsorType;
}; };
@ -26,6 +28,8 @@ type PageSponsorProps = {
export function PageSponsor(props: PageSponsorProps) { export function PageSponsor(props: PageSponsorProps) {
const { gaPageIdentifier } = props; const { gaPageIdentifier } = props;
const $isSponsorHidden = useStore(sponsorHidden); const $isSponsorHidden = useStore(sponsorHidden);
const [sponsorId, setSponsorId] = useState<string | null>(null);
const [sponsor, setSponsor] = useState<PageSponsorType>(); const [sponsor, setSponsor] = useState<PageSponsorType>();
const loadSponsor = async () => { const loadSponsor = async () => {
@ -59,6 +63,7 @@ export function PageSponsor(props: PageSponsorProps) {
} }
setSponsor(response.sponsor); setSponsor(response.sponsor);
setSponsorId(response?.id || null);
window.fireEvent({ window.fireEvent({
category: 'SponsorImpression', category: 'SponsorImpression',
@ -69,6 +74,20 @@ export function PageSponsor(props: PageSponsorProps) {
}); });
}; };
const clickSponsor = async (sponsorId: string) => {
const { response, error } = await httpPatch<{ status: 'ok' }>(
`${import.meta.env.PUBLIC_API_URL}/v1-view-sponsor/${sponsorId}`,
{},
);
if (error || !response) {
console.error(error);
return;
}
setViewSponsorCookie(sponsorId);
};
useEffect(() => { useEffect(() => {
window.setTimeout(loadSponsor); window.setTimeout(loadSponsor);
}, []); }, []);
@ -85,12 +104,13 @@ export function PageSponsor(props: PageSponsorProps) {
target="_blank" target="_blank"
rel="noopener sponsored nofollow" rel="noopener sponsored nofollow"
className="fixed bottom-[15px] right-[15px] z-50 flex max-w-[350px] bg-white shadow-lg outline-0 outline-transparent" className="fixed bottom-[15px] right-[15px] z-50 flex max-w-[350px] bg-white shadow-lg outline-0 outline-transparent"
onClick={() => { onClick={async () => {
window.fireEvent({ window.fireEvent({
category: 'SponsorClick', category: 'SponsorClick',
action: `${company} Redirect`, action: `${company} Redirect`,
label: gaLabel || `${gaPageIdentifier} / ${company} Link`, label: gaLabel || `${gaPageIdentifier} / ${company} Link`,
}); });
await clickSponsor(sponsorId || '');
}} }}
> >
<span <span

@ -109,3 +109,19 @@ export function removeAIReferralCode() {
domain: import.meta.env.DEV ? 'localhost' : '.roadmap.sh', domain: import.meta.env.DEV ? 'localhost' : '.roadmap.sh',
}); });
} }
export function setViewSponsorCookie(sponsorId: string) {
const key = `vsc-${sponsorId}`;
const alreadyExist = Cookies.get(key);
if (alreadyExist) {
return;
}
Cookies.set(key, '1', {
path: '/',
expires: 1,
sameSite: 'lax',
secure: true,
domain: import.meta.env.DEV ? 'localhost' : '.roadmap.sh',
});
}

Loading…
Cancel
Save