forked from housseindjirdeh/angular2-hn
-
Notifications
You must be signed in to change notification settings - Fork 8
Add usage metrics tracker feature #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iancmoritz
wants to merge
1
commit into
COG-GTM:master
Choose a base branch
from
COG-GTM-OPEN:feature/tracker-metrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import { Injectable } from '@angular/core'; | ||
|
|
||
| export interface PageView { | ||
| page: string; | ||
| timestamp: number; | ||
| duration?: number; | ||
| } | ||
|
|
||
| export interface StoryClick { | ||
| storyId: number; | ||
| title: string; | ||
| timestamp: number; | ||
| } | ||
|
|
||
| export interface UserView { | ||
| userId: string; | ||
| timestamp: number; | ||
| } | ||
|
|
||
| export interface TrackerData { | ||
| pageViews: PageView[]; | ||
| storyClicks: StoryClick[]; | ||
| userViews: UserView[]; | ||
| sessionCount: number; | ||
| firstVisit: number; | ||
| } | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class TrackerService { | ||
| private readonly STORAGE_KEY = 'hn_tracker_data'; | ||
| private currentPageStart: number | null = null; | ||
| private currentPage: string | null = null; | ||
|
|
||
| constructor() { | ||
| this.incrementSession(); | ||
| } | ||
|
|
||
| private getData(): TrackerData { | ||
| const stored = localStorage.getItem(this.STORAGE_KEY); | ||
| if (stored) { | ||
| return JSON.parse(stored); | ||
| } | ||
| return { | ||
| pageViews: [], | ||
| storyClicks: [], | ||
| userViews: [], | ||
| sessionCount: 0, | ||
| firstVisit: Date.now() | ||
| }; | ||
| } | ||
|
|
||
| private saveData(data: TrackerData): void { | ||
| localStorage.setItem(this.STORAGE_KEY, JSON.stringify(data)); | ||
| } | ||
|
|
||
| private incrementSession(): void { | ||
| const data = this.getData(); | ||
| data.sessionCount++; | ||
| this.saveData(data); | ||
| } | ||
|
|
||
| trackPageView(page: string): void { | ||
| this.endCurrentPageView(); | ||
| this.currentPage = page; | ||
| this.currentPageStart = Date.now(); | ||
|
|
||
| const data = this.getData(); | ||
| data.pageViews.push({ | ||
| page, | ||
| timestamp: this.currentPageStart | ||
| }); | ||
| this.saveData(data); | ||
| } | ||
|
|
||
| endCurrentPageView(): void { | ||
| if (this.currentPage && this.currentPageStart) { | ||
| const duration = Date.now() - this.currentPageStart; | ||
| const data = this.getData(); | ||
| const lastView = data.pageViews.find( | ||
| pv => pv.page === this.currentPage && pv.timestamp === this.currentPageStart | ||
| ); | ||
| if (lastView) { | ||
| lastView.duration = duration; | ||
| this.saveData(data); | ||
| } | ||
| } | ||
| this.currentPage = null; | ||
| this.currentPageStart = null; | ||
| } | ||
|
|
||
| trackStoryClick(storyId: number, title: string): void { | ||
| const data = this.getData(); | ||
| data.storyClicks.push({ | ||
| storyId, | ||
| title, | ||
| timestamp: Date.now() | ||
| }); | ||
| this.saveData(data); | ||
| } | ||
|
|
||
| trackUserView(userId: string): void { | ||
| const data = this.getData(); | ||
| data.userViews.push({ | ||
| userId, | ||
| timestamp: Date.now() | ||
| }); | ||
| this.saveData(data); | ||
| } | ||
|
|
||
| getMetrics() { | ||
| const data = this.getData(); | ||
|
|
||
| const pageViewsByType = this.aggregateByKey(data.pageViews, 'page'); | ||
| const storyClickCounts = this.aggregateStoryClicks(data.storyClicks); | ||
| const userViewCounts = this.aggregateByKey(data.userViews, 'userId'); | ||
| const activityByHour = this.aggregateByHour(data.pageViews); | ||
| const avgTimeByPage = this.calculateAvgTime(data.pageViews); | ||
|
|
||
| return { | ||
| totalPageViews: data.pageViews.length, | ||
| totalStoryClicks: data.storyClicks.length, | ||
| totalUserViews: data.userViews.length, | ||
| sessionCount: data.sessionCount, | ||
| firstVisit: data.firstVisit, | ||
| pageViewsByType: this.sortByCount(pageViewsByType), | ||
| topStories: storyClickCounts.slice(0, 10), | ||
| topUsers: this.sortByCount(userViewCounts).slice(0, 10), | ||
| activityByHour, | ||
| avgTimeByPage | ||
| }; | ||
| } | ||
|
|
||
| private aggregateByKey(items: any[], key: string): { name: string; count: number }[] { | ||
| const counts: Record<string, number> = {}; | ||
| items.forEach(item => { | ||
| const val = item[key]; | ||
| counts[val] = (counts[val] || 0) + 1; | ||
| }); | ||
| return Object.entries(counts).map(([name, count]) => ({ name, count })); | ||
| } | ||
|
|
||
| private aggregateStoryClicks(clicks: StoryClick[]): { storyId: number; title: string; count: number }[] { | ||
| const counts: Record<number, { title: string; count: number }> = {}; | ||
| clicks.forEach(click => { | ||
| if (!counts[click.storyId]) { | ||
| counts[click.storyId] = { title: click.title, count: 0 }; | ||
| } | ||
| counts[click.storyId].count++; | ||
| }); | ||
| return Object.entries(counts) | ||
| .map(([id, data]) => ({ storyId: +id, title: data.title, count: data.count })) | ||
| .sort((a, b) => b.count - a.count); | ||
| } | ||
|
|
||
| private aggregateByHour(pageViews: PageView[]): { hour: number; count: number }[] { | ||
| const counts: Record<number, number> = {}; | ||
| for (let i = 0; i < 24; i++) counts[i] = 0; | ||
| pageViews.forEach(pv => { | ||
| const hour = new Date(pv.timestamp).getHours(); | ||
| counts[hour]++; | ||
| }); | ||
| return Object.entries(counts).map(([hour, count]) => ({ hour: +hour, count })); | ||
| } | ||
|
|
||
| private calculateAvgTime(pageViews: PageView[]): { page: string; avgTime: number }[] { | ||
| const times: Record<string, number[]> = {}; | ||
| pageViews.forEach(pv => { | ||
| if (pv.duration) { | ||
| if (!times[pv.page]) times[pv.page] = []; | ||
| times[pv.page].push(pv.duration); | ||
| } | ||
| }); | ||
| return Object.entries(times).map(([page, durations]) => ({ | ||
| page, | ||
| avgTime: Math.round(durations.reduce((a, b) => a + b, 0) / durations.length / 1000) | ||
| })); | ||
| } | ||
|
|
||
| private sortByCount(items: { name: string; count: number }[]): { name: string; count: number }[] { | ||
| return items.sort((a, b) => b.count - a.count); | ||
| } | ||
|
|
||
| clearData(): void { | ||
| localStorage.removeItem(this.STORAGE_KEY); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <div class="tracker-container"> | ||
| <div class="tracker-header"> | ||
| <h1>Usage Metrics</h1> | ||
| <button class="clear-btn" (click)="clearData()">Clear Data</button> | ||
| </div> | ||
|
|
||
| <div class="metrics-grid" *ngIf="metrics"> | ||
| <div class="metric-card"> | ||
| <h3>Overview</h3> | ||
| <table> | ||
| <tr><td>Total Page Views</td><td>{{metrics.totalPageViews}}</td></tr> | ||
| <tr><td>Total Story Clicks</td><td>{{metrics.totalStoryClicks}}</td></tr> | ||
| <tr><td>Total User Profile Views</td><td>{{metrics.totalUserViews}}</td></tr> | ||
| <tr><td>Sessions</td><td>{{metrics.sessionCount}}</td></tr> | ||
| <tr><td>Tracking Since</td><td>{{formatDate(metrics.firstVisit)}}</td></tr> | ||
| </table> | ||
| </div> | ||
|
|
||
| <div class="metric-card"> | ||
| <h3>Page Views by Type</h3> | ||
| <table *ngIf="metrics.pageViewsByType.length"> | ||
| <tr *ngFor="let pv of metrics.pageViewsByType"> | ||
| <td>{{pv.name}}</td> | ||
| <td>{{pv.count}}</td> | ||
| </tr> | ||
| </table> | ||
| <p *ngIf="!metrics.pageViewsByType.length" class="no-data">No data yet</p> | ||
| </div> | ||
|
|
||
| <div class="metric-card"> | ||
| <h3>Average Time per Page (seconds)</h3> | ||
| <table *ngIf="metrics.avgTimeByPage.length"> | ||
| <tr *ngFor="let avg of metrics.avgTimeByPage"> | ||
| <td>{{avg.page}}</td> | ||
| <td>{{avg.avgTime}}s</td> | ||
| </tr> | ||
| </table> | ||
| <p *ngIf="!metrics.avgTimeByPage.length" class="no-data">No data yet</p> | ||
| </div> | ||
|
|
||
| <div class="metric-card"> | ||
| <h3>Top 10 Clicked Stories</h3> | ||
| <table *ngIf="metrics.topStories.length"> | ||
| <tr *ngFor="let story of metrics.topStories"> | ||
| <td class="story-title">{{story.title}}</td> | ||
| <td>{{story.count}}</td> | ||
| </tr> | ||
| </table> | ||
| <p *ngIf="!metrics.topStories.length" class="no-data">No data yet</p> | ||
| </div> | ||
|
|
||
| <div class="metric-card"> | ||
| <h3>Top 10 Viewed Users</h3> | ||
| <table *ngIf="metrics.topUsers.length"> | ||
| <tr *ngFor="let user of metrics.topUsers"> | ||
| <td>{{user.name}}</td> | ||
| <td>{{user.count}}</td> | ||
| </tr> | ||
| </table> | ||
| <p *ngIf="!metrics.topUsers.length" class="no-data">No data yet</p> | ||
| </div> | ||
|
|
||
| <div class="metric-card wide"> | ||
| <h3>Activity by Hour</h3> | ||
| <div class="hour-chart"> | ||
| <div class="hour-bar" *ngFor="let h of metrics.activityByHour" | ||
| [style.height.%]="metrics.totalPageViews ? (h.count / metrics.totalPageViews * 100) : 0"> | ||
| <span class="hour-label">{{formatHour(h.hour)}}</span> | ||
| <span class="hour-count">{{h.count}}</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.