@flare-engine/analytics 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +26 -4
- package/dist/index.d.mts +63 -17
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +63 -17
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +116 -49
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +111 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -9
package/LICENSE
CHANGED
|
@@ -1,21 +1,201 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Grzegorz Otto
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @flare-engine/analytics
|
|
2
2
|
|
|
3
|
-
Typed `AnalyticsProvider` interface + `NoopAnalyticsProvider` + React context
|
|
3
|
+
Typed `AnalyticsProvider` interface + `NoopAnalyticsProvider` + React context, plus `createMemoryAnalytics` / `createConsoleAnalytics` — local providers for tests and dev. Concrete platform adapters (PostHog, Amplitude, Segment, Firebase) are post-launch; wire your own at the app boundary until then.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -26,8 +26,30 @@ export function App() {
|
|
|
26
26
|
}
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
`useAnalytics()` returns the provider
|
|
29
|
+
`useAnalytics()` returns the active provider. The `AnalyticsProvider` surface is `track(event, props?)`, `identify(userId, traits?)`, `screen(name, props?)`, and `flush()` — all async. Swapping `NoopAnalyticsProvider` for another provider requires no consumer changes.
|
|
30
30
|
|
|
31
|
-
##
|
|
31
|
+
## Local providers
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
`createMemoryAnalytics(opts?)` captures events into a bounded ring buffer you can inspect — ideal for asserting behavior in tests:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { createMemoryAnalytics } from '@flare-engine/analytics';
|
|
37
|
+
|
|
38
|
+
const analytics = createMemoryAnalytics({ capacity: 100 });
|
|
39
|
+
await analytics.track('level_start', { zone: 1 });
|
|
40
|
+
await analytics.screen('hud');
|
|
41
|
+
analytics.getEvents();
|
|
42
|
+
// [{ type: 'track', name: 'level_start', props: { zone: 1 }, timestamp },
|
|
43
|
+
// { type: 'screen', name: 'hud', timestamp }]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- Each call records the matching `AnalyticsEvent` variant. When the buffer is full (`capacity`, default `256`, clamped to `>= 1`) the oldest event is dropped.
|
|
47
|
+
- `getEvents()` returns a defensive copy, oldest → newest; `clear()` empties it. Timestamps come from the injected `now` (default `Date.now`). `flush()` is a no-op.
|
|
48
|
+
|
|
49
|
+
`createConsoleAnalytics(opts?)` logs each event to an injected sink (default `console.log`) as `log('[analytics]', event)` — a zero-setup provider for local runs.
|
|
50
|
+
|
|
51
|
+
`AnalyticsEvent` is a discriminated union on `type` (`'track'` / `'identify'` / `'screen'`) — the event shape both local providers record.
|
|
52
|
+
|
|
53
|
+
## Env-gated fallback
|
|
54
|
+
|
|
55
|
+
In CI or dev builds without platform credentials, select `NoopAnalyticsProvider` (or a local provider) behind your own env flag so nothing touches a native SDK until real credentials are present.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/provider.d.ts
|
|
3
2
|
/** Free-form event properties — adapters serialize as needed. */
|
|
4
3
|
type AnalyticsProps = Record<string, unknown>;
|
|
5
|
-
/** A
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
4
|
+
/** A captured analytics event — the discriminated union the memory/console providers record. */
|
|
5
|
+
type AnalyticsEvent = {
|
|
6
|
+
readonly type: 'track';
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly props?: AnalyticsProps;
|
|
9
|
+
readonly timestamp: number;
|
|
10
|
+
} | {
|
|
11
|
+
readonly type: 'identify';
|
|
12
|
+
readonly userId: string;
|
|
13
|
+
readonly traits?: AnalyticsProps;
|
|
14
|
+
readonly timestamp: number;
|
|
15
|
+
} | {
|
|
16
|
+
readonly type: 'screen';
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly props?: AnalyticsProps;
|
|
19
|
+
readonly timestamp: number;
|
|
20
|
+
};
|
|
12
21
|
/**
|
|
13
22
|
* Analytics provider interface. Consumers supply a concrete adapter (PostHog,
|
|
14
23
|
* Amplitude, Segment, Firebase Analytics) at the app boundary. Batching,
|
|
@@ -16,20 +25,57 @@ interface AnalyticsEvent {
|
|
|
16
25
|
* deliberately thin so any SDK can back it.
|
|
17
26
|
*/
|
|
18
27
|
interface AnalyticsProvider {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
track(event: string, props?: AnalyticsProps): Promise<void>;
|
|
29
|
+
identify(userId: string, traits?: AnalyticsProps): Promise<void>;
|
|
30
|
+
screen(name: string, props?: AnalyticsProps): Promise<void>;
|
|
31
|
+
flush(): Promise<void>;
|
|
23
32
|
}
|
|
24
33
|
/** Default no-op provider. Resolves immediately; retains nothing. */
|
|
25
34
|
declare const NoopAnalyticsProvider: AnalyticsProvider;
|
|
26
|
-
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/context.d.ts
|
|
27
37
|
/**
|
|
28
38
|
* React context carrying the active `AnalyticsProvider`. Defaults to
|
|
29
39
|
* `NoopAnalyticsProvider`.
|
|
30
40
|
*/
|
|
31
|
-
declare const AnalyticsContext: react.Context<AnalyticsProvider>;
|
|
41
|
+
declare const AnalyticsContext: import("react").Context<AnalyticsProvider>;
|
|
32
42
|
/** Hook returning the current `AnalyticsProvider`. */
|
|
33
43
|
declare function useAnalytics(): AnalyticsProvider;
|
|
34
|
-
|
|
35
|
-
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/memory.d.ts
|
|
46
|
+
/** Options for `createMemoryAnalytics`. */
|
|
47
|
+
interface MemoryAnalyticsOptions {
|
|
48
|
+
/** Ring-buffer capacity; clamped to >= 1. Defaults to 256. */
|
|
49
|
+
capacity?: number;
|
|
50
|
+
/** Timestamp source in unix ms. Defaults to `() => Date.now()`. */
|
|
51
|
+
now?: () => number;
|
|
52
|
+
}
|
|
53
|
+
/** In-memory `AnalyticsProvider` that records events into a bounded ring buffer. */
|
|
54
|
+
interface MemoryAnalytics extends AnalyticsProvider {
|
|
55
|
+
/** Recorded events, oldest -> newest, as a defensive copy. */
|
|
56
|
+
getEvents(): readonly AnalyticsEvent[];
|
|
57
|
+
/** Drop all recorded events. */
|
|
58
|
+
clear(): void;
|
|
59
|
+
}
|
|
60
|
+
/** Options for `createConsoleAnalytics`. */
|
|
61
|
+
interface ConsoleAnalyticsOptions {
|
|
62
|
+
/** Sink for captured events. Defaults to `console.log`. */
|
|
63
|
+
log?: (...args: unknown[]) => void;
|
|
64
|
+
/** Timestamp source in unix ms. Defaults to `() => Date.now()`. */
|
|
65
|
+
now?: () => number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create an in-memory analytics provider backed by a bounded ring buffer.
|
|
69
|
+
* Useful for tests and dev overlays. When the buffer is full the oldest
|
|
70
|
+
* event is dropped. `flush` is a no-op.
|
|
71
|
+
*/
|
|
72
|
+
declare function createMemoryAnalytics(opts?: MemoryAnalyticsOptions): MemoryAnalytics;
|
|
73
|
+
/**
|
|
74
|
+
* Create an analytics provider that logs each captured event to an injected
|
|
75
|
+
* sink (default `console.log`) as `log('[analytics]', event)`. `flush` is a
|
|
76
|
+
* no-op.
|
|
77
|
+
*/
|
|
78
|
+
declare function createConsoleAnalytics(opts?: ConsoleAnalyticsOptions): AnalyticsProvider;
|
|
79
|
+
//#endregion
|
|
80
|
+
export { AnalyticsContext, type AnalyticsEvent, type AnalyticsProps, type AnalyticsProvider, type ConsoleAnalyticsOptions, type MemoryAnalytics, type MemoryAnalyticsOptions, NoopAnalyticsProvider, createConsoleAnalytics, createMemoryAnalytics, useAnalytics };
|
|
81
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/memory.ts"],"mappings":";;KACY,iBAAiB;;KAGjB;WAEG;WACA;WACA,QAAQ;WACR;;WAGA;WACA;WACA,SAAS;WACT;;WAGA;WACA;WACA,QAAQ;WACR;;;;;;;;UASE;EACf,MAAM,eAAe,QAAQ,iBAAiB;EAC9C,SAAS,gBAAgB,SAAS,iBAAiB;EACnD,OAAO,cAAc,QAAQ,iBAAiB;EAC9C,SAAS;;;cAIE,uBAAuB;;;;;;;cC/BvB,kCAAgB,QAAA;;iBAGb,gBAAgB;;;;UCPf;;EAEf;;EAEA;;;UAIe,wBAAwB;;EAEvC,sBAAsB;;EAEtB;;;UAIe;;EAEf,UAAU;;EAEV;;;;;;;iBA0Bc,sBAAsB,OAAO,yBAAyB;;;;;;iBAqCtD,uBAAuB,OAAO,0BAA0B"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/provider.d.ts
|
|
3
2
|
/** Free-form event properties — adapters serialize as needed. */
|
|
4
3
|
type AnalyticsProps = Record<string, unknown>;
|
|
5
|
-
/** A
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
4
|
+
/** A captured analytics event — the discriminated union the memory/console providers record. */
|
|
5
|
+
type AnalyticsEvent = {
|
|
6
|
+
readonly type: 'track';
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly props?: AnalyticsProps;
|
|
9
|
+
readonly timestamp: number;
|
|
10
|
+
} | {
|
|
11
|
+
readonly type: 'identify';
|
|
12
|
+
readonly userId: string;
|
|
13
|
+
readonly traits?: AnalyticsProps;
|
|
14
|
+
readonly timestamp: number;
|
|
15
|
+
} | {
|
|
16
|
+
readonly type: 'screen';
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly props?: AnalyticsProps;
|
|
19
|
+
readonly timestamp: number;
|
|
20
|
+
};
|
|
12
21
|
/**
|
|
13
22
|
* Analytics provider interface. Consumers supply a concrete adapter (PostHog,
|
|
14
23
|
* Amplitude, Segment, Firebase Analytics) at the app boundary. Batching,
|
|
@@ -16,20 +25,57 @@ interface AnalyticsEvent {
|
|
|
16
25
|
* deliberately thin so any SDK can back it.
|
|
17
26
|
*/
|
|
18
27
|
interface AnalyticsProvider {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
track(event: string, props?: AnalyticsProps): Promise<void>;
|
|
29
|
+
identify(userId: string, traits?: AnalyticsProps): Promise<void>;
|
|
30
|
+
screen(name: string, props?: AnalyticsProps): Promise<void>;
|
|
31
|
+
flush(): Promise<void>;
|
|
23
32
|
}
|
|
24
33
|
/** Default no-op provider. Resolves immediately; retains nothing. */
|
|
25
34
|
declare const NoopAnalyticsProvider: AnalyticsProvider;
|
|
26
|
-
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/context.d.ts
|
|
27
37
|
/**
|
|
28
38
|
* React context carrying the active `AnalyticsProvider`. Defaults to
|
|
29
39
|
* `NoopAnalyticsProvider`.
|
|
30
40
|
*/
|
|
31
|
-
declare const AnalyticsContext: react.Context<AnalyticsProvider>;
|
|
41
|
+
declare const AnalyticsContext: import("react").Context<AnalyticsProvider>;
|
|
32
42
|
/** Hook returning the current `AnalyticsProvider`. */
|
|
33
43
|
declare function useAnalytics(): AnalyticsProvider;
|
|
34
|
-
|
|
35
|
-
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/memory.d.ts
|
|
46
|
+
/** Options for `createMemoryAnalytics`. */
|
|
47
|
+
interface MemoryAnalyticsOptions {
|
|
48
|
+
/** Ring-buffer capacity; clamped to >= 1. Defaults to 256. */
|
|
49
|
+
capacity?: number;
|
|
50
|
+
/** Timestamp source in unix ms. Defaults to `() => Date.now()`. */
|
|
51
|
+
now?: () => number;
|
|
52
|
+
}
|
|
53
|
+
/** In-memory `AnalyticsProvider` that records events into a bounded ring buffer. */
|
|
54
|
+
interface MemoryAnalytics extends AnalyticsProvider {
|
|
55
|
+
/** Recorded events, oldest -> newest, as a defensive copy. */
|
|
56
|
+
getEvents(): readonly AnalyticsEvent[];
|
|
57
|
+
/** Drop all recorded events. */
|
|
58
|
+
clear(): void;
|
|
59
|
+
}
|
|
60
|
+
/** Options for `createConsoleAnalytics`. */
|
|
61
|
+
interface ConsoleAnalyticsOptions {
|
|
62
|
+
/** Sink for captured events. Defaults to `console.log`. */
|
|
63
|
+
log?: (...args: unknown[]) => void;
|
|
64
|
+
/** Timestamp source in unix ms. Defaults to `() => Date.now()`. */
|
|
65
|
+
now?: () => number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create an in-memory analytics provider backed by a bounded ring buffer.
|
|
69
|
+
* Useful for tests and dev overlays. When the buffer is full the oldest
|
|
70
|
+
* event is dropped. `flush` is a no-op.
|
|
71
|
+
*/
|
|
72
|
+
declare function createMemoryAnalytics(opts?: MemoryAnalyticsOptions): MemoryAnalytics;
|
|
73
|
+
/**
|
|
74
|
+
* Create an analytics provider that logs each captured event to an injected
|
|
75
|
+
* sink (default `console.log`) as `log('[analytics]', event)`. `flush` is a
|
|
76
|
+
* no-op.
|
|
77
|
+
*/
|
|
78
|
+
declare function createConsoleAnalytics(opts?: ConsoleAnalyticsOptions): AnalyticsProvider;
|
|
79
|
+
//#endregion
|
|
80
|
+
export { AnalyticsContext, type AnalyticsEvent, type AnalyticsProps, type AnalyticsProvider, type ConsoleAnalyticsOptions, type MemoryAnalytics, type MemoryAnalyticsOptions, NoopAnalyticsProvider, createConsoleAnalytics, createMemoryAnalytics, useAnalytics };
|
|
81
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/memory.ts"],"mappings":";;KACY,iBAAiB;;KAGjB;WAEG;WACA;WACA,QAAQ;WACR;;WAGA;WACA;WACA,SAAS;WACT;;WAGA;WACA;WACA,QAAQ;WACR;;;;;;;;UASE;EACf,MAAM,eAAe,QAAQ,iBAAiB;EAC9C,SAAS,gBAAgB,SAAS,iBAAiB;EACnD,OAAO,cAAc,QAAQ,iBAAiB;EAC9C,SAAS;;;cAIE,uBAAuB;;;;;;;cC/BvB,kCAAgB,QAAA;;iBAGb,gBAAgB;;;;UCPf;;EAEf;;EAEA;;;UAIe,wBAAwB;;EAEvC,sBAAsB;;EAEtB;;;UAIe;;EAEf,UAAU;;EAEV;;;;;;;iBA0Bc,sBAAsB,OAAO,yBAAyB;;;;;;iBAqCtD,uBAAuB,OAAO,0BAA0B"}
|
package/dist/index.js
CHANGED
|
@@ -1,53 +1,120 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/provider.ts
|
|
4
|
+
/** Default no-op provider. Resolves immediately; retains nothing. */
|
|
5
|
+
const NoopAnalyticsProvider = {
|
|
6
|
+
async track() {},
|
|
7
|
+
async identify() {},
|
|
8
|
+
async screen() {},
|
|
9
|
+
async flush() {}
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
AnalyticsContext: () => AnalyticsContext,
|
|
24
|
-
NoopAnalyticsProvider: () => NoopAnalyticsProvider,
|
|
25
|
-
useAnalytics: () => useAnalytics
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(index_exports);
|
|
28
|
-
|
|
29
|
-
// src/provider.ts
|
|
30
|
-
var NoopAnalyticsProvider = {
|
|
31
|
-
async track() {
|
|
32
|
-
},
|
|
33
|
-
async identify() {
|
|
34
|
-
},
|
|
35
|
-
async screen() {
|
|
36
|
-
},
|
|
37
|
-
async flush() {
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
// src/context.ts
|
|
42
|
-
var import_react = require("react");
|
|
43
|
-
var AnalyticsContext = (0, import_react.createContext)(NoopAnalyticsProvider);
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/context.ts
|
|
13
|
+
/**
|
|
14
|
+
* React context carrying the active `AnalyticsProvider`. Defaults to
|
|
15
|
+
* `NoopAnalyticsProvider`.
|
|
16
|
+
*/
|
|
17
|
+
const AnalyticsContext = (0, react.createContext)(NoopAnalyticsProvider);
|
|
18
|
+
/** Hook returning the current `AnalyticsProvider`. */
|
|
44
19
|
function useAnalytics() {
|
|
45
|
-
|
|
20
|
+
return (0, react.useContext)(AnalyticsContext);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/memory.ts
|
|
24
|
+
function trackEvent(name, timestamp, props) {
|
|
25
|
+
return props === void 0 ? {
|
|
26
|
+
type: "track",
|
|
27
|
+
name,
|
|
28
|
+
timestamp
|
|
29
|
+
} : {
|
|
30
|
+
type: "track",
|
|
31
|
+
name,
|
|
32
|
+
props,
|
|
33
|
+
timestamp
|
|
34
|
+
};
|
|
46
35
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
36
|
+
function identifyEvent(userId, timestamp, traits) {
|
|
37
|
+
return traits === void 0 ? {
|
|
38
|
+
type: "identify",
|
|
39
|
+
userId,
|
|
40
|
+
timestamp
|
|
41
|
+
} : {
|
|
42
|
+
type: "identify",
|
|
43
|
+
userId,
|
|
44
|
+
traits,
|
|
45
|
+
timestamp
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function screenEvent(name, timestamp, props) {
|
|
49
|
+
return props === void 0 ? {
|
|
50
|
+
type: "screen",
|
|
51
|
+
name,
|
|
52
|
+
timestamp
|
|
53
|
+
} : {
|
|
54
|
+
type: "screen",
|
|
55
|
+
name,
|
|
56
|
+
props,
|
|
57
|
+
timestamp
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Create an in-memory analytics provider backed by a bounded ring buffer.
|
|
62
|
+
* Useful for tests and dev overlays. When the buffer is full the oldest
|
|
63
|
+
* event is dropped. `flush` is a no-op.
|
|
64
|
+
*/
|
|
65
|
+
function createMemoryAnalytics(opts) {
|
|
66
|
+
const capacity = Math.max(1, opts?.capacity ?? 256);
|
|
67
|
+
const now = opts?.now ?? (() => Date.now());
|
|
68
|
+
let events = [];
|
|
69
|
+
function push(event) {
|
|
70
|
+
events.push(event);
|
|
71
|
+
if (events.length > capacity) events.shift();
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
async track(event, props) {
|
|
75
|
+
push(trackEvent(event, now(), props));
|
|
76
|
+
},
|
|
77
|
+
async identify(userId, traits) {
|
|
78
|
+
push(identifyEvent(userId, now(), traits));
|
|
79
|
+
},
|
|
80
|
+
async screen(name, props) {
|
|
81
|
+
push(screenEvent(name, now(), props));
|
|
82
|
+
},
|
|
83
|
+
async flush() {},
|
|
84
|
+
getEvents() {
|
|
85
|
+
return events.slice();
|
|
86
|
+
},
|
|
87
|
+
clear() {
|
|
88
|
+
events = [];
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Create an analytics provider that logs each captured event to an injected
|
|
94
|
+
* sink (default `console.log`) as `log('[analytics]', event)`. `flush` is a
|
|
95
|
+
* no-op.
|
|
96
|
+
*/
|
|
97
|
+
function createConsoleAnalytics(opts) {
|
|
98
|
+
const log = opts?.log ?? console.log;
|
|
99
|
+
const now = opts?.now ?? (() => Date.now());
|
|
100
|
+
return {
|
|
101
|
+
async track(event, props) {
|
|
102
|
+
log("[analytics]", trackEvent(event, now(), props));
|
|
103
|
+
},
|
|
104
|
+
async identify(userId, traits) {
|
|
105
|
+
log("[analytics]", identifyEvent(userId, now(), traits));
|
|
106
|
+
},
|
|
107
|
+
async screen(name, props) {
|
|
108
|
+
log("[analytics]", screenEvent(name, now(), props));
|
|
109
|
+
},
|
|
110
|
+
async flush() {}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
exports.AnalyticsContext = AnalyticsContext;
|
|
115
|
+
exports.NoopAnalyticsProvider = NoopAnalyticsProvider;
|
|
116
|
+
exports.createConsoleAnalytics = createConsoleAnalytics;
|
|
117
|
+
exports.createMemoryAnalytics = createMemoryAnalytics;
|
|
118
|
+
exports.useAnalytics = useAnalytics;
|
|
119
|
+
|
|
53
120
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/memory.ts"],"sourcesContent":["/** Free-form event properties — adapters serialize as needed. */\nexport type AnalyticsProps = Record<string, unknown>;\n\n/** A captured analytics event — the discriminated union the memory/console providers record. */\nexport type AnalyticsEvent =\n | {\n readonly type: 'track';\n readonly name: string;\n readonly props?: AnalyticsProps;\n readonly timestamp: number;\n }\n | {\n readonly type: 'identify';\n readonly userId: string;\n readonly traits?: AnalyticsProps;\n readonly timestamp: number;\n }\n | {\n readonly type: 'screen';\n readonly name: string;\n readonly props?: AnalyticsProps;\n readonly timestamp: number;\n };\n\n/**\n * Analytics provider interface. Consumers supply a concrete adapter (PostHog,\n * Amplitude, Segment, Firebase Analytics) at the app boundary. Batching,\n * sampling, and offline queueing are adapter concerns — the interface is\n * deliberately thin so any SDK can back it.\n */\nexport interface AnalyticsProvider {\n track(event: string, props?: AnalyticsProps): Promise<void>;\n identify(userId: string, traits?: AnalyticsProps): Promise<void>;\n screen(name: string, props?: AnalyticsProps): Promise<void>;\n flush(): Promise<void>;\n}\n\n/** Default no-op provider. Resolves immediately; retains nothing. */\nexport const NoopAnalyticsProvider: AnalyticsProvider = {\n async track(): Promise<void> {\n // no-op\n },\n async identify(): Promise<void> {\n // no-op\n },\n async screen(): Promise<void> {\n // no-op\n },\n async flush(): Promise<void> {\n // no-op\n },\n};\n","import { createContext, useContext } from 'react';\nimport { type AnalyticsProvider, NoopAnalyticsProvider } from './provider.js';\n\n/**\n * React context carrying the active `AnalyticsProvider`. Defaults to\n * `NoopAnalyticsProvider`.\n */\nexport const AnalyticsContext = createContext<AnalyticsProvider>(NoopAnalyticsProvider);\n\n/** Hook returning the current `AnalyticsProvider`. */\nexport function useAnalytics(): AnalyticsProvider {\n return useContext(AnalyticsContext);\n}\n","import type { AnalyticsEvent, AnalyticsProps, AnalyticsProvider } from './provider.js';\n\n/** Options for `createMemoryAnalytics`. */\nexport interface MemoryAnalyticsOptions {\n /** Ring-buffer capacity; clamped to >= 1. Defaults to 256. */\n capacity?: number;\n /** Timestamp source in unix ms. Defaults to `() => Date.now()`. */\n now?: () => number;\n}\n\n/** In-memory `AnalyticsProvider` that records events into a bounded ring buffer. */\nexport interface MemoryAnalytics extends AnalyticsProvider {\n /** Recorded events, oldest -> newest, as a defensive copy. */\n getEvents(): readonly AnalyticsEvent[];\n /** Drop all recorded events. */\n clear(): void;\n}\n\n/** Options for `createConsoleAnalytics`. */\nexport interface ConsoleAnalyticsOptions {\n /** Sink for captured events. Defaults to `console.log`. */\n log?: (...args: unknown[]) => void;\n /** Timestamp source in unix ms. Defaults to `() => Date.now()`. */\n now?: () => number;\n}\n\nfunction trackEvent(name: string, timestamp: number, props?: AnalyticsProps): AnalyticsEvent {\n return props === undefined\n ? { type: 'track', name, timestamp }\n : { type: 'track', name, props, timestamp };\n}\n\nfunction identifyEvent(userId: string, timestamp: number, traits?: AnalyticsProps): AnalyticsEvent {\n return traits === undefined\n ? { type: 'identify', userId, timestamp }\n : { type: 'identify', userId, traits, timestamp };\n}\n\nfunction screenEvent(name: string, timestamp: number, props?: AnalyticsProps): AnalyticsEvent {\n return props === undefined\n ? { type: 'screen', name, timestamp }\n : { type: 'screen', name, props, timestamp };\n}\n\n/**\n * Create an in-memory analytics provider backed by a bounded ring buffer.\n * Useful for tests and dev overlays. When the buffer is full the oldest\n * event is dropped. `flush` is a no-op.\n */\nexport function createMemoryAnalytics(opts?: MemoryAnalyticsOptions): MemoryAnalytics {\n const capacity = Math.max(1, opts?.capacity ?? 256);\n const now = opts?.now ?? ((): number => Date.now());\n let events: AnalyticsEvent[] = [];\n\n function push(event: AnalyticsEvent): void {\n events.push(event);\n if (events.length > capacity) events.shift();\n }\n\n return {\n async track(event: string, props?: AnalyticsProps): Promise<void> {\n push(trackEvent(event, now(), props));\n },\n async identify(userId: string, traits?: AnalyticsProps): Promise<void> {\n push(identifyEvent(userId, now(), traits));\n },\n async screen(name: string, props?: AnalyticsProps): Promise<void> {\n push(screenEvent(name, now(), props));\n },\n async flush(): Promise<void> {\n // no-op\n },\n getEvents(): readonly AnalyticsEvent[] {\n return events.slice();\n },\n clear(): void {\n events = [];\n },\n };\n}\n\n/**\n * Create an analytics provider that logs each captured event to an injected\n * sink (default `console.log`) as `log('[analytics]', event)`. `flush` is a\n * no-op.\n */\nexport function createConsoleAnalytics(opts?: ConsoleAnalyticsOptions): AnalyticsProvider {\n const log = opts?.log ?? console.log;\n const now = opts?.now ?? ((): number => Date.now());\n\n return {\n async track(event: string, props?: AnalyticsProps): Promise<void> {\n log('[analytics]', trackEvent(event, now(), props));\n },\n async identify(userId: string, traits?: AnalyticsProps): Promise<void> {\n log('[analytics]', identifyEvent(userId, now(), traits));\n },\n async screen(name: string, props?: AnalyticsProps): Promise<void> {\n log('[analytics]', screenEvent(name, now(), props));\n },\n async flush(): Promise<void> {\n // no-op\n },\n };\n}\n"],"mappings":";;;;AAsCA,MAAa,wBAA2C;CACtD,MAAM,QAAuB,CAE7B;CACA,MAAM,WAA0B,CAEhC;CACA,MAAM,SAAwB,CAE9B;CACA,MAAM,QAAuB,CAE7B;AACF;;;;;;;AC5CA,MAAa,oBAAA,GAAA,MAAA,cAAA,CAAoD,qBAAqB;;AAGtF,SAAgB,eAAkC;CAChD,QAAA,GAAA,MAAA,WAAA,CAAkB,gBAAgB;AACpC;;;ACcA,SAAS,WAAW,MAAc,WAAmB,OAAwC;CAC3F,OAAO,UAAU,KAAA,IACb;EAAE,MAAM;EAAS;EAAM;CAAU,IACjC;EAAE,MAAM;EAAS;EAAM;EAAO;CAAU;AAC9C;AAEA,SAAS,cAAc,QAAgB,WAAmB,QAAyC;CACjG,OAAO,WAAW,KAAA,IACd;EAAE,MAAM;EAAY;EAAQ;CAAU,IACtC;EAAE,MAAM;EAAY;EAAQ;EAAQ;CAAU;AACpD;AAEA,SAAS,YAAY,MAAc,WAAmB,OAAwC;CAC5F,OAAO,UAAU,KAAA,IACb;EAAE,MAAM;EAAU;EAAM;CAAU,IAClC;EAAE,MAAM;EAAU;EAAM;EAAO;CAAU;AAC/C;;;;;;AAOA,SAAgB,sBAAsB,MAAgD;CACpF,MAAM,WAAW,KAAK,IAAI,GAAG,MAAM,YAAY,GAAG;CAClD,MAAM,MAAM,MAAM,cAAsB,KAAK,IAAI;CACjD,IAAI,SAA2B,CAAC;CAEhC,SAAS,KAAK,OAA6B;EACzC,OAAO,KAAK,KAAK;EACjB,IAAI,OAAO,SAAS,UAAU,OAAO,MAAM;CAC7C;CAEA,OAAO;EACL,MAAM,MAAM,OAAe,OAAuC;GAChE,KAAK,WAAW,OAAO,IAAI,GAAG,KAAK,CAAC;EACtC;EACA,MAAM,SAAS,QAAgB,QAAwC;GACrE,KAAK,cAAc,QAAQ,IAAI,GAAG,MAAM,CAAC;EAC3C;EACA,MAAM,OAAO,MAAc,OAAuC;GAChE,KAAK,YAAY,MAAM,IAAI,GAAG,KAAK,CAAC;EACtC;EACA,MAAM,QAAuB,CAE7B;EACA,YAAuC;GACrC,OAAO,OAAO,MAAM;EACtB;EACA,QAAc;GACZ,SAAS,CAAC;EACZ;CACF;AACF;;;;;;AAOA,SAAgB,uBAAuB,MAAmD;CACxF,MAAM,MAAM,MAAM,OAAO,QAAQ;CACjC,MAAM,MAAM,MAAM,cAAsB,KAAK,IAAI;CAEjD,OAAO;EACL,MAAM,MAAM,OAAe,OAAuC;GAChE,IAAI,eAAe,WAAW,OAAO,IAAI,GAAG,KAAK,CAAC;EACpD;EACA,MAAM,SAAS,QAAgB,QAAwC;GACrE,IAAI,eAAe,cAAc,QAAQ,IAAI,GAAG,MAAM,CAAC;EACzD;EACA,MAAM,OAAO,MAAc,OAAuC;GAChE,IAAI,eAAe,YAAY,MAAM,IAAI,GAAG,KAAK,CAAC;EACpD;EACA,MAAM,QAAuB,CAE7B;CACF;AACF"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,24 +1,115 @@
|
|
|
1
|
-
// src/provider.ts
|
|
2
|
-
var NoopAnalyticsProvider = {
|
|
3
|
-
async track() {
|
|
4
|
-
},
|
|
5
|
-
async identify() {
|
|
6
|
-
},
|
|
7
|
-
async screen() {
|
|
8
|
-
},
|
|
9
|
-
async flush() {
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// src/context.ts
|
|
14
1
|
import { createContext, useContext } from "react";
|
|
15
|
-
|
|
2
|
+
//#region src/provider.ts
|
|
3
|
+
/** Default no-op provider. Resolves immediately; retains nothing. */
|
|
4
|
+
const NoopAnalyticsProvider = {
|
|
5
|
+
async track() {},
|
|
6
|
+
async identify() {},
|
|
7
|
+
async screen() {},
|
|
8
|
+
async flush() {}
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/context.ts
|
|
12
|
+
/**
|
|
13
|
+
* React context carrying the active `AnalyticsProvider`. Defaults to
|
|
14
|
+
* `NoopAnalyticsProvider`.
|
|
15
|
+
*/
|
|
16
|
+
const AnalyticsContext = createContext(NoopAnalyticsProvider);
|
|
17
|
+
/** Hook returning the current `AnalyticsProvider`. */
|
|
16
18
|
function useAnalytics() {
|
|
17
|
-
|
|
19
|
+
return useContext(AnalyticsContext);
|
|
18
20
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/memory.ts
|
|
23
|
+
function trackEvent(name, timestamp, props) {
|
|
24
|
+
return props === void 0 ? {
|
|
25
|
+
type: "track",
|
|
26
|
+
name,
|
|
27
|
+
timestamp
|
|
28
|
+
} : {
|
|
29
|
+
type: "track",
|
|
30
|
+
name,
|
|
31
|
+
props,
|
|
32
|
+
timestamp
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function identifyEvent(userId, timestamp, traits) {
|
|
36
|
+
return traits === void 0 ? {
|
|
37
|
+
type: "identify",
|
|
38
|
+
userId,
|
|
39
|
+
timestamp
|
|
40
|
+
} : {
|
|
41
|
+
type: "identify",
|
|
42
|
+
userId,
|
|
43
|
+
traits,
|
|
44
|
+
timestamp
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function screenEvent(name, timestamp, props) {
|
|
48
|
+
return props === void 0 ? {
|
|
49
|
+
type: "screen",
|
|
50
|
+
name,
|
|
51
|
+
timestamp
|
|
52
|
+
} : {
|
|
53
|
+
type: "screen",
|
|
54
|
+
name,
|
|
55
|
+
props,
|
|
56
|
+
timestamp
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create an in-memory analytics provider backed by a bounded ring buffer.
|
|
61
|
+
* Useful for tests and dev overlays. When the buffer is full the oldest
|
|
62
|
+
* event is dropped. `flush` is a no-op.
|
|
63
|
+
*/
|
|
64
|
+
function createMemoryAnalytics(opts) {
|
|
65
|
+
const capacity = Math.max(1, opts?.capacity ?? 256);
|
|
66
|
+
const now = opts?.now ?? (() => Date.now());
|
|
67
|
+
let events = [];
|
|
68
|
+
function push(event) {
|
|
69
|
+
events.push(event);
|
|
70
|
+
if (events.length > capacity) events.shift();
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
async track(event, props) {
|
|
74
|
+
push(trackEvent(event, now(), props));
|
|
75
|
+
},
|
|
76
|
+
async identify(userId, traits) {
|
|
77
|
+
push(identifyEvent(userId, now(), traits));
|
|
78
|
+
},
|
|
79
|
+
async screen(name, props) {
|
|
80
|
+
push(screenEvent(name, now(), props));
|
|
81
|
+
},
|
|
82
|
+
async flush() {},
|
|
83
|
+
getEvents() {
|
|
84
|
+
return events.slice();
|
|
85
|
+
},
|
|
86
|
+
clear() {
|
|
87
|
+
events = [];
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Create an analytics provider that logs each captured event to an injected
|
|
93
|
+
* sink (default `console.log`) as `log('[analytics]', event)`. `flush` is a
|
|
94
|
+
* no-op.
|
|
95
|
+
*/
|
|
96
|
+
function createConsoleAnalytics(opts) {
|
|
97
|
+
const log = opts?.log ?? console.log;
|
|
98
|
+
const now = opts?.now ?? (() => Date.now());
|
|
99
|
+
return {
|
|
100
|
+
async track(event, props) {
|
|
101
|
+
log("[analytics]", trackEvent(event, now(), props));
|
|
102
|
+
},
|
|
103
|
+
async identify(userId, traits) {
|
|
104
|
+
log("[analytics]", identifyEvent(userId, now(), traits));
|
|
105
|
+
},
|
|
106
|
+
async screen(name, props) {
|
|
107
|
+
log("[analytics]", screenEvent(name, now(), props));
|
|
108
|
+
},
|
|
109
|
+
async flush() {}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
export { AnalyticsContext, NoopAnalyticsProvider, createConsoleAnalytics, createMemoryAnalytics, useAnalytics };
|
|
114
|
+
|
|
24
115
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.ts","../src/context.ts"],"sourcesContent":["/** Free-form event properties — adapters serialize as needed. */\nexport type AnalyticsProps = Record<string, unknown>;\n\n/** A
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/memory.ts"],"sourcesContent":["/** Free-form event properties — adapters serialize as needed. */\nexport type AnalyticsProps = Record<string, unknown>;\n\n/** A captured analytics event — the discriminated union the memory/console providers record. */\nexport type AnalyticsEvent =\n | {\n readonly type: 'track';\n readonly name: string;\n readonly props?: AnalyticsProps;\n readonly timestamp: number;\n }\n | {\n readonly type: 'identify';\n readonly userId: string;\n readonly traits?: AnalyticsProps;\n readonly timestamp: number;\n }\n | {\n readonly type: 'screen';\n readonly name: string;\n readonly props?: AnalyticsProps;\n readonly timestamp: number;\n };\n\n/**\n * Analytics provider interface. Consumers supply a concrete adapter (PostHog,\n * Amplitude, Segment, Firebase Analytics) at the app boundary. Batching,\n * sampling, and offline queueing are adapter concerns — the interface is\n * deliberately thin so any SDK can back it.\n */\nexport interface AnalyticsProvider {\n track(event: string, props?: AnalyticsProps): Promise<void>;\n identify(userId: string, traits?: AnalyticsProps): Promise<void>;\n screen(name: string, props?: AnalyticsProps): Promise<void>;\n flush(): Promise<void>;\n}\n\n/** Default no-op provider. Resolves immediately; retains nothing. */\nexport const NoopAnalyticsProvider: AnalyticsProvider = {\n async track(): Promise<void> {\n // no-op\n },\n async identify(): Promise<void> {\n // no-op\n },\n async screen(): Promise<void> {\n // no-op\n },\n async flush(): Promise<void> {\n // no-op\n },\n};\n","import { createContext, useContext } from 'react';\nimport { type AnalyticsProvider, NoopAnalyticsProvider } from './provider.js';\n\n/**\n * React context carrying the active `AnalyticsProvider`. Defaults to\n * `NoopAnalyticsProvider`.\n */\nexport const AnalyticsContext = createContext<AnalyticsProvider>(NoopAnalyticsProvider);\n\n/** Hook returning the current `AnalyticsProvider`. */\nexport function useAnalytics(): AnalyticsProvider {\n return useContext(AnalyticsContext);\n}\n","import type { AnalyticsEvent, AnalyticsProps, AnalyticsProvider } from './provider.js';\n\n/** Options for `createMemoryAnalytics`. */\nexport interface MemoryAnalyticsOptions {\n /** Ring-buffer capacity; clamped to >= 1. Defaults to 256. */\n capacity?: number;\n /** Timestamp source in unix ms. Defaults to `() => Date.now()`. */\n now?: () => number;\n}\n\n/** In-memory `AnalyticsProvider` that records events into a bounded ring buffer. */\nexport interface MemoryAnalytics extends AnalyticsProvider {\n /** Recorded events, oldest -> newest, as a defensive copy. */\n getEvents(): readonly AnalyticsEvent[];\n /** Drop all recorded events. */\n clear(): void;\n}\n\n/** Options for `createConsoleAnalytics`. */\nexport interface ConsoleAnalyticsOptions {\n /** Sink for captured events. Defaults to `console.log`. */\n log?: (...args: unknown[]) => void;\n /** Timestamp source in unix ms. Defaults to `() => Date.now()`. */\n now?: () => number;\n}\n\nfunction trackEvent(name: string, timestamp: number, props?: AnalyticsProps): AnalyticsEvent {\n return props === undefined\n ? { type: 'track', name, timestamp }\n : { type: 'track', name, props, timestamp };\n}\n\nfunction identifyEvent(userId: string, timestamp: number, traits?: AnalyticsProps): AnalyticsEvent {\n return traits === undefined\n ? { type: 'identify', userId, timestamp }\n : { type: 'identify', userId, traits, timestamp };\n}\n\nfunction screenEvent(name: string, timestamp: number, props?: AnalyticsProps): AnalyticsEvent {\n return props === undefined\n ? { type: 'screen', name, timestamp }\n : { type: 'screen', name, props, timestamp };\n}\n\n/**\n * Create an in-memory analytics provider backed by a bounded ring buffer.\n * Useful for tests and dev overlays. When the buffer is full the oldest\n * event is dropped. `flush` is a no-op.\n */\nexport function createMemoryAnalytics(opts?: MemoryAnalyticsOptions): MemoryAnalytics {\n const capacity = Math.max(1, opts?.capacity ?? 256);\n const now = opts?.now ?? ((): number => Date.now());\n let events: AnalyticsEvent[] = [];\n\n function push(event: AnalyticsEvent): void {\n events.push(event);\n if (events.length > capacity) events.shift();\n }\n\n return {\n async track(event: string, props?: AnalyticsProps): Promise<void> {\n push(trackEvent(event, now(), props));\n },\n async identify(userId: string, traits?: AnalyticsProps): Promise<void> {\n push(identifyEvent(userId, now(), traits));\n },\n async screen(name: string, props?: AnalyticsProps): Promise<void> {\n push(screenEvent(name, now(), props));\n },\n async flush(): Promise<void> {\n // no-op\n },\n getEvents(): readonly AnalyticsEvent[] {\n return events.slice();\n },\n clear(): void {\n events = [];\n },\n };\n}\n\n/**\n * Create an analytics provider that logs each captured event to an injected\n * sink (default `console.log`) as `log('[analytics]', event)`. `flush` is a\n * no-op.\n */\nexport function createConsoleAnalytics(opts?: ConsoleAnalyticsOptions): AnalyticsProvider {\n const log = opts?.log ?? console.log;\n const now = opts?.now ?? ((): number => Date.now());\n\n return {\n async track(event: string, props?: AnalyticsProps): Promise<void> {\n log('[analytics]', trackEvent(event, now(), props));\n },\n async identify(userId: string, traits?: AnalyticsProps): Promise<void> {\n log('[analytics]', identifyEvent(userId, now(), traits));\n },\n async screen(name: string, props?: AnalyticsProps): Promise<void> {\n log('[analytics]', screenEvent(name, now(), props));\n },\n async flush(): Promise<void> {\n // no-op\n },\n };\n}\n"],"mappings":";;;AAsCA,MAAa,wBAA2C;CACtD,MAAM,QAAuB,CAE7B;CACA,MAAM,WAA0B,CAEhC;CACA,MAAM,SAAwB,CAE9B;CACA,MAAM,QAAuB,CAE7B;AACF;;;;;;;AC5CA,MAAa,mBAAmB,cAAiC,qBAAqB;;AAGtF,SAAgB,eAAkC;CAChD,OAAO,WAAW,gBAAgB;AACpC;;;ACcA,SAAS,WAAW,MAAc,WAAmB,OAAwC;CAC3F,OAAO,UAAU,KAAA,IACb;EAAE,MAAM;EAAS;EAAM;CAAU,IACjC;EAAE,MAAM;EAAS;EAAM;EAAO;CAAU;AAC9C;AAEA,SAAS,cAAc,QAAgB,WAAmB,QAAyC;CACjG,OAAO,WAAW,KAAA,IACd;EAAE,MAAM;EAAY;EAAQ;CAAU,IACtC;EAAE,MAAM;EAAY;EAAQ;EAAQ;CAAU;AACpD;AAEA,SAAS,YAAY,MAAc,WAAmB,OAAwC;CAC5F,OAAO,UAAU,KAAA,IACb;EAAE,MAAM;EAAU;EAAM;CAAU,IAClC;EAAE,MAAM;EAAU;EAAM;EAAO;CAAU;AAC/C;;;;;;AAOA,SAAgB,sBAAsB,MAAgD;CACpF,MAAM,WAAW,KAAK,IAAI,GAAG,MAAM,YAAY,GAAG;CAClD,MAAM,MAAM,MAAM,cAAsB,KAAK,IAAI;CACjD,IAAI,SAA2B,CAAC;CAEhC,SAAS,KAAK,OAA6B;EACzC,OAAO,KAAK,KAAK;EACjB,IAAI,OAAO,SAAS,UAAU,OAAO,MAAM;CAC7C;CAEA,OAAO;EACL,MAAM,MAAM,OAAe,OAAuC;GAChE,KAAK,WAAW,OAAO,IAAI,GAAG,KAAK,CAAC;EACtC;EACA,MAAM,SAAS,QAAgB,QAAwC;GACrE,KAAK,cAAc,QAAQ,IAAI,GAAG,MAAM,CAAC;EAC3C;EACA,MAAM,OAAO,MAAc,OAAuC;GAChE,KAAK,YAAY,MAAM,IAAI,GAAG,KAAK,CAAC;EACtC;EACA,MAAM,QAAuB,CAE7B;EACA,YAAuC;GACrC,OAAO,OAAO,MAAM;EACtB;EACA,QAAc;GACZ,SAAS,CAAC;EACZ;CACF;AACF;;;;;;AAOA,SAAgB,uBAAuB,MAAmD;CACxF,MAAM,MAAM,MAAM,OAAO,QAAQ;CACjC,MAAM,MAAM,MAAM,cAAsB,KAAK,IAAI;CAEjD,OAAO;EACL,MAAM,MAAM,OAAe,OAAuC;GAChE,IAAI,eAAe,WAAW,OAAO,IAAI,GAAG,KAAK,CAAC;EACpD;EACA,MAAM,SAAS,QAAgB,QAAwC;GACrE,IAAI,eAAe,cAAc,QAAQ,IAAI,GAAG,MAAM,CAAC;EACzD;EACA,MAAM,OAAO,MAAc,OAAuC;GAChE,IAAI,eAAe,YAAY,MAAM,IAAI,GAAG,KAAK,CAAC;EACpD;EACA,MAAM,QAAuB,CAE7B;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Analytics provider interface (consumer-supplied adapter)",
|
|
5
5
|
"keywords": ["flare-engine", "analytics", "provider", "interface"],
|
|
6
|
-
"license": "
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
7
|
"author": "Grzegorz Otto",
|
|
8
8
|
"homepage": "https://github.com/grzott/flare-engine#readme",
|
|
9
9
|
"bugs": {
|
|
@@ -19,23 +19,31 @@
|
|
|
19
19
|
"types": "dist/index.d.ts",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
|
-
"files": ["dist", "README.md", "LICENSE"],
|
|
32
|
+
"files": ["dist", "README.md", "LICENSE", "NOTICE"],
|
|
28
33
|
"scripts": {
|
|
29
|
-
"build": "
|
|
34
|
+
"build": "tsdown",
|
|
30
35
|
"test": "bun test",
|
|
31
36
|
"lint": "biome check src/",
|
|
32
37
|
"typecheck": "tsc --noEmit"
|
|
33
38
|
},
|
|
34
39
|
"devDependencies": {
|
|
35
|
-
"@types/react": "^
|
|
40
|
+
"@types/react": "^19.0.0"
|
|
36
41
|
},
|
|
37
42
|
"peerDependencies": {
|
|
38
|
-
"react": ">=
|
|
43
|
+
"react": ">=19.0.0"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=20"
|
|
39
47
|
},
|
|
40
48
|
"publishConfig": {
|
|
41
49
|
"access": "restricted",
|