This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
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.
implemented the analytics from blalange.org
- Loading branch information
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import PocketBase from 'pocketbase'; | ||
import { getSessionId } from "./session"; | ||
import { dbEndpoint } from './conf'; | ||
|
||
let pb = new PocketBase(dbEndpoint); | ||
|
||
let lastValues = { | ||
userAgent: typeof window !== 'undefined' ? '' : '', | ||
language: typeof window !== 'undefined' ? '' : '', | ||
url: typeof window !== 'undefined' ? '' : '' | ||
}; | ||
|
||
let ip = ''; | ||
|
||
export { startAnalyticsMonitoring }; | ||
|
||
async function collect2() { | ||
if (typeof window === 'undefined') return; // Exit if not in a browser environment | ||
|
||
const userAgent = navigator.userAgent; | ||
const language = navigator.language; | ||
const unix = new Date().getTime(); | ||
const url = window.location.href; | ||
const screenWidth = window.screen.width; | ||
const screenHeight = window.screen.height; | ||
const networkInfo = navigator.connection ? navigator.connection.type : 'unknown'; | ||
const referrer = document.referrer; | ||
|
||
if (ip == '') { | ||
ip = await fetch('https://blalange.org/api/ip').then((res) => res.text()); | ||
} | ||
|
||
if ( | ||
userAgent !== lastValues.userAgent || | ||
language !== lastValues.language || | ||
url !== lastValues.url | ||
) { | ||
lastValues = { userAgent, language, url }; | ||
|
||
return await pb.collection('kf_analytics').create({ | ||
useragent: userAgent, | ||
language: language, | ||
unix: unix, | ||
url: url, | ||
session: getSessionId(), | ||
ip: ip, | ||
width: screenWidth, | ||
height: screenHeight, | ||
network: networkInfo, | ||
referrer: referrer | ||
}); | ||
} else { | ||
console.log('Collect2: Nothing has changed, not running.'); | ||
} | ||
} | ||
|
||
function startAnalyticsMonitoring() { | ||
if (typeof window === 'undefined') return; // Exit if not in a browser environment | ||
|
||
// Set up MutationObserver to watch for changes in the document | ||
const observer = new MutationObserver(async () => { | ||
await collect2(); | ||
}); | ||
|
||
observer.observe(document, { childList: true, subtree: true }); | ||
|
||
// Set up interval to check for changes in navigator.userAgent and navigator.language | ||
setInterval(async () => { | ||
await collect2(); | ||
}, 1000); // Check every second | ||
} |
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,20 @@ | ||
export function getSessionId() { | ||
// Check if a session ID already exists in local storage | ||
let sessionId = localStorage.getItem('sessionId'); | ||
|
||
// If no session ID exists, generate a new one and store it | ||
if (!sessionId) { | ||
console.log("No session ID found. Generating a new one."); | ||
sessionId = generateUniqueId(); | ||
localStorage.setItem('sessionId', sessionId); | ||
} else { | ||
console.log("Existing session ID found"); | ||
} | ||
|
||
return sessionId; | ||
} | ||
|
||
function generateUniqueId() { | ||
// Generate a unique ID using a combination of Date.now() and Math.random() | ||
return 'session-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9); | ||
} |
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