|
1
|
import { |
|
2
|
TrackAnalyticsEventDTO, |
|
3
|
IdentifyAnalyticsEventDTO, |
|
4
|
} from "@medusajs/types" |
|
5
|
import AnalyticsProviderService from "./provider-service" |
|
6
|
import { MedusaError } from "@medusajs/framework/utils" |
|
7
|
|
|
8
|
type InjectedDependencies = { |
|
9
|
analyticsProviderService: AnalyticsProviderService |
|
10
|
} |
|
11
|
|
|
12
|
export default class AnalyticsService { |
|
13
|
protected readonly analyticsProviderService_: AnalyticsProviderService |
|
14
|
|
|
15
|
constructor({ analyticsProviderService }: InjectedDependencies) { |
|
16
|
this.analyticsProviderService_ = analyticsProviderService |
|
17
|
} |
|
18
|
|
|
19
|
__hooks = { |
|
20
|
onApplicationShutdown: async () => { |
|
21
|
await this.analyticsProviderService_.shutdown() |
|
22
|
}, |
|
23
|
} |
|
24
|
|
|
25
|
getProvider() { |
|
26
|
return this.analyticsProviderService_ |
|
27
|
} |
|
28
|
|
|
29
|
async track(data: TrackAnalyticsEventDTO): Promise<void> { |
|
30
|
try { |
|
31
|
await this.analyticsProviderService_.track(data) |
|
32
|
} catch (error) { |
|
33
|
throw new MedusaError( |
|
34
|
MedusaError.Types.UNEXPECTED_STATE, |
|
35
|
`Error tracking event for ${data.event}: ${error.message}` |
|
36
|
) |
|
37
|
} |
|
38
|
} |
|
39
|
|
|
40
|
async identify(data: IdentifyAnalyticsEventDTO): Promise<void> { |
|
41
|
try { |
|
42
|
await this.analyticsProviderService_.identify(data) |
|
43
|
} catch (error) { |
|
44
|
throw new MedusaError( |
|
45
|
MedusaError.Types.UNEXPECTED_STATE, |
|
46
|
`Error identifying event for ${ |
|
47
|
"group" in data ? data.group.id : data.actor_id |
|
48
|
}: ${error.message}` |
|
49
|
) |
|
50
|
} |
|
51
|
} |
|
52
|
} |