1 import {
2 IAnalyticsProvider,
3 ProviderIdentifyAnalyticsEventDTO,
4 ProviderTrackAnalyticsEventDTO,
5 } from "@medusajs/types"
6
7 /**
8 * ### constructor
9 *
10 * The constructor allows you to access resources from the module's container using the first parameter,
11 * and the module's options using the second parameter.
12 *
13 * If you're creating a client or establishing a connection with a third-party service, do it in the constructor.
14 *
15 * #### Example
16 *
17 * ```ts
18 * import { Logger } from "@medusajs/framework/types"
19 * import { AbstractAnalyticsProviderService } from "@medusajs/framework/utils"
20 *
21 * type InjectedDependencies = {
22 * logger: Logger
23 * }
24 *
25 * type Options = {
26 * apiKey: string
27 * }
28 *
29 * class MyAnalyticsProviderService extends AbstractAnalyticsProviderService {
30 * protected logger_: Logger
31 * protected options_: Options
32 * static identifier = "my-analytics"
33 * // assuming you're initializing a client
34 * protected client
35 *
36 * constructor (
37 * { logger }: InjectedDependencies,
38 * options: Options
39 * ) {
40 * super()
41 *
42 * this.logger_ = logger
43 * this.options_ = options
44 *
45 * // assuming you're initializing a client
46 * this.client = new Client(options)
47 * }
48 * }
49 *
50 * export default MyAnalyticsProviderService
51 * ```
52 */
53 export class AbstractAnalyticsProviderService implements IAnalyticsProvider {
54 /**
55 * Each analytics provider has a unique ID used to identify it. The provider's ID
56 * will be stored as `aly_{identifier}_{id}`, where `{id}` is the provider's `id`
57 * property in the `medusa-config.ts`.
58 *
59 * @example
60 * class MyAnalyticsProviderService extends AbstractAnalyticsProviderService {
61 * static identifier = "my-analytics"
62 * // ...
63 * }
64 */
65 static identifier: string
66
67 /**
68 * @ignore
69 */
70 getIdentifier() {
71 return (this.constructor as any).identifier
72 }
73
74 /**
75 * This method tracks an event with the third-party analytics provider. The Analytics Module
76 * will use this method in its `track` method if your provider is configured in `medusa-config.ts`.
77 *
78 * You can send the event to the third-party provider based on its semantics.
79 *
80 * @param {ProviderTrackAnalyticsEventDTO} data - The event's details.
81 * @returns {Promise<void>} Resolves when the event is tracked successfully.
82 *
83 * @example
84 * class MyAnalyticsProviderService extends AbstractAnalyticsProviderService {
85 * // ...
86 * async track(
87 * data: ProviderTrackAnalyticsEventDTO
88 * ): Promise<void> {
89 * // track event to third-party provider
90 * // or using custom logic
91 * // for example:
92 * this.client.track(data)
93 * }
94 * }
95 */
96 async track(data: ProviderTrackAnalyticsEventDTO): Promise<void> {
97 throw Error("track must be overridden by the child class")
98 }
99
100 /**
101 * This method identifies an actor or group in the third-party analytics provider. The Analytics Module
102 * will use this method in its `identify` method if your provider is configured in `medusa-config.ts`.
103 *
104 * @param {ProviderIdentifyAnalyticsEventDTO} data - The details of the actor or group.
105 * @returns {Promise<void>} Resolves when the actor or group is identified successfully.
106 *
107 * @example
108 * class MyAnalyticsProviderService extends AbstractAnalyticsProviderService {
109 * // ...
110 * async identify(
111 * data: ProviderIdentifyAnalyticsEventDTO
112 * ): Promise<void> {
113 * // identify actor or group in the analytics provider
114 * // or using custom logic
115 * // for example:
116 * this.client.identify(data)
117 * }
118 * }
119 */
120 async identify(data: ProviderIdentifyAnalyticsEventDTO): Promise<void> {
121 throw Error("identify must be overridden by the child class")
122 }
123 }