text: // simpleanalytics by infinixius
  const PORT = process.env.PORT || 80
  const OUTPUT_PATH = process.env.OUTPUT_PATH || "analytics.txt"
  const BACKUP_PATH = process.env.BACKUP_PATH || "backups/%DATE%_%TIME%.txt"
  const CAMPAIGNS = process.env.CAMPAIGNS || "main,test"
  const URL = process.env.URL || "/"
  const ADMIN_KEY = process.env.ADMIN_KEY || "admin"
  const CORS = process.env.CORS || "*"
  const http = require("http")
  const fs = require("fs")
  var analytics = {}
  
  // initialization
  
  if (BACKUP_PATH.startsWith("backups/") && !fs.existsSync("./backups")) {
  	fs.mkdirSync("./backups")
  }
  
  for (campaign of CAMPAIGNS.split(",")) {
  	analytics[campaign] = 0
  }
  
  fs.readFile(OUTPUT_PATH, "utf8", function(err, data) {
  	if (err) return
  	var lines = data.split("\n")
  	for (line of lines) {
  		var raw = line.split(" | ")
  		analytics[raw[0]] = Number(raw[1])
  		if (isNaN(raw[1])) analytics[raw[0]] = 0
  	}
  })
  
  function update() {
  	var writeData = ""
  	for (campaign of Object.keys(analytics)) {
  		writeData += campaign + " | " + analytics[campaign] + "\n"
  	}
  
  	fs.writeFile(OUTPUT_PATH, writeData, function (err) { if (err) throw err })
  }
  
  function backup(callback) {
  	const timestamp = new Date().toLocaleTimeString()
  	.replace(/:/g,"-") // regex for :
  	const datestamp = new Date().toLocaleDateString()
  		.replace(/\//g,"-") // regex for /
  
  	var writeData = `simpleanalytics log taken at ${datestamp} - ${timestamp}\n\n`
  	for (campaign of Object.keys(analytics)) {
  		writeData += campaign + " | " + analytics[campaign] + "\n"
  	}
  
  	fs.writeFile(BACKUP_PATH.replace("%DATE%", datestamp).replace("%TIME%", timestamp), writeData, callback)
  }
  
  // server
  var server = http.createServer(function(req, res) {
  	const headers = {
  		"Access-Control-Allow-Origin": CORS
  	}
  	var url = req.url.replace(URL, "").replace(/\//, "")
  	
  	if (analytics[url] || analytics[url] == 0) {
  		res.writeHead(204, headers)
  		analytics[url] ++
  		res.end()
  
  	} else if (url.endsWith("?EXPORTCAMPAIGNS")) {
  		if (!req.headers.authorization) { res.writeHead(400); return res.end() }
  		if (req.headers.authorization != ADMIN_KEY) { res.writeHead(401); return res.end() }
  
  		res.writeHead(200, headers)
  		res.write(JSON.stringify(analytics))
  		res.end()
  
  	} else if (url.endsWith("?CREATECAMPAIGN")) {
  		if (!req.headers.authorization) { res.writeHead(400); return res.end() }
  		if (req.headers.authorization != ADMIN_KEY) { res.writeHead(401); return res.end() }
  		if (!req.headers.campaign) { res.writeHead(400); return res.end() }
  		if (analytics[req.headers.campaign] || analytics[req.headers.campaign] == 0) { res.writeHead(409); return res.end() }
  
  		analytics[req.headers.campaign] = 0
  		res.writeHead(201, headers)
  		res.end()
  
  	} else if (url.endsWith("?DELETECAMPAIGN")) {
  		if (!req.headers.authorization) { res.writeHead(400); return res.end() }
  		if (req.headers.authorization != ADMIN_KEY) { res.writeHead(401); return res.end() }
  		if (!req.headers.campaign) { res.writeHead(400); return res.end() }
  		if (!analytics[req.headers.campaign] && analytics[req.headers.campaign] != 0) { res.writeHead(404); return res.end() }
  
  		delete analytics[req.headers.campaign]
  		res.writeHead(204, headers)
  		res.end()
  
  	} else if (url.endsWith("?BACKUPCAMPAIGNS")) {
  		if (!req.headers.authorization) { res.writeHead(400); return res.end() }
  		if (req.headers.authorization != ADMIN_KEY) { res.writeHead(401); return res.end() }
  
  		backup(function (err) {
  			if (err) { console.log(err); res.writeHead(500); return res.end()}
  			res.writeHead(201, headers)
  			res.end()
  		})
  	} else { res.writeHead(404, headers) }
  	update()
  }).listen(PORT)
  
  console.log(`Listening on port ${PORT}`)
  backup(function(err) {if (err) console.error(err) })
  