chore: eventing + refactoring

This commit is contained in:
Cory Dransfeldt 2024-03-24 21:00:49 -07:00
parent 13e7b489ef
commit 9a957159e9
No known key found for this signature in database
3 changed files with 15 additions and 14 deletions

View file

@ -5,31 +5,34 @@ export default async (request, context) => {
const params = new URL(request['url']).searchParams
const ns = params.get('ns')
const page = params.get('page')
const num = params.get('num') || 'unknown'
const ig = params.get('ig')
const setUrl = (id, event) => `https://cdn.usefathom.com/?h=${encodeURIComponent(page)}&sid=CWSVCDJC&cid=${id}&name=${encodeURIComponent(event)}`
const lang = decodeURIComponent(params.get('lang'))
const nav = decodeURIComponent(params.get('nav'))
const notLang = !lang || lang === 'null' || lang === 'undefined'
const notNav = !nav || nav === 'null' || nav === 'undefined'
const ig = params.get('ig')
const setUrl = (id, event) => `https://cdn.usefathom.com/?h=${encodeURIComponent(page)}&sid=CWSVCDJC&cid=${id}&name=${encodeURIComponent(event)}`
const acceptLanguage = notLang ? request['headers'].get('accept-language') : lang
const userAgent = notNav ? request['headers'].get('user-agent') : nav
const headers = {
'Accept-Language': acceptLanguage,
'User-Agent': userAgent
}
let url
const id = crypto.createHash('md5').update(`${context['ip']}${context['geo']['city']}`).digest('hex')
const ids = getStore('ids')
const userId = await ids.get(id)
if (!userId) await ids.set(id, id)
const idVal = await ids.get(id)
if (ig) return
if (ns) {
const id = crypto.createHash('md5').update(`${context['ip']}${context['geo']['city']}`).digest('hex')
const ids = getStore('ids')
const userId = await ids.get(id)
if (!userId) await ids.set(id, id)
const idVal = await ids.get(id)
url = setUrl(idVal, 'noscript visit')
} else {
url = setUrl(num, 'Blocked visit')
url = setUrl(idVal, 'Blocked visit')
}
fetch(url, { headers })

View file

@ -1,6 +1,6 @@
{
"name": "coryd.dev",
"version": "8.7.4",
"version": "8.7.5",
"description": "The source for my personal site. Built using 11ty.",
"type": "module",
"scripts": {

View file

@ -1,7 +1,5 @@
const id = Math.floor(Math.random() * Date.now())
let i;
let url = `https://coryd.dev/api/event/?page=${encodeURIComponent(window.location.href)}&num=${sessionStorage.getItem('id' || id)}&lang=${encodeURIComponent(navigator.language)}&nav=${encodeURIComponent(navigator.userAgent)}`
if (window.sessionStorage && !window.sessionStorage?.getItem('id')) sessionStorage.setItem('id', id)
let ig;
let url = `https://coryd.dev/api/event/?page=${encodeURIComponent(window.location.href)}&lang=${encodeURIComponent(navigator.language)}&nav=${encodeURIComponent(navigator.userAgent)}`
if (window.localStorage && window.localStorage.getItem('ig')) {
ig = localStorage.getItem('ig')
url = `${url}&ig=${ig}`