https://github.com/ambersun1234/count-page-views
Simple backend API server for retrieving page views with Google Analytics Data API
https://github.com/ambersun1234/count-page-views
docker docker-compose express google-analytics helm-chart kubernetes nodejs redis skaffold
Last synced: 3 months ago
JSON representation
Simple backend API server for retrieving page views with Google Analytics Data API
- Host: GitHub
- URL: https://github.com/ambersun1234/count-page-views
- Owner: ambersun1234
- License: apache-2.0
- Created: 2024-08-17T13:35:54.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2026-03-30T12:15:21.000Z (3 months ago)
- Last Synced: 2026-04-05T22:36:54.022Z (3 months ago)
- Topics: docker, docker-compose, express, google-analytics, helm-chart, kubernetes, nodejs, redis, skaffold
- Language: TypeScript
- Homepage:
- Size: 679 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Count Page Views

This repo is a simple backend server that retrieve the page count using [Google Analytics Data API](https://developers.google.com/analytics/devguides/reporting/data/v1?hl=zh-tw)\
The tool is build to integrate with **static page** by default, specifically GitHub pages([Minimal Mistakes](https://mmistakes.github.io/minimal-mistakes/))
## Features
+ Aggregate page view count (include redirect URL)
+ Cache page view count with Redis to prevent API rate limit
+ The http response is also cached to prevent unnecessary API call, default to `1 day`(since the view count is not updated frequently by the Google Analytics)
+ Easy deployment with Kubernetes(Helm Chart), Docker-Compose, or standalone
## Prerequisites
+ Enable [Google Analytics Data API](https://developers.google.com/analytics/devguides/reporting/data/v1?hl=zh-tw)
+ Enable Google Analytics and have some page view data
## Environment Variables
copy [.env.example](./.env.example) to `.env` and fill in the following variables
| Name | Description |
|:--|:--|
| PORT | Port number of the server |
| LOG_LEVEL | Log level of the server |
| CORS | Allow origin url for CORS |
| REDIS | Redis connection string |
| GOOGLE_APPLICATION_CREDENTIALS | Path to the Google Cloud credential file |
| ID | Google Analytics View ID |
| START_DATE | Start date of the query |
## API
| Method | Path | Description | Query Parameters |
|:--|:--|:--|:--|
| GET | `/views` | Get the page view count | `url`: URL of the page |
| POST | `/views` | Renew page view count for page ||
## Build
```shell
$ make build
```
## Run
### Standalone
```shell
$ make dev
```
### Docker-Compose
```shell
$ docker-compose build --no-cache
$ docker-compose up -d
```
### Kubernetes
```shell
$ make docker-build
$ make k3d-create
$ make k3d-import
$ make k3d-apply
```
### Helm
```shell
$ make docker-build
$ make k3d-create
$ make k3d-import
$ helm install cpv charts/cpv
```
#### Skaffold
```shell
$ make k3d-create
$ make skaffold-dev
```
## How to Use
As per introduction, this tool is designed to integrate with static page, specifically GitHub pages. So the following steps are based on it(Jekyll).
```html
Views: Loading...
```
```html
fetch(`http://127.0.0.1:8888/views?url={{ page.url }}`)
.then(response => response.json())
.then(data => {
if (isNaN(data.views)) {
data.views = 0;
}
document.getElementById("view-field").innerHTML = Intl.NumberFormat('en', { notation: 'compact' }).format(data.views);
})
.catch(error => {
console.error(error)
});
```
Place the above code snippet in the page you want to display the view count.\
Please note that the `{{ page.url }}` is the Jekyll variable(written in [liquid](https://shopify.github.io/liquid/)) that represents the page URL, for example `/git/git-hook/`, it doesn't contain the domain name.
And call the API to get the view count(remember to replace the URL with the actual backend).
> To beautify the view count, `Intl.NumberFormat` will format the number to a more readable format(e.g. `1.4k`).
The final result will look something like this

or

if the backend server is not accessible at the time.
## Author
+ [ambersun1234](https://github.com/ambersun1234)
## License
This project is licensed under Apache 2.0. See the [LICENSE](./LICENSE) file for details.