Log site referrals efficiently without any large scale, privacy degrading trackers.
[โ] Serverless.
[โ] Small client bundle.
[โ] Served on Cloudflare.
[โ] Privacy focused.
[โ] GNU GPL v3.0 Licence.
[โ] Readable internal code.
- A Cloudflare account
- A Fauna Account
- Cloudflare Wrangler Installed
- A Github Account
- A Basic Knowlage of Git / GitHub
Our first step will be configuring a database through Fauna's dashboard.
After you've logged into the dashboard, create a new database named RefTracker and choose a region closest to your users:
We're now going to create the Referrals
collection to store the documents for our inventory.
To accomplish this, we're going to execute an FQL query on the Fauna shell which is available from the main menu of the dashboard:
To create a collection, simply execute the following FQL query that uses the CreateCollection function:
CreateCollection({name: "Referrals"})
The result will be similar to this:
{
ref: Collection("Referrals"),
ts: 1617851434855000,
history_days: 30,
name: "Referrals"
}
To be able to connect to the database from a Worker we now need to create a key.
Go to the Security
section of the dashboard and create a new key with the Server
role:
After saving, Fauna will show us the key's secret which we'll use to execute queries from our Worker:
Save the secret somewhere safe as Fauna will never show it again.
Also, never commit this secret to your Git repository. The Server role is all-powerful. Anyone with this secret would have full access to the database. The initial configuration of Fauna is ready.
Fork this on github and pull it to your computer. (If your using this I would assume you would know what I'm talking about)
Go to Cloudflare and open account settings.
Go to the API Tokens tab and generate a new token.
Select the Edit Cloudflare Workers
template.
Set Account Resources
to your email address and set Zone Resources
to "All Zones from an account and your email address.
Continue through and create the token. Copy the token and create a new Github secret in settings (Settings => Secrets => New Repository Secret) with the name CF_API_TOKEN
and paste in the Cloudflare token. Save.
Login to Cloudflare.
wrangler login
Run
wrangler secret put FAUNA_SECRET
then paste your Fauna Secret saved earlier.
Open configuration.json
and modify regionString
, corsOriginDomain
, collectionName
, siteName
, refPath
, githubRefTrackerUrl
to the respective labeled values
Variable Name | Intended Content |
---|---|
regionString | Region string were your database is held. |
corsOriginDomain | Page that will be tracked CORS domain. |
collectionName | The name of your database collection. |
siteName | Name for logs (Uninportant). |
refPath | Path of url to send logs to. |
githubRefTrackerUrl | The path to reftracker.min.js in your github repository (/client/reftracker.min.js ). |
Push your changes
Create an HTML Document or use your production website.
To add tracking, put:
<script defer data-send-location="https://[Your worker URL]/[refPath]" src="http://[Your worker URL]/reftracker.min.js"></script>
in the header, it should automatically start tracking requests.
And thats it, just access your website with ?ref=example
as a paramater and check your database!