Skip to content

Commit

Permalink
feat: Update types and hookup accumulator
Browse files Browse the repository at this point in the history
  • Loading branch information
whizzzkid committed Jan 19, 2023
1 parent d07079d commit ccece9b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/MetricsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { COUNTLY_SETUP_DEFAULTS } from './config.js'
import { EventAccumulator } from './EventAccumulator.js'

import type { metricFeatures, CountlyWebSdk } from 'countly-sdk-web'
import type { CountlyNodeSdk } from 'countly-sdk-nodejs'
import type { CountlyWebSdk, metricFeatures } from 'countly-sdk-web'
import type { consentTypes, consentTypesExceptAll } from './types/index.js'

export interface MetricsProviderConstructorOptions<T> {
appKey: string
autoTrack?: boolean
interval?: number
max_events?: number
metricsService: T
queue_size?: number
session_update?: number
url?: string
metricsService: T
}

export default class MetricsProvider<T extends CountlyWebSdk & CountlyNodeSdk> {
public readonly accumulate: EventAccumulator
private readonly groupedFeatures: Record<consentTypes, metricFeatures[]> = this.mapAllEvents({
minimal: ['sessions', 'views', 'events'],
performance: ['crashes', 'apm'],
Expand All @@ -40,6 +42,7 @@ export default class MetricsProvider<T extends CountlyWebSdk & CountlyNodeSdk> {
url,
require_consent: true
})
this.accumulate = new EventAccumulator(metricsService)

this.metricsService.init(serviceConfig)
this.metricsService.group_features(this.groupedFeatures)
Expand Down
13 changes: 10 additions & 3 deletions src/types/countly.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
declare module 'countly-sdk-web' {
interface CountlyEventData {
export interface CountlyEventData {
key: string
count: number
sum: number
segmentation: Record<string, string | number>
dur: number
segmentation: Segments
}
interface CountlyEvent {
export interface CountlyEvent {
// name or id of the event
key: string
// how many times did event occur
Expand All @@ -18,12 +19,18 @@ declare module 'countly-sdk-web' {
segmentation?: Segments
}

export interface IEventAccumulator {
addEvent: (event: CountlyEvent, flush: boolean) => void
flush: (key: string) => void
}

export type metricFeatures = 'apm' | 'attribution' | 'clicks' | 'crashes' | 'events' | 'feedback' | 'forms' |
'location' | 'scrolls' | 'sessions' | 'star-rating' | 'users' | 'views'
type Segments = Record<string, string>
type IgnoreList = Array<string | RegExp>
type CountlyEventQueueItem = [string, CountlyEventData] | [eventName: string, key: string] | [eventName: string]
export interface CountlyWebSdk {
accumulate: IEventAccumulator
group_features: (arg0: Record<import('./index.js').consentTypes, metricFeatures[]>) => unknown
check_consent: (consentFeature: metricFeatures | import('./index.js').consentTypes) => boolean
add_consent: (consentFeature: import('./index.js').consentTypes | Array<import('./index.js').consentTypes>) => void
Expand Down

0 comments on commit ccece9b

Please sign in to comment.