@naturalcycles/internal-web-lib 1.0.0
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/bundle/analyticsClient.js +1334 -0
- package/bundle/analyticsClient.js.map +1 -0
- package/dist/analytics/analyticsClient.d.ts +356 -0
- package/dist/analytics/analyticsClient.js +828 -0
- package/dist/analytics/index.d.ts +1 -0
- package/dist/analytics/index.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +54 -0
- package/readme.md +81 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './analyticsClient.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './analyticsClient.js';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './analytics/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './analytics/index.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@naturalcycles/internal-web-lib",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@naturalcycles/js-lib": "^15",
|
|
6
|
+
"@naturalcycles/shared": "0.0.0",
|
|
7
|
+
"tslib": "^2"
|
|
8
|
+
},
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"@naturalcycles/dev-lib": "^20",
|
|
11
|
+
"@types/node": "^26",
|
|
12
|
+
"happy-dom": "^20",
|
|
13
|
+
"typescript": "^7",
|
|
14
|
+
"vite": "^8",
|
|
15
|
+
"vitest": "^5"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": "./dist/index.js",
|
|
19
|
+
"./analytics": "./dist/analytics/index.js",
|
|
20
|
+
"./analytics/*.js": "./dist/analytics/*.js"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"bundle",
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"main": "dist/index.js",
|
|
27
|
+
"types": "dist/index.d.ts",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=24.15.0"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git@github.com:NaturalCycles/NCBackend3.git",
|
|
35
|
+
"directory": "packages/internal-web-lib"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"version": "1.0.0",
|
|
41
|
+
"description": "Internal web libraries for Natural Cycles, served via CDN and published from the NCBackend3 monorepo",
|
|
42
|
+
"author": "Natural Cycles Team",
|
|
43
|
+
"license": "UNLICENSED",
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc -P tsconfig.prod.json --noCheck && pnpm bundle",
|
|
46
|
+
"bundle": "vite build",
|
|
47
|
+
"test": "dev-lib test",
|
|
48
|
+
"lint": "dev-lib lint",
|
|
49
|
+
"bt": "dev-lib bt",
|
|
50
|
+
"clean": "dev-lib clean",
|
|
51
|
+
"typecheck": "dev-lib typecheck",
|
|
52
|
+
"check": "dev-lib check"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
## @naturalcycles/internal-web-lib
|
|
2
|
+
|
|
3
|
+
> Internal web libraries for Natural Cycles, served via CDN
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@naturalcycles/internal-web-lib)
|
|
6
|
+
|
|
7
|
+
Source lives in this monorepo at `packages/internal-web-lib`. Published publicly to npm so jsDelivr
|
|
8
|
+
can serve it, but it is not open-source: it encodes our own configuration, so consumers don't have
|
|
9
|
+
to define their own.
|
|
10
|
+
|
|
11
|
+
One self-contained bundle per feature in `bundle/`, loaded as `<script type="module" async>`.
|
|
12
|
+
Consumers with their own bundler use the package exports instead.
|
|
13
|
+
|
|
14
|
+
## Analytics client on a page
|
|
15
|
+
|
|
16
|
+
The stub is inline and synchronous, so nothing is lost while the bundle downloads. `init()` drains
|
|
17
|
+
it, replaying each call under the timestamp it was made at. The stub carries every method of the
|
|
18
|
+
real client, so a page never has to check whether it has loaded. Only `track` and `identify` can be
|
|
19
|
+
replayed, the rest have nothing to act on yet and warn.
|
|
20
|
+
|
|
21
|
+
```html
|
|
22
|
+
<script>
|
|
23
|
+
{
|
|
24
|
+
const tooEarly = name => () => console.warn(`[analytics] ${name}() before the client loaded`)
|
|
25
|
+
const queue = method =>
|
|
26
|
+
function (...args) {
|
|
27
|
+
this.q.push({ method, args, ts: Date.now() })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
globalThis.analyticsClient = {
|
|
31
|
+
q: [],
|
|
32
|
+
track: queue('track'),
|
|
33
|
+
identify: queue('identify'),
|
|
34
|
+
init: tooEarly('init'),
|
|
35
|
+
reset: tooEarly('reset'),
|
|
36
|
+
flushNow: tooEarly('flushNow'),
|
|
37
|
+
destroy: tooEarly('destroy'),
|
|
38
|
+
onEvent: () => {
|
|
39
|
+
tooEarly('onEvent')()
|
|
40
|
+
return () => {} // the unsubscribe the real onEvent returns
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
<script type="module" async>
|
|
47
|
+
import { AnalyticsClient } from 'https://cdn.jsdelivr.net/npm/@naturalcycles/internal-web-lib@1/bundle/analyticsClient.js'
|
|
48
|
+
|
|
49
|
+
const analyticsClient = new AnalyticsClient({
|
|
50
|
+
url: 'https://api.example.com/web/e',
|
|
51
|
+
clientId: 10, // ClientId.LovableBR, allow-listed by the destination
|
|
52
|
+
identity: { persistence: 'cookie', persistenceKey: 'analyticsId' },
|
|
53
|
+
})
|
|
54
|
+
analyticsClient.init()
|
|
55
|
+
|
|
56
|
+
Object.assign(globalThis, { analyticsClient }) // calls from here on go straight to the client
|
|
57
|
+
</script>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`clientId` is `ClientId` from `@naturalcycles/shared`, one id per repo the client is built from, so
|
|
61
|
+
every embedding site needs its own. Add a member there before pointing a new site at the bundle, the
|
|
62
|
+
ingestion endpoint rejects ids it doesn't know. A CDN page passes the plain number, TypeScript
|
|
63
|
+
consumers import the enum.
|
|
64
|
+
|
|
65
|
+
Pin the jsDelivr url to a major, as above. An unpinned url follows `latest`, shipping a new bundle
|
|
66
|
+
to every embedding page the moment a release lands. A major keeps fixes and features flowing while
|
|
67
|
+
holding back the releases that would need the page changed.
|
|
68
|
+
|
|
69
|
+
## Releases
|
|
70
|
+
|
|
71
|
+
Cut from `master` by `ci-release-packages.yml` on every commit under `packages/**`, driven by
|
|
72
|
+
`dev-lib release` ([Conventional Commits](https://www.conventionalcommits.org/)), same as
|
|
73
|
+
`@naturalcycles/shared`. See its readme for the commit-type mapping and `beta-*` prereleases. The
|
|
74
|
+
workflow runs each package's `build` script, so `bundle/` is rebuilt from the released commit.
|
|
75
|
+
|
|
76
|
+
## Credits
|
|
77
|
+
|
|
78
|
+
The analytics client is a clean-room reimplementation whose design, persisted-identity format and
|
|
79
|
+
default property names originate from [mixpanel-browser](https://github.com/mixpanel/mixpanel-js),
|
|
80
|
+
Copyright Mixpanel, Inc., licensed under the
|
|
81
|
+
[Apache License 2.0](https://github.com/mixpanel/mixpanel-js/blob/master/LICENSE).
|