Skip to content

Commit

Permalink
feat: Count DNT and noscript visits
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Jun 28, 2021
1 parent a2f2245 commit 87933e6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {
BrowserEvent,
SessionData,
GenericEvent,
isSessionStartEvent,
isGenericStringEvent,
isBrowserEvent,
isGenericNumberEvent,
isGenericNumbersEvent,
isGenericStringEvent,
isGenericStringsEvent,
isBrowserEvent,
isSessionEndEvent
isSessionDNTEvent,
isSessionEndEvent,
isSessionNoScriptEvent,
isSessionStartEvent,
SessionData
} from '@chiffre/analytics-core'
import Bowser from 'bowser'

Expand Down Expand Up @@ -61,6 +63,8 @@ export interface ReturningVisitorInfo<E> {

export class BrowserEventsProcessor {
private _sessionMap: Map<string, BrowserEvent[]>
dnt: number
noscript: number
browsers: CounterMap
pageCount: CounterMap
referrers: CounterMap
Expand All @@ -80,6 +84,8 @@ export class BrowserEventsProcessor {
this.lang = new CounterMap()
this.viewportWidth = new CounterMap<number>()
this.browsers = new CounterMap()
this.dnt = 0
this.noscript = 0
}

public process(event: BrowserEvent) {
Expand All @@ -104,6 +110,10 @@ export class BrowserEventsProcessor {
this.lang.count(event.data.lang)
this.viewportWidth.count(event.data.vp.w)
this.browsers.count(ua.browser.name || 'N.A.')
} else if (isSessionDNTEvent(event)) {
this.dnt += 1
} else if (isSessionNoScriptEvent(event)) {
this.noscript += 1
}
}

Expand All @@ -119,16 +129,16 @@ export class BrowserEventsProcessor {
return [sid, 0]
}
// For events sorted by time:
const min = events[0].time
const max = events[events.length - 1].time
// const min = events[0].time
// const max = events[events.length - 1].time
// For unsorted events:
// const { min, max } = events.reduce(
// ({ min, max }, event) => ({
// max: Math.max(max, event.time),
// min: Math.min(min, event.time)
// }),
// { min: Infinity, max: 0 }
// )
const { min, max } = events.reduce(
({ min, max }, event) => ({
max: Math.max(max, event.time),
min: Math.min(min, event.time)
}),
{ min: Infinity, max: 0 }
)
return [sid, max - min]
})
.sort((a, b) => a[1] - b[1])
Expand Down
44 changes: 44 additions & 0 deletions src/leaderboards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export interface LeaderboardEntry<K = string> {
key: K
score: number
percent: number
}

// --

export class CounterMap<K = string> {
private _map: Map<K, number>

constructor() {
this._map = new Map()
}

public count(key: K) {
this._map.set(key, (this._map.get(key) || 0) + 1)
}

public get leaderboard(): LeaderboardEntry<K>[] {
const sum = Array.from(this._map.values()).reduce((s, c) => s + c, 0)
return Array.from(this._map.entries())
.map(([key, count]) => ({
key,
score: count,
percent: (100 * count) / sum
}))
.sort((a, b) => b.score - a.score)
}
}

export function groupLeaderboard<K, GroupKeys extends string>(
leaderboard: LeaderboardEntry<K>[],
findGroupKey: (key: K) => GroupKeys
) {
type Groups = Map<GroupKeys, LeaderboardEntry<K>[]>
const groups: Groups = new Map()
leaderboard.forEach(entry => {
const groupKey = findGroupKey(entry.key)
const existing = groups.get(groupKey) ?? []
groups.set(groupKey, [...existing, entry])
})
return groups
}

0 comments on commit 87933e6

Please sign in to comment.