diff --git a/src/components/CustomRoadmap/CustomRoadmap.tsx b/src/components/CustomRoadmap/CustomRoadmap.tsx index bd823c9a6..9a039e0a6 100644 --- a/src/components/CustomRoadmap/CustomRoadmap.tsx +++ b/src/components/CustomRoadmap/CustomRoadmap.tsx @@ -101,22 +101,10 @@ export function CustomRoadmap(props: CustomRoadmapProps) { setIsLoading(false); } - async function trackVisit() { - if (!isLoggedIn() || isEmbed) { - return; - } - - await httpPost(`${import.meta.env.PUBLIC_API_URL}/v1-visit`, { - resourceId: id, - resourceType: 'roadmap', - }); - } - useEffect(() => { getRoadmap().finally(() => { hideRoadmapLoader(); }); - trackVisit().then(); }, []); if (isLoading) { diff --git a/src/components/FrameRenderer/renderer.ts b/src/components/FrameRenderer/renderer.ts index 415c8ffd6..82172420c 100644 --- a/src/components/FrameRenderer/renderer.ts +++ b/src/components/FrameRenderer/renderer.ts @@ -117,19 +117,6 @@ export class Renderer { }); } - trackVisit() { - if (!isLoggedIn()) { - return; - } - - window.setTimeout(() => { - httpPost(`${import.meta.env.PUBLIC_API_URL}/v1-visit`, { - resourceId: this.resourceId, - resourceType: this.resourceType, - }).then(() => null); - }, 0); - } - onDOMLoaded() { if (!this.prepareConfig()) { return; @@ -138,8 +125,6 @@ export class Renderer { const urlParams = new URLSearchParams(window.location.search); const roadmapType = urlParams.get('r'); - this.trackVisit(); - if (roadmapType) { this.switchRoadmap(`/${roadmapType}.json`); } else { diff --git a/src/components/PageVisit/PageVisit.tsx b/src/components/PageVisit/PageVisit.tsx new file mode 100644 index 000000000..defcb7db2 --- /dev/null +++ b/src/components/PageVisit/PageVisit.tsx @@ -0,0 +1,25 @@ +import { useEffect } from 'react'; +import { isLoggedIn } from '../../lib/jwt'; +import { httpPost } from '../../lib/http'; +import type { ResourceType } from '../../lib/resource-progress'; + +type PageVisitProps = { + resourceId?: string; + resourceType?: ResourceType; +}; + +export function PageVisit(props: PageVisitProps) { + const { resourceId, resourceType } = props; + + useEffect(() => { + if (!isLoggedIn()) { + return; + } + + httpPost(`${import.meta.env.PUBLIC_API_URL}/v1-visit`, { + ...(resourceType && { resourceType, resourceId }), + }).finally(() => {}); + }, []); + + return null; +} diff --git a/src/layouts/BaseLayout.astro b/src/layouts/BaseLayout.astro index 049b72619..5baa80f82 100644 --- a/src/layouts/BaseLayout.astro +++ b/src/layouts/BaseLayout.astro @@ -11,6 +11,8 @@ import { Toaster } from '../components/Toast'; import { PageSponsor } from '../components/PageSponsor'; import { siteConfig } from '../lib/config'; import '../styles/global.css'; +import { PageVisit } from '../components/PageVisit/PageVisit'; +import type { ResourceType } from '../lib/resource-progress'; export interface Props { title: string; @@ -25,6 +27,8 @@ export interface Props { initialLoadingMessage?: string; permalink?: string; jsonLd?: Record[]; + resourceId?: string; + resourceType?: ResourceType; } const { @@ -39,6 +43,8 @@ const { jsonLd = [], redirectUrl = '', initialLoadingMessage = '', + resourceId, + resourceType, } = Astro.props; // Remove trailing slashes to consider the page as canonical @@ -55,7 +61,7 @@ const gaPageIdentifier = Astro.url.pathname .replace(/\//g, ':'); --- - + @@ -171,5 +177,10 @@ const gaPageIdentifier = Astro.url.pathname + diff --git a/src/pages/[roadmapId]/index.astro b/src/pages/[roadmapId]/index.astro index f2285077e..e30851491 100644 --- a/src/pages/[roadmapId]/index.astro +++ b/src/pages/[roadmapId]/index.astro @@ -67,6 +67,8 @@ if (roadmapFAQs.length) { keywords={roadmapData.seo.keywords} noIndex={roadmapData.isUpcoming} jsonLd={jsonLdSchema} + resourceId={roadmapId} + resourceType='roadmap' >