- 1. Overview
- 2. Project Structure
- 3. Shared Constants
- 4. Cloud Function Implementation
- 5. Frontend Integration Example (Landing Page)
- 6. Security & Best Practices
- 7. References
- 8. Local Testing with Firebase Emulator Suite when adding new repos
This document describes the architecture for automated fetching and storage of GitHub traffic analytics (views and clones) for the following repositories:
zoechbauer/z-control-landing-pagezoechbauer/z-control-qr-code-generatorzoechbauer/z-control-multi-language-translatorzoechbauer/z-control-Backup-scriptszoechbauer/copilot-learning-calculator
The analytics data is retrieved via a scheduled Firebase Cloud Function and stored in Firestore for display in the landing page app.
- Purpose: Collect daily GitHub traffic insights for multiple repositories.
- Storage: Data is saved in Firestore under two collections:
githubAnalyticsTraffic: Stores the latest 14 days of analytics (overwrites data).githubAnalyticsTrafficHistory: Appends each day's analytics, building a historical record.
- Display: The landing page fetches and displays analytics data from Firestore.
- Security: GitHub Personal Access Token is stored securely as a Firebase environment variable.
landing-page/
├─ src/
│ ├─ app/
│ └─ ...
├─ shared/
│ └─ GitHubConstants.ts # Shared constants for BE & FE (frontend and general use)
├─ package.json
├─ tsconfig.json
└─ ...
Firestore structure:
githubAnalyticsTraffic/
├─ copilot-learning-calculator
├─ z-control-multi-language-translator
├─ z-control-Backup-scripts
├─ z-control-qr-code-generator
└─ z-control-landing-page
githubAnalyticsTrafficHistory/
├─ copilot-learning-calculator
├─ z-control-multi-language-translator
├─ z-control-Backup-scripts
├─ z-control-qr-code-generator
└─ z-control-landing-page
Each document in both collections contains:
timestamp: ISO string of last updateviews: Object with total and daily view entriesclones: Object with total and daily clone entries
In githubAnalyticsTrafficHistory, the views and clones arrays accumulate all daily entries since the function started.
A shared constants file is used for both backend and frontend to keep repository and collection names in sync:
// filepath: shared/GitHubConstants.ts
export const REPOS = [
{ owner: 'zoechbauer', repo: 'z-control-landing-page' },
{ owner: 'zoechbauer', repo: 'z-control-qr-code-generator' },
{ owner: 'zoechbauer', repo: 'z-control-Backup-scripts' },
{ owner: 'zoechbauer', repo: 'z-control-multi-language-translator' },
{ owner: 'zoechbauer', repo: 'copilot-learning-calculator' },
];
export const COLLECTION = {
GITHUB_ANALYTICS_TRAFFIC: "githubAnalyticsTraffic",
GITHUB_ANALYTICS_TRAFFIC_HISTORY: "githubAnalyticsTrafficHistory",
};see document github-analytics-architecture.md in the docs folder of z-control-backend-functions repository.
The landing page fetches analytics data from Firestore and displays it. Here is a simplified example of how to retrieve and log the data:
import { getFirestore, doc, getDoc } from "firebase/firestore";
import { REPOS, COLLECTION } from "../../shared/GitHubConstants";
const db = getFirestore();
const docRef = doc(db, COLLECTION.GITHUB_ANALYTICS_TRAFFIC_HISTORY, REPOS[0].repo);
async function fetchAnalytics() {
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
const data = docSnap.data();
// Display data in your app
console.log("Views:", data.views);
console.log("Clones:", data.clones);
} else {
console.log("No analytics data found.");
}
}Note:
In Firestore, theviewsandclonescollections may include entries with zero values. However, the frontend filters out these zero-value entries, displaying only dates where the values are greater than zero.
- Token Security: Store GitHub token only as a Cloud Function environment variable, never in source code.
- Error Handling: Log errors for each repo fetch; do not halt the entire function on a single failure.
- Data Retention:
githubAnalyticsTrafficstores only the latest analytics snapshot;githubAnalyticsTrafficHistoryaccumulates all daily entries for historical analysis.
For detailed instructions, refer to GITHUB_ANALYTICS_TEST_LOCALLY.md.