-
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.
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
1 parent
2343f82
commit f6613c1
Showing
10 changed files
with
713 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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,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') | ||
|
||
} | ||
|
||
} |
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
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
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
Empty file.
Oops, something went wrong.
f6613c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
wordle-am – ./
wordle-am-quantum9innovation.vercel.app
wordle-am.vercel.app
wordle-am-git-main-quantum9innovation.vercel.app