Skip to content

Commit

Permalink
Analytics (#19)
Browse files Browse the repository at this point in the history
Add analytics with FaunaDB and Vercel serverless functions, fix some typos cleanup unnecessary files.
Commits shown below:

* Add serverless analytics

* Make serverless functions discoverable

Move from root to /App/

* Add verbosity to analytics script for debugging

* Further debug analytics

* Debug analytics

* Pre-debug analytics fix

* Enhance logging

* Fix issue in log func

* Respect DNT

* Add analytics scripts to all websites

* Fix typos

Various cleanup stuffs
  • Loading branch information
quantum9Innovation committed May 30, 2022
1 parent 2343f82 commit f6613c1
Show file tree
Hide file tree
Showing 10 changed files with 713 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
81 changes: 81 additions & 0 deletions App/api/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// ----- /api/analytics.js -----

// Load database resources
const faunadb = require('faunadb')
let q = faunadb.query


// Instantiate client
let client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET,
domain: 'db.fauna.com',
port: 443,
scheme: 'https',
})


// Log data
const log = async obj => {

let create = client.query(
q.Create(q.Collection('analytics'), { data: obj })
).catch(e => {

console.log(e)
return false

})

return create

}


// Serverless function
// The main, exported, function of the endpoint,
// dealing with the request and subsequent response
module.exports = async (req, res) => {

// Parse the request
let method = req.method
let body = JSON.parse(req.body)

if (method != 'POST') res.status(405).send('Method not allowed: ' + method)

try {

// Get request headers
const referer = req.headers['referer']
const ip = req.headers['x-forwarded-for']
const ua = req.headers['user-agent']
const ul = req.headers['accept-language']
const dnt = req.headers['dnt']
const meta = { ip, ua, ul, referer }

// Respect 'Do Not Track'
if (dnt == '1') return res.status(200).send('DNT respected')

// Region info
const country = req.headers['x-vercel-ip-country']
const region = req.headers['x-vercel-ip-country-region']
const city = req.headers['x-vercel-ip-city']
const loc = { city, region, country }

// Compile database entry
info = { body, meta, loc }

// Log data
let output = await log(info)

// Respond
if (output === false) res.status(500).send('Database connection error')
else res.status(200).send('OK')

} catch (e) {

console.log(error)
res.status(500).send('Internal server error')

}

}
11 changes: 11 additions & 0 deletions App/four/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ const logWord = () => {
}
const logWin = () => {

if (Cookies.get('4/win')) return

let period = expire
Cookies.set('4/win', square[0], { expires: period })

Expand All @@ -608,6 +610,15 @@ const logWin = () => {

Cookies.set('4/win-' + M + '/' + D + '/' + Y, square[0], { expires: 365 })

let xhttp = new XMLHttpRequest()
xhttp.open('POST', '../api/analytics', true)
xhttp.send(JSON.stringify({
game: 'four',
version: 'beta.2',
recDate: M + '/' + D + '/' + Y,
chances: square[0],
}))

}
const getWords = () => {

Expand Down
17 changes: 14 additions & 3 deletions App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ const findInDictionary = word => {


// Cookie handlers
const expireeDate = () => {
const expiryDate = () => {

let today = new Date()

Expand Down Expand Up @@ -599,6 +599,8 @@ const logWord = () => {
}
const logWin = () => {

if (Cookies.get('win')) return

let period = expire
Cookies.set('win', square[0], { expires: period })

Expand All @@ -607,6 +609,15 @@ const logWin = () => {
const D = period.getDate()

Cookies.set('win-' + M + '/' + D + '/' + Y, square[0], { expires: 365 })

let xhttp = new XMLHttpRequest()
xhttp.open('POST', './api/analytics', true)
xhttp.send(JSON.stringify({
game: 'standard',
version: 'beta.2',
recDate: M + '/' + D + '/' + Y,
chances: square[0],
}))

}
const getWords = () => {
Expand Down Expand Up @@ -1017,7 +1028,7 @@ window.onload = () => {
)
).then ( () => {

expire = expireeDate()
expire = expiryDate()
if (waiting) {
window.location.replace('/waiting.html')
}
Expand All @@ -1036,7 +1047,7 @@ window.onload = () => {

}

// Intialize event listeners
// Initialize event listeners
document.addEventListener(
'keyup',
(e) => {
Expand Down
15 changes: 12 additions & 3 deletions App/practice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ const findInDictionary = word => {


// Cookie handlers
const expireeDate = () => {
const expiryDate = () => {

let today = new Date()
let todayEAT = new Date(
Expand Down Expand Up @@ -550,6 +550,15 @@ const logWin = () => {

// Cookies.set('win-' + M + '/' + D + '/' + Y, square[0], { expires: 365 })

let xhttp = new XMLHttpRequest()
xhttp.open('POST', '../api/analytics', true)
xhttp.send(JSON.stringify({
game: 'practice',
version: 'beta.2',
recDate: M + '/' + D + '/' + Y,
chances: square[0],
}))

}
const getWords = () => {

Expand Down Expand Up @@ -913,7 +922,7 @@ window.onload = () => {
)
).then ( () => {

expire = expireeDate()
expire = expiryDate()
makeWord().then( () => {

endLoad()
Expand All @@ -928,7 +937,7 @@ window.onload = () => {

}

// Intialize event listeners
// Initialize event listeners
document.addEventListener(
'keyup',
(e) => {
Expand Down
17 changes: 14 additions & 3 deletions App/time/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ const findInDictionary = word => {


// Cookie handlers
const expireeDate = () => {
const expiryDate = () => {

let today = new Date()

Expand Down Expand Up @@ -622,6 +622,8 @@ const logVisit = () => {
}
const logWin = () => {

if (Cookies.get('T/win')) return

let period = expire
Cookies.set('T/win', nwords, { expires: period })

Expand All @@ -631,6 +633,15 @@ const logWin = () => {

Cookies.set('T/win-' + M + '/' + D + '/' + Y, nwords, { expires: 365 })

let xhttp = new XMLHttpRequest()
xhttp.open('POST', '../api/analytics', true)
xhttp.send(JSON.stringify({
game: 'time',
version: 'beta.2',
recDate: M + '/' + D + '/' + Y,
chances: nwords,
}))

}
const getWords = () => {

Expand Down Expand Up @@ -1136,7 +1147,7 @@ window.onload = () => {
)
).then ( () => {

expire = expireeDate()
expire = expiryDate()
if (waiting) {
window.location.replace('../waiting.html')
}
Expand All @@ -1159,7 +1170,7 @@ window.onload = () => {

}

// Intialize event listeners
// Initialize event listeners
document.addEventListener(
'keyup',
(e) => {
Expand Down
4 changes: 2 additions & 2 deletions App/waiting.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ <h1 id="title">ቃልነት | መጠበቅ</h1>

}

const expireeDate = () => {
const expiryDate = () => {

let today = new Date()

Expand All @@ -126,7 +126,7 @@ <h1 id="title">ቃልነት | መጠበቅ</h1>


// Initialize
expireeDate()
expiryDate()
updateCountdown()
window.setInterval(updateCountdown, 1000)

Expand Down
Empty file removed Scripts/requirements.txt
Empty file.

1 comment on commit f6613c1

@vercel
Copy link

@vercel vercel bot commented on f6613c1 May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.