@leanbase.com/js 0.1.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/LICENSE +37 -0
- package/README.md +155 -0
- package/dist/index.cjs +118 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.mjs +116 -0
- package/dist/index.mjs.map +1 -0
- package/dist/leanbase.iife.js +1789 -0
- package/dist/leanbase.iife.js.map +1 -0
- package/package.json +46 -0
- package/src/iife.ts +83 -0
- package/src/index.ts +2 -0
- package/src/leanbase-logger.ts +23 -0
- package/src/leanbase.ts +129 -0
- package/src/version.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Copyright 2025 Leanflag Limited
|
|
2
|
+
This project is a fork of PostHog/posthog-js.
|
|
3
|
+
|
|
4
|
+
All rights reserved. This software is proprietary to Leanflag Limited and may only be used for Leanflag Limited products and internal purposes. Redistribution, modification, or commercial use outside of Leanflag Limited is strictly prohibited without explicit written permission from Leanflag Limited.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
Copyright 2020 Posthog / Hiberly, Inc.
|
|
10
|
+
|
|
11
|
+
Copyright 2015 Mixpanel, Inc.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
Some files in this codebase contain code from getsentry/sentry-javascript by Software, Inc. dba Sentry.
|
|
15
|
+
In such cases it is explicitly stated in the file header. This license only applies to the relevant code in such cases.
|
|
16
|
+
|
|
17
|
+
MIT License
|
|
18
|
+
|
|
19
|
+
Copyright (c) 2012 Functional Software, Inc. dba Sentry
|
|
20
|
+
|
|
21
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
22
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
23
|
+
the Software without restriction, including without limitation the rights to
|
|
24
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
25
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
26
|
+
so, subject to the following conditions:
|
|
27
|
+
|
|
28
|
+
The above copyright notice and this permission notice shall be included in all
|
|
29
|
+
copies or substantial portions of the Software.
|
|
30
|
+
|
|
31
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
32
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
33
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
34
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
35
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
36
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
37
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Leanbase SDK
|
|
2
|
+
|
|
3
|
+
Lightweight browser SDK for Leanbase - event tracking, autocapture, and session replay.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @leanbase/js
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @leanbase/js
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { Leanbase } from '@leanbase/js'
|
|
17
|
+
|
|
18
|
+
// Initialize with your API key
|
|
19
|
+
const leanbase = new Leanbase('your-api-key')
|
|
20
|
+
|
|
21
|
+
// Track events
|
|
22
|
+
leanbase.capture('button_clicked', {
|
|
23
|
+
button_name: 'signup',
|
|
24
|
+
page: 'homepage'
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
// Identify users
|
|
28
|
+
leanbase.identify('user-123', {
|
|
29
|
+
email: 'user@example.com',
|
|
30
|
+
name: 'John Doe'
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## CDN / Script Tag
|
|
35
|
+
|
|
36
|
+
Include the prebuilt IIFE bundle from a CDN and use the global `leanbase` object. Call `leanbase.init('<apiKey>', options)` once, then use `leanbase.capture(...)` and friends.
|
|
37
|
+
|
|
38
|
+
```html
|
|
39
|
+
<script src="https://unpkg.com/@leanbase/js/dist/leanbase.iife.js"></script>
|
|
40
|
+
<script>
|
|
41
|
+
// Initialize with your API key
|
|
42
|
+
leanbase.init('your-api-key', { host: 'https://i.leanbase.co' })
|
|
43
|
+
|
|
44
|
+
// Track an event
|
|
45
|
+
leanbase.capture('pageview')
|
|
46
|
+
</script>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Alternatively via jsDelivr:
|
|
50
|
+
|
|
51
|
+
```html
|
|
52
|
+
<script src="https://cdn.jsdelivr.net/npm/@leanbase/js/dist/leanbase.iife.js"></script>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Configuration
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const leanbase = new Leanbase('your-api-key', {
|
|
59
|
+
host: 'https://i.leanbase.co', // default
|
|
60
|
+
autocapture: true, // default - automatically capture clicks and form interactions
|
|
61
|
+
preloadFeatureFlags: true, // default - fetch feature flags on initialization
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API Reference
|
|
66
|
+
|
|
67
|
+
### `leanbase.capture(event, properties?, options?)`
|
|
68
|
+
|
|
69
|
+
Track custom events.
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
leanbase.capture('purchase_completed', {
|
|
73
|
+
amount: 99.99,
|
|
74
|
+
currency: 'USD',
|
|
75
|
+
product_id: 'abc-123'
|
|
76
|
+
})
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### `leanbase.identify(distinctId?, properties?, options?)`
|
|
80
|
+
|
|
81
|
+
Identify users and set user properties.
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
leanbase.identify('user-456', {
|
|
85
|
+
email: 'jane@example.com',
|
|
86
|
+
plan: 'premium'
|
|
87
|
+
})
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### `leanbase.group(groupType, groupKey, properties?)`
|
|
91
|
+
|
|
92
|
+
Associate users with groups (teams, organizations, etc).
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
leanbase.group('company', 'acme-corp', {
|
|
96
|
+
name: 'Acme Corporation',
|
|
97
|
+
plan: 'enterprise'
|
|
98
|
+
})
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### `leanbase.alias(alias)`
|
|
102
|
+
|
|
103
|
+
Create an alias for the current user.
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
leanbase.alias('user-123')
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Feature Flags
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
// Check if a feature is enabled
|
|
113
|
+
const isEnabled = leanbase.isFeatureEnabled('new-feature')
|
|
114
|
+
|
|
115
|
+
// Get feature flag value with fallback
|
|
116
|
+
const variant = leanbase.getFeatureFlag('experiment-variant', 'control')
|
|
117
|
+
|
|
118
|
+
// Reload feature flags
|
|
119
|
+
await leanbase.reloadFeatureFlags()
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Publishing
|
|
123
|
+
|
|
124
|
+
### Version Bumping
|
|
125
|
+
This monorepo uses [Changesets](https://github.com/changesets/changesets) for versioning.
|
|
126
|
+
|
|
127
|
+
To bump the version:
|
|
128
|
+
1. Create a changeset: `pnpm changeset add`
|
|
129
|
+
- Select `@leanbase/js` package
|
|
130
|
+
- Choose version bump type (patch, minor, major)
|
|
131
|
+
- Add a description of changes
|
|
132
|
+
2. Commit the changeset file (`.changeset/*.md`)
|
|
133
|
+
3. Run `pnpm changeset version` to update versions
|
|
134
|
+
4. Commit the version changes
|
|
135
|
+
|
|
136
|
+
Example changeset file (`.changeset/example-changes.md`):
|
|
137
|
+
```markdown
|
|
138
|
+
---
|
|
139
|
+
"@leanbase.com/js": patch
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
Added IIFE build for script-tag usage and improved documentation.
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Publishing to NPM
|
|
146
|
+
1. Ensure you have an NPM token with publish permissions
|
|
147
|
+
2. Set the token: `export NPM_TOKEN=your_token`
|
|
148
|
+
3. Publish: `pnpm --filter=@leanbase/js publish`
|
|
149
|
+
4. Or test first: `pnpm --filter=@leanbase/js exec npm publish --dry-run`
|
|
150
|
+
|
|
151
|
+
The package will be published to https://www.npmjs.com/package/@leanbase/js
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
Copyrighted by Leanflag Limited
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@posthog/core');
|
|
4
|
+
|
|
5
|
+
const version = '0.1.0';
|
|
6
|
+
|
|
7
|
+
// Simple logger with [Leanbase] prefix
|
|
8
|
+
const PREFIX = '[Leanbase]';
|
|
9
|
+
const logger = {
|
|
10
|
+
info: (...args) => {
|
|
11
|
+
if (typeof console !== 'undefined') {
|
|
12
|
+
// eslint-disable-next-line no-console
|
|
13
|
+
console.log(PREFIX, ...args);
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
warn: (...args) => {
|
|
17
|
+
if (typeof console !== 'undefined') {
|
|
18
|
+
// eslint-disable-next-line no-console
|
|
19
|
+
console.warn(PREFIX, ...args);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
error: (...args) => {
|
|
23
|
+
if (typeof console !== 'undefined') {
|
|
24
|
+
// eslint-disable-next-line no-console
|
|
25
|
+
console.error(PREFIX, ...args);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
class Leanbase extends core.PostHogCore {
|
|
31
|
+
constructor(apiKey, options) {
|
|
32
|
+
// Leanbase defaults
|
|
33
|
+
const leanbaseOptions = {
|
|
34
|
+
host: 'https://i.leanbase.co',
|
|
35
|
+
...options
|
|
36
|
+
};
|
|
37
|
+
super(apiKey, leanbaseOptions);
|
|
38
|
+
this._storage = new Map();
|
|
39
|
+
this._storageKey = `leanbase_${apiKey}`;
|
|
40
|
+
// Load from localStorage if available
|
|
41
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
42
|
+
try {
|
|
43
|
+
const stored = window.localStorage.getItem(this._storageKey);
|
|
44
|
+
if (stored) {
|
|
45
|
+
const parsed = JSON.parse(stored);
|
|
46
|
+
Object.entries(parsed).forEach(([key, value]) => {
|
|
47
|
+
this._storage.set(key, value);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
} catch (err) {
|
|
51
|
+
logger.warn('Failed to load persisted data', err);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
logger.info('Leanbase initialized', {
|
|
55
|
+
apiKey,
|
|
56
|
+
host: leanbaseOptions.host
|
|
57
|
+
});
|
|
58
|
+
// Preload feature flags if not explicitly disabled
|
|
59
|
+
if (options?.preloadFeatureFlags !== false) {
|
|
60
|
+
this.reloadFeatureFlags();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// PostHogCore abstract methods
|
|
64
|
+
fetch(url, options) {
|
|
65
|
+
const fetchFn = core.getFetch();
|
|
66
|
+
if (!fetchFn) {
|
|
67
|
+
return Promise.reject(new Error('Fetch API is not available in this environment.'));
|
|
68
|
+
}
|
|
69
|
+
return fetchFn(url, options);
|
|
70
|
+
}
|
|
71
|
+
getLibraryId() {
|
|
72
|
+
return 'leanbase';
|
|
73
|
+
}
|
|
74
|
+
getLibraryVersion() {
|
|
75
|
+
return version;
|
|
76
|
+
}
|
|
77
|
+
getCustomUserAgent() {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
getPersistedProperty(key) {
|
|
81
|
+
return this._storage.get(key);
|
|
82
|
+
}
|
|
83
|
+
setPersistedProperty(key, value) {
|
|
84
|
+
if (core.isNull(value)) {
|
|
85
|
+
this._storage.delete(key);
|
|
86
|
+
} else {
|
|
87
|
+
this._storage.set(key, value);
|
|
88
|
+
}
|
|
89
|
+
// Persist to localStorage if available
|
|
90
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
91
|
+
try {
|
|
92
|
+
const obj = {};
|
|
93
|
+
this._storage.forEach((v, k) => {
|
|
94
|
+
obj[k] = v;
|
|
95
|
+
});
|
|
96
|
+
window.localStorage.setItem(this._storageKey, JSON.stringify(obj));
|
|
97
|
+
} catch (err) {
|
|
98
|
+
logger.warn('Failed to persist data', err);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Public API: leanbase.capture()
|
|
103
|
+
capture(event, properties, options) {
|
|
104
|
+
super.capture(event, properties, options);
|
|
105
|
+
}
|
|
106
|
+
// Public API: leanbase.identify()
|
|
107
|
+
identify(distinctId, properties, options) {
|
|
108
|
+
super.identify(distinctId, properties, options);
|
|
109
|
+
}
|
|
110
|
+
// Cleanup
|
|
111
|
+
destroy() {
|
|
112
|
+
// Future: cleanup autocapture and session recording
|
|
113
|
+
this._storage.clear();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
exports.Leanbase = Leanbase;
|
|
118
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/version.ts","../src/leanbase-logger.ts","../src/leanbase.ts"],"sourcesContent":[null,null,null],"names":["version","PREFIX","logger","info","args","console","log","warn","error","Leanbase","PostHogCore","constructor","apiKey","options","leanbaseOptions","host","_storage","Map","_storageKey","window","localStorage","stored","getItem","parsed","JSON","parse","Object","entries","forEach","key","value","set","err","preloadFeatureFlags","reloadFeatureFlags","fetch","url","fetchFn","getFetch","Promise","reject","Error","getLibraryId","getLibraryVersion","getCustomUserAgent","getPersistedProperty","get","setPersistedProperty","isNull","delete","obj","v","k","setItem","stringify","capture","event","properties","identify","distinctId","destroy","clear"],"mappings":";;;;AAAO,MAAMA,OAAO,GAAG,OAAO;;ACA9B;AACA,MAAMC,MAAM,GAAG,YAAY;AAEpB,MAAMC,MAAM,GAAG;AAClBC,EAAAA,IAAI,EAAEA,CAAC,GAAGC,IAAW,KAAI;AACrB,IAAA,IAAI,OAAOC,OAAO,KAAK,WAAW,EAAE;AAChC;AACAA,MAAAA,OAAO,CAACC,GAAG,CAACL,MAAM,EAAE,GAAGG,IAAI,CAAC;AAChC,IAAA;EACJ,CAAC;AACDG,EAAAA,IAAI,EAAEA,CAAC,GAAGH,IAAW,KAAI;AACrB,IAAA,IAAI,OAAOC,OAAO,KAAK,WAAW,EAAE;AAChC;AACAA,MAAAA,OAAO,CAACE,IAAI,CAACN,MAAM,EAAE,GAAGG,IAAI,CAAC;AACjC,IAAA;EACJ,CAAC;AACDI,EAAAA,KAAK,EAAEA,CAAC,GAAGJ,IAAW,KAAI;AACtB,IAAA,IAAI,OAAOC,OAAO,KAAK,WAAW,EAAE;AAChC;AACAA,MAAAA,OAAO,CAACG,KAAK,CAACP,MAAM,EAAE,GAAGG,IAAI,CAAC;AAClC,IAAA;AACJ,EAAA;CACH;;ACKK,MAAOK,QAAS,SAAQC,gBAAW,CAAA;AAIrCC,EAAAA,WAAAA,CAAYC,MAAc,EAAEC,OAAyB,EAAA;AACjD;AACA,IAAA,MAAMC,eAAe,GAAuB;AACxCC,MAAAA,IAAI,EAAE,uBAAuB;MAC7B,GAAGF;KACN;AAED,IAAA,KAAK,CAACD,MAAM,EAAEE,eAAe,CAAC;AAV1B,IAAA,IAAA,CAAAE,QAAQ,GAAqB,IAAIC,GAAG,EAAE;AAY1C,IAAA,IAAI,CAACC,WAAW,GAAG,CAAA,SAAA,EAAYN,MAAM,CAAA,CAAE;AAEvC;IACA,IAAI,OAAOO,MAAM,KAAK,W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5C,OAA+B,EAAA;IAC9F,KAAK,CAAC6C,QAAQ,CAACC,UAAU,EAAEF,UAAU,EAAE5C,OAAO,CAAC;AACnD,EAAA;AAEA;AACA+C,EAAAA,OAAOA,GAAA;AACH;AACA,IAAA,IAAI,CAAC5C,QAAQ,CAAC6C,KAAK,EAAE;AACzB,EAAA;AACH;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PostHogCore, PostHogCoreOptions, PostHogFetchOptions, PostHogFetchResponse, PostHogPersistedProperty, PostHogEventProperties, PostHogCaptureOptions } from '@posthog/core';
|
|
2
|
+
|
|
3
|
+
interface LeanbaseOptions extends Partial<PostHogCoreOptions> {
|
|
4
|
+
/**
|
|
5
|
+
* Enable autocapture of clicks and form interactions
|
|
6
|
+
* @default true
|
|
7
|
+
*/
|
|
8
|
+
autocapture?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* API host for Leanbase
|
|
11
|
+
* @default 'https://i.leanbase.co'
|
|
12
|
+
*/
|
|
13
|
+
host?: string;
|
|
14
|
+
}
|
|
15
|
+
declare class Leanbase extends PostHogCore {
|
|
16
|
+
private _storage;
|
|
17
|
+
private _storageKey;
|
|
18
|
+
constructor(apiKey: string, options?: LeanbaseOptions);
|
|
19
|
+
fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
|
|
20
|
+
getLibraryId(): string;
|
|
21
|
+
getLibraryVersion(): string;
|
|
22
|
+
getCustomUserAgent(): void;
|
|
23
|
+
getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
|
|
24
|
+
setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
|
|
25
|
+
capture(event: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
26
|
+
identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
|
|
27
|
+
destroy(): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { Leanbase, type LeanbaseOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { PostHogCore, getFetch, isNull } from '@posthog/core';
|
|
2
|
+
|
|
3
|
+
const version = '0.1.0';
|
|
4
|
+
|
|
5
|
+
// Simple logger with [Leanbase] prefix
|
|
6
|
+
const PREFIX = '[Leanbase]';
|
|
7
|
+
const logger = {
|
|
8
|
+
info: (...args) => {
|
|
9
|
+
if (typeof console !== 'undefined') {
|
|
10
|
+
// eslint-disable-next-line no-console
|
|
11
|
+
console.log(PREFIX, ...args);
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
warn: (...args) => {
|
|
15
|
+
if (typeof console !== 'undefined') {
|
|
16
|
+
// eslint-disable-next-line no-console
|
|
17
|
+
console.warn(PREFIX, ...args);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
error: (...args) => {
|
|
21
|
+
if (typeof console !== 'undefined') {
|
|
22
|
+
// eslint-disable-next-line no-console
|
|
23
|
+
console.error(PREFIX, ...args);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class Leanbase extends PostHogCore {
|
|
29
|
+
constructor(apiKey, options) {
|
|
30
|
+
// Leanbase defaults
|
|
31
|
+
const leanbaseOptions = {
|
|
32
|
+
host: 'https://i.leanbase.co',
|
|
33
|
+
...options
|
|
34
|
+
};
|
|
35
|
+
super(apiKey, leanbaseOptions);
|
|
36
|
+
this._storage = new Map();
|
|
37
|
+
this._storageKey = `leanbase_${apiKey}`;
|
|
38
|
+
// Load from localStorage if available
|
|
39
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
40
|
+
try {
|
|
41
|
+
const stored = window.localStorage.getItem(this._storageKey);
|
|
42
|
+
if (stored) {
|
|
43
|
+
const parsed = JSON.parse(stored);
|
|
44
|
+
Object.entries(parsed).forEach(([key, value]) => {
|
|
45
|
+
this._storage.set(key, value);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
} catch (err) {
|
|
49
|
+
logger.warn('Failed to load persisted data', err);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
logger.info('Leanbase initialized', {
|
|
53
|
+
apiKey,
|
|
54
|
+
host: leanbaseOptions.host
|
|
55
|
+
});
|
|
56
|
+
// Preload feature flags if not explicitly disabled
|
|
57
|
+
if (options?.preloadFeatureFlags !== false) {
|
|
58
|
+
this.reloadFeatureFlags();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// PostHogCore abstract methods
|
|
62
|
+
fetch(url, options) {
|
|
63
|
+
const fetchFn = getFetch();
|
|
64
|
+
if (!fetchFn) {
|
|
65
|
+
return Promise.reject(new Error('Fetch API is not available in this environment.'));
|
|
66
|
+
}
|
|
67
|
+
return fetchFn(url, options);
|
|
68
|
+
}
|
|
69
|
+
getLibraryId() {
|
|
70
|
+
return 'leanbase';
|
|
71
|
+
}
|
|
72
|
+
getLibraryVersion() {
|
|
73
|
+
return version;
|
|
74
|
+
}
|
|
75
|
+
getCustomUserAgent() {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
getPersistedProperty(key) {
|
|
79
|
+
return this._storage.get(key);
|
|
80
|
+
}
|
|
81
|
+
setPersistedProperty(key, value) {
|
|
82
|
+
if (isNull(value)) {
|
|
83
|
+
this._storage.delete(key);
|
|
84
|
+
} else {
|
|
85
|
+
this._storage.set(key, value);
|
|
86
|
+
}
|
|
87
|
+
// Persist to localStorage if available
|
|
88
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
89
|
+
try {
|
|
90
|
+
const obj = {};
|
|
91
|
+
this._storage.forEach((v, k) => {
|
|
92
|
+
obj[k] = v;
|
|
93
|
+
});
|
|
94
|
+
window.localStorage.setItem(this._storageKey, JSON.stringify(obj));
|
|
95
|
+
} catch (err) {
|
|
96
|
+
logger.warn('Failed to persist data', err);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Public API: leanbase.capture()
|
|
101
|
+
capture(event, properties, options) {
|
|
102
|
+
super.capture(event, properties, options);
|
|
103
|
+
}
|
|
104
|
+
// Public API: leanbase.identify()
|
|
105
|
+
identify(distinctId, properties, options) {
|
|
106
|
+
super.identify(distinctId, properties, options);
|
|
107
|
+
}
|
|
108
|
+
// Cleanup
|
|
109
|
+
destroy() {
|
|
110
|
+
// Future: cleanup autocapture and session recording
|
|
111
|
+
this._storage.clear();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export { Leanbase };
|
|
116
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/version.ts","../src/leanbase-logger.ts","../src/leanbase.ts"],"sourcesContent":[null,null,null],"names":["version","PREFIX","logger","info","args","console","log","warn","error","Leanbase","PostHogCore","constructor","apiKey","options","leanbaseOptions","host","_storage","Map","_storageKey","window","localStorage","stored","getItem","parsed","JSON","parse","Object","entries","forEach","key","value","set","err","preloadFeatureFlags","reloadFeatureFlags","fetch","url","fetchFn","getFetch","Promise","reject","Error","getLibraryId","getLibraryVersion","getCustomUserAgent","getPersistedProperty","get","setPersistedProperty","isNull","delete","obj","v","k","setItem","stringify","capture","event","properties","identify","distinctId","destroy","clear"],"mappings":";;AAAO,MAAMA,OAAO,GAAG,OAAO;;ACA9B;AACA,MAAMC,MAAM,GAAG,YAAY;AAEpB,MAAMC,MAAM,GAAG;AAClBC,EAAAA,IAAI,EAAEA,CAAC,GAAGC,IAAW,KAAI;AACrB,IAAA,IAAI,OAAOC,OAAO,KAAK,WAAW,EAAE;AAChC;AACAA,MAAAA,OAAO,CAACC,GAAG,CAACL,MAAM,EAAE,GAAGG,IAAI,CAAC;AAChC,IAAA;EACJ,CAAC;AACDG,EAAAA,IAAI,EAAEA,CAAC,GAAGH,IAAW,KAAI;AACrB,IAAA,IAAI,OAAOC,OAAO,KAAK,WAAW,EAAE;AAChC;AACAA,MAAAA,OAAO,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