Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
tsRepo: true,
build: {
types: true,
bundle: false,
config: {
format: 'esm',
external: ['electron', '#ansi-styles', 'yargs/yargs', '#supports-color'],
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
"import": "./dist/index.min.js"
},
"./vanilla": {
"types": "./dist/src/MetricsProvider.d.ts",
"import": "./dist/src/MetricsProvider.js"
"browser": "./dist/src/BrowserMetrics.js",
"import": "./dist/src/NodeMetrics.js"
},
"./*": {
"types": "./dist/src/*.d.ts",
"import": "./dist/src/*.js"
}
},
"eslintConfig": {
Expand Down Expand Up @@ -139,6 +143,7 @@
"build-storybook": "build-storybook"
},
"dependencies": {
"countly-sdk-nodejs": "^22.6.0",
"countly-sdk-web": "^22.6.4",
"esbuild-css-modules-plugin": "^2.7.0",
"react": "^18.2.0",
Expand Down
9 changes: 9 additions & 0 deletions src/BrowserMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Countly from 'countly-sdk-web'

import MetricsProvider, { MetricsProviderConstructorOptions } from './MetricsProvider.js'

export default class BrowserMetricsProvider extends MetricsProvider<typeof Countly> {
constructor (args: Omit<MetricsProviderConstructorOptions<typeof Countly>, 'metricsService'>) {
super({ ...args, metricsService: Countly })
}
}
18 changes: 9 additions & 9 deletions src/MetricsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { COUNTLY_API_URL } from './config.js'
import type { consentTypes, consentTypesExceptAll, metricFeatures } from 'countly-sdk-web'
import Countly from 'countly-sdk-web'
import type { metricFeatures, CountlyWebSdk } from 'countly-sdk-web'
import type { CountlyNodeSdk } from 'countly-sdk-nodejs'
import type { consentTypes, consentTypesExceptAll } from './types/index.js'

interface MetricsProviderConstructorOptions {
export interface MetricsProviderConstructorOptions<T> {
appKey: string
url?: string
autoTrack?: boolean
metricsService: T
}

export default class MetricsProvider {
export default class MetricsProvider<T extends CountlyWebSdk | CountlyNodeSdk> {
private readonly groupedFeatures: Record<consentTypes, metricFeatures[]> = this.mapAllEvents({
minimal: ['sessions', 'views', 'events'],
performance: ['crashes', 'apm'],
Expand All @@ -19,8 +21,10 @@ export default class MetricsProvider {

private sessionStarted: boolean = false
private readonly _consentGranted: Set<consentTypes> = new Set()
private readonly metricsService: T

constructor ({ autoTrack = true, url = COUNTLY_API_URL, appKey }: MetricsProviderConstructorOptions) {
constructor ({ autoTrack = true, url = COUNTLY_API_URL, appKey, metricsService }: MetricsProviderConstructorOptions<T>) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opt: long lines can be split

Suggested change
constructor ({ autoTrack = true, url = COUNTLY_API_URL, appKey, metricsService }: MetricsProviderConstructorOptions<T>) {
constructor ({
autoTrack = true,
url = COUNTLY_API_URL,
appKey,
metricsService
}: MetricsProviderConstructorOptions<T>) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually the lint will fix this when it needs breaking out. We shouldn't need to worry about style things like this since our build does it (should do it?) for us.

And if we want it to be broken up earlier, we should update rules so that it happens automatically.

this.metricsService = metricsService
this.metricsService.init({
app_key: appKey,
url,
Expand All @@ -41,10 +45,6 @@ export default class MetricsProvider {
}
}

get metricsService (): typeof Countly {
return Countly
}

get consentGranted (): consentTypes[] {
return [...this._consentGranted]
}
Expand Down
9 changes: 9 additions & 0 deletions src/NodeMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Countly from 'countly-sdk-nodejs'

import MetricsProvider, { MetricsProviderConstructorOptions } from './MetricsProvider.js'

export default class NodeMetricsProvider extends MetricsProvider<typeof Countly> {
constructor (args: Omit<MetricsProviderConstructorOptions<typeof Countly>, 'metricsService'>) {
super({ ...args, metricsService: Countly })
}
}
6 changes: 2 additions & 4 deletions src/types/countly.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ declare module 'countly-sdk-web' {

export type metricFeatures = 'apm' | 'attribution' | 'clicks' | 'crashes' | 'events' | 'feedback' | 'forms' |
'location' | 'scrolls' | 'sessions' | 'star-rating' | 'users' | 'views'
export type consentTypesExceptAll = 'minimal' | 'performance' | 'ux' | 'feedback' | 'location'
export type consentTypes = 'all' | consentTypesExceptAll
type Segments = Record<string, string>
type IgnoreList = Array<string | RegExp>
type CountlyEventQueueItem = [string, CountlyEventData] | [eventName: string, key: string] | [eventName: string]
interface CountlyWebSdk {
export interface CountlyWebSdk {
group_features: (arg0: Record<consentTypes, metricFeatures[]>) => unknown
check_consent: (consentFeature: metricFeatures | consentTypes) => boolean
add_consent: (consentFeature: consentTypes | consentTypes[]) => void
Expand Down Expand Up @@ -119,5 +117,5 @@ declare module 'countly-sdk-web' {
end_session: (sec, force) => void
}
const Countly: CountlyWebSdk
export = Countly
export default Countly
}
6 changes: 6 additions & 0 deletions src/types/countlyNode.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module 'countly-sdk-nodejs' {
export type CountlyNodeSdk = import('countly-sdk-web').CountlyWebSdk
const Countly: CountlyNodeSdk
export type metricFeatures = import('countly-sdk-web').metricFeatures
export default Countly
}
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export type consentTypesExceptAll = 'minimal' | 'performance' | 'ux' | 'feedback' | 'location'
export type consentTypes = 'all' | consentTypesExceptAll