Analytics & Link Statistics
Relevant source files
The following files were used as context for generating this wiki page:
- src/client/user/components/Drawer/ControlPanel/LinkAnalytics/index.tsx
- src/client/user/components/Drawer/ControlPanel/LinkAnalytics/util/statistics.ts
- src/client/user/components/Drawer/ControlPanel/LinkAnalytics/DailyStatistics.tsx
- src/client/user/components/Drawer/ControlPanel/LinkAnalytics/widgets/DailyStatistics/DownloadClicksButton.tsx
- src/server/util/dogstatsd.ts
- src/client/home/components/StatisticsSliver.tsx
- test/server/repositories/UrlRepository.test.ts
GoGovSG provides real-time analytics for shortened links, allowing users to track link popularity and engagement metrics. The system tracks total clicks, daily click distributions, device-specific usage, and temporal heatmaps to provide comprehensive insights into link performance.
Sources: src/client/user/components/Drawer/ControlPanel/LinkAnalytics/index.tsx:210-224, public/locales/gov/en/translation.json
System Architecture & Data Flow
The analytics system operates through a multi-tier approach:
- Server-Side Tracking: The backend records every short-link access event.
- Infrastructure Storage: Persistent metrics reside in a PostgreSQL database, while transient click statistics and frequently used links are managed in a dedicated Redis instance (REDIS_STAT_URI).
- Client-Side Visualization: The React frontend fetches this data via internal APIs to render interactive charts and statistical summaries.
Component Interaction Flow
The following diagram illustrates how the frontend components request and receive link statistics.
Client-Side Implementation
The frontend organizes analytics into three primary views: Devices, Clicks, and Traffic. The LinkAnalytics component serves as the entry point within the user control panel.
Analytics Categories
| Category | Component | Description |
|---|---|---|
| Devices | DeviceStatistics | Visualizes the distribution of clicks across different hardware or browser types. |
| Clicks | DailyStatistics | Displays a line chart of click counts over the past week. |
| Traffic | HeatMapStatistics | Shows engagement density across different days of the week. |
Sources: src/client/user/components/Drawer/ControlPanel/LinkAnalytics/index.tsx:168-200
Data Retrieval
The useStatistics hook manages the lifecycle of an analytics request. It tracks the status of the fetch and the contents of the response.
export const useStatistics = (shortUrl: string) => {
const [statistics, setStatistics] = useState<LinkStatisticsState>(initialState)
useEffect(() => {
const fetchStatistics = async () => {
const endpoint = `/api/link-stats?url=${shortUrl}`
const response = await get(endpoint)
// ... process response
}
if (shortUrl) fetchStatistics()
}, [])
return statistics
}
Sources: src/client/user/components/Drawer/ControlPanel/LinkAnalytics/util/statistics.ts:16-43
Data Models & Repositories
The system uses specific Sequelize models to store and aggregate statistics. These are managed primarily through the UrlRepository.
Key Data Structures
- UrlClicks: Stores aggregated click counts for a specific URL.
- Daily Clicks: Tracks click counts mapped to specific calendar dates.
- WeekdayClicks: Maps clicks to days of the week for heatmap generation.
- Devices: Stores user-agent derived metrics.
Sources: test/server/repositories/UrlRepository.test.ts:31-48
Statistical Metadata
The repository includes methods to retrieve links along with their total click counts, ensuring that analytics are readily available during directory searches or user dashboard rendering.
Sources: test/server/repositories/UrlRepository.test.ts:114-126
Exporting Analytics
Users can download full click statistics in CSV format. The DownloadClicksButton triggers a request to the /api/link-stats endpoint with a large day offset (defaulting to 3650 days) to capture historical data.
- Format: The data is converted into a Blob with
text/csv;charset=utf-8. - IE Support: Includes a fallback using
navigator.msSaveBlobfor older browser versions.
Global System Metrics
The landing page displays aggregated platform-wide statistics to demonstrate the scale and trustworthiness of the service.
These platform metrics are loaded via homeActions.loadStats() and formatted for the landing page UI.
Sources: src/client/home/components/StatisticsSliver.tsx:85-110
Monitoring & Instrumentation
The system utilizes dogstatsd (via the hot-shots library) to track internal operational metrics. This includes:
shortlink.clicks: Counter for every redirect event.shortlink.create: Tracks link creation frequency.search.user.url: Monitors user search patterns within analytics interfaces.
Sources: src/server/util/dogstatsd.ts:4-24
Summary
The Analytics & Link Statistics module provides users with actionable insights into link engagement through real-time tracking and visualization. By leveraging Redis for performance and PostgreSQL for persistence, GoGovSG ensures that both individual link owners and platform administrators can monitor usage patterns effectively.