Skip to content

fdxx/pagecounter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

PageCounter is an API for tracking page views, deployed using Cloudflare Workers and Cloudflare D1.

If you want to self-host, check out the self-host branch.

Deploy

git clone https://github.com/fdxx/pagecounter
cd pagecounter
npm install

## https://developers.cloudflare.com/workers/wrangler/ci-cd/#api-token
## Generate your cf token.
export CLOUDFLARE_API_TOKEN=xxx
export CLOUDFLARE_ACCOUNT_ID=xxx

## Create a database
npx wrangler d1 create pagecounter

## After successful creation, write the returned database info into wrangler.toml
[[d1_databases]]
binding = "DB" # available in your Worker on env.DB
database_name = "pagecounter"
database_id = "xxx"

## Create a table
npx wrangler d1 execute pagecounter --remote -y --file ./schema.sql

## Deploy to Cloudflare
npm run deploy

## After successful deployment, the access URL will be returned.
## Open this URL directly, you should see "Hello World"
Deployed pagecounter triggers (0.30 sec)
  https://pagecounter.xx.workers.dev
Current Version ID: xx

How it works

/pageviews

POST Request Routing

Request body

application/json

  • domain: To distinguish different domains.
  • path: Page Path.

Processing

  • If domain && path are found, update count+1.
  • If not found then insert.

Response

json object with count fields.

Example

Request

curl -X POST https://pagecounter.xx.workers.dev/pageviews -H "Content-Type: application/json" -d '{"domain": "exp.com", "path": "/blog/"}'

Response

{"ret":"OK","data":{"_id":1,"domain":"exp.com","path":"/blog/","count":1}}

About

PageCounter is an API for tracking page views.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published