Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

serverless-website-analytics

> DevOps
Open source

A CDK construct that consists of a serverless backend, frontend and client side code to track website analytics

196 stars0 likes0 views
WebsiteGitHub

About

A CDK construct that consists of a serverless backend, frontend and client side code to track website analytics

Serverless Website Analytics

  • Features
  • Objectives
  • Getting started
    • Serverside setup
      • Certificate Requirements
    • Client side setup
      • Standalone Import Script Usage
      • SDK Client Usage
      • Vue
  • Worst case projected costs
  • What's in the box
    • Frontend
    • Backend
      • Anomaly detection
    • Ingestion API
    • Querying data manually
  • Upgrading
  • Sponsors
  • Contributing
  • Docs
  • Roadmap

This is a CDK serverless website analytics construct that can be deployed to AWS. This construct creates backend, frontend and the ingestion APIs.

This solution was designed for multiple websites with low to moderate traffic. It is designed to be as cheap as possible, but it is not free. The cost is mostly driven by the ingestion API that saves the data to S3 through a Kinesis Firehose.

You can see a LIVE DEMO HERE and read about the simulated traffic here

Features

  • Serverless, only pay for the AWS services you use
  • Track multiple site
  • Custom domain (use your own domain or a generic CloudFront domain)
  • Page view tracking (includes time on page)
  • Event tracking
  • Anomaly detection and alerts
  • Privacy focused, don't store any Personally Identifiable Information (PII)
  • You own your data
  • Three Dashboard authentication options; none, basic auth or AWS Cognito.
  • Tracks:
    • Referrers
    • Location by country and city
    • Device type
    • All UTM parameters
    • Query parameters
    • Bot detection
  • Easy integration in any JS framework through:
    • JS/TS SDK
    • Standalone 1-liner `
…

html /api-ingest/v1/event/track/beacon.gif?site=&event=" height="1" width="1" alt="">


#### SDK Client Usage

Install the [client-side library](https://www.npmjs.com/package/serverless-website-analytics-client):

npm install serverless-website-analytics-client

…

typescript ... import * as swaClient from 'serverless-website-analytics-client';

const app = createApp(App); app.use(router);

swaClient.v1.analyticsPageInit({ inBrowser: true, //Not SSR site: "", //example.com apiUrl: "", //https://my-serverless-website-analytics-backend.com // debug: true, }); router.afterEach((event) => { swaClient.v1.analyticsPageChange(event.path); });

app.mount('#app');

export { swaClient };


Event Tracking:

[_./usage/vue/vue-project/src/App.vue_](https://github.com/rehanvdm/serverless-website-analytics-client/blob/master/usage/vue/vue-project/src/App.vue)
```typescript
import {swaClient} from "./main";
...
//                         (event: string, data?: number, category?: string)
swaClient.v1.analyticsTrack("subscribe", 1, "clicks")

…

sql
WITH
cte_data AS (
  SELECT site, page_url, time_on_page, page_opened_at,
         ROW_NUMBER() OVER (PARTITION BY page_id ORDER BY time_on_page DESC) rn
  FROM page_views
  WHERE (site = 'site1' site = 'site2') AND (page_opened_at_date = '2023-10-26' OR page_opened_at_date = '2023-10-27')
),
cte_data_filtered AS (
  SELECT *
  FROM cte_data
  WHERE rn = 1 AND page_opened_at BETWEEN parse_datetime('2023-10-26 22:00:00.000','yyyy-MM-dd HH:mm:ss.SSS')
        AND parse_datetime('2023-11-03 21:59:59.999','yyyy-MM-dd HH:mm:ss.SSS')
),
cte_data_by_page_view AS (
SELECT
 site,
 page_url,
 COUNT(*) as "views",
 ROUND(AVG(time_on_page),2) as "avg_time_on_page"
FROM cte_data_filtered
GROUP BY site, page_url
)
SELECT *
FROM cte_data_by_page_view
ORDER BY views DESC, page_url ASC

Events query:

sql
WITH
cte_data AS (
  SELECT site, category, event, data, tracked_at,
         ROW_NUMBER() OVER (PARTITION BY event_id) rn
  FROM events
  WHERE (site = 'site1' site = 'site2') AND (tracked_at_date = '2023-11-03' OR tracked_at_date = '2023-11-04')
),
cte_data_filtered AS (
  SELECT *
  FROM cte_data
  WHERE rn = 1 AND tracked_at BETWEEN parse_datetime('2023-11-03 22:00:00.000','yyyy-MM-dd HH:mm:ss.SSS')
        AND parse_datetime('2023-11-04 21:59:59.999','yyyy-MM-dd HH:mm:ss.SSS')
),
cte_data_by_event AS (
SELECT
 site,
 category,
 event,
 COUNT(data) as "count",
 ROUND(AVG(data),2) as "avg",
 MIN(data) as "min",
 MAX(data) as "max",
 SUM(data) as "sum"
FROM cte_data_filtered
GROUP BY site, category, event
)
SELECT *
FROM cte_data_by_event
ORDER BY count DESC, category ASC, event ASC

…

bash

npm install npm install serverless-website-analytics@~1

…


[Tried to create resource record set [name='analytics.rehanvdm.com.', type='A'] but it already exists]
[Tried to create resource record set [name='login.analytics.rehanvdm.com.', type='A'] but it already exists]

Sponsors

Proudly sponsored by:

  • Cloud Glance - A Single pane of glass for all your AWS credentials and more.
  • Interested in sponsoring?

Contributing

See CONTRIBUTING.md for more info on how to contribute + design decisions.

Docs

  • ANOMALY_DETECTION.md
  • CONTRIBUTING.md
  • COST.md
  • DEMO-TRAFFIC.md
  • EJECTING_FROM_PROJEN.md
  • DEPENDENCIES.md

Roadmap

Can be found in the here

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptanalyticscdkserverless

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理