Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync main to dev after release 0.4.6 #1929

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 24 additions & 58 deletions web/utils/umami.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,31 @@
import { useEffect } from 'react'

import Script from 'next/script'

// Define the type for the umami data object
interface UmamiData {
version: string
}

declare global {
interface Window {
umami:
| {
track: (event: string, data?: UmamiData) => void
}
| undefined
}
}

const Umami = () => {
const appVersion = VERSION
const analyticsHost = ANALYTICS_HOST
const analyticsId = ANALYTICS_ID

useEffect(() => {
if (!appVersion || !analyticsHost || !analyticsId) return
const ping = () => {
// Check if umami is defined before ping
if (window.umami !== null && typeof window.umami !== 'undefined') {
window.umami.track(appVersion, {
version: appVersion,
})
}
}

// Wait for umami to be defined before ping
if (window.umami !== null && typeof window.umami !== 'undefined') {
ping()
} else {
// Listen for umami script load event
document.addEventListener('umami:loaded', ping)
}

// Cleanup function to remove event listener if the component unmounts
return () => {
document.removeEventListener('umami:loaded', ping)
}
}, [appVersion, analyticsHost, analyticsId])

return (
<>
{appVersion && analyticsHost && analyticsId && (
<Script
src={analyticsHost}
data-website-id={analyticsId}
data-cache="true"
defer
onLoad={() => document.dispatchEvent(new Event('umami:loaded'))}
/>
)}
</>
)
if (!VERSION || !ANALYTICS_HOST || !ANALYTICS_ID) return
fetch(ANALYTICS_HOST, {
method: 'POST',
// eslint-disable-next-line @typescript-eslint/naming-convention
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
payload: {
website: ANALYTICS_ID,
hostname: 'jan.ai',
screen: `${screen.width}x${screen.height}`,
language: navigator.language,
referrer: 'index.html',
data: { version: VERSION },
type: 'event',
title: document.title,
url: 'index.html',
name: VERSION,
},
type: 'event',
}),
})
}, [])

return <></>
}

export default Umami
Loading