update
This commit is contained in:
parent
812080d74a
commit
574dffb08b
16 changed files with 282 additions and 1052 deletions
|
@ -1,149 +1,156 @@
|
|||
let defaultLocale: string | undefined;
|
||||
let defaultIsDevelopment: boolean | undefined;
|
||||
let _sessionId = NewSessionId();
|
||||
let _lastTouched = new Date();
|
||||
const isInBrowser = true;
|
||||
|
||||
let _sessionId = newSessionId();
|
||||
let _lastTouched = new Date();
|
||||
|
||||
const _hosts: { [region: string]: string } = {
|
||||
ZV: 'https://events.sudovanilla.org',
|
||||
SH: '',
|
||||
// Hosts
|
||||
const _hosts: { [server: string]: string } = {
|
||||
ZV: 'https://events.sudovanilla.org',
|
||||
SH: '',
|
||||
};
|
||||
|
||||
//
|
||||
export type ZalvenaOptions = {
|
||||
// Custom host for self-hosted Zalvena.
|
||||
host?: string;
|
||||
// Custom path for API endpoint. Useful when using reverse proxy.
|
||||
apiUrl?: string;
|
||||
// Defines the app version.
|
||||
appVersion?: string;
|
||||
// Defines whether the app is running in development mode.
|
||||
isDevelopment?: boolean;
|
||||
host?: string;
|
||||
apiUrl?: string;
|
||||
appVersion?: string;
|
||||
isDevelopment?: boolean;
|
||||
};
|
||||
|
||||
export function inMemorySessionId(timeout: number): string {
|
||||
const diffInMs = new Date().getTime() - _lastTouched.getTime();
|
||||
const diffInSec = Math.floor(diffInMs / 1000);
|
||||
if (diffInSec > timeout) {
|
||||
_sessionId = newSessionId();
|
||||
}
|
||||
_lastTouched = new Date();
|
||||
// Save Session ID
|
||||
export function InMemorySessionId(timeout: number): string {
|
||||
const diffInMs = new Date().getTime() - _lastTouched.getTime();
|
||||
const diffInSec = Math.floor(diffInMs / 1000);
|
||||
if (diffInSec > timeout) {
|
||||
_sessionId = NewSessionId();
|
||||
}
|
||||
_lastTouched = new Date();
|
||||
|
||||
return _sessionId;
|
||||
return _sessionId;
|
||||
}
|
||||
|
||||
export function newSessionId(): string {
|
||||
const epochInSeconds = Math.floor(Date.now() / 1000).toString();
|
||||
const random = Math.floor(Math.random() * 100000000)
|
||||
.toString()
|
||||
.padStart(8, '0');
|
||||
// Create Session ID
|
||||
export function NewSessionId(): string {
|
||||
const epochInSeconds = Math.floor(Date.now() / 1000).toString();
|
||||
const random = Math.floor(Math.random() * 100000000)
|
||||
.toString()
|
||||
.padStart(8, '0');
|
||||
|
||||
return epochInSeconds + random;
|
||||
return epochInSeconds + random;
|
||||
}
|
||||
|
||||
export function validateAppKey(appKey: string): boolean {
|
||||
const parts = appKey.split('-');
|
||||
if (parts.length !== 3 || _hosts[parts[1]] === undefined) {
|
||||
console.warn(`The Zalvena App Key "${appKey}" is invalid. Tracking will be disabled.`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
// Check if the app key is valid
|
||||
export function ValidateAppKey(appKey: string): boolean {
|
||||
const parts = appKey.split('-');
|
||||
if (parts.length !== 3 || _hosts[parts[1]] === undefined) {
|
||||
console.warn(`The Zalvena App Key "${appKey}" is invalid. Tracking will be disabled.`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function getApiUrl(appKey: string, options?: ZalvenaOptions): string | undefined {
|
||||
const region = appKey.split('-')[1];
|
||||
if (region === 'SH') {
|
||||
if (!options?.host) {
|
||||
console.warn(`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`);
|
||||
return;
|
||||
// Check if user is using selfhosted key,
|
||||
// then point both coditions to API path
|
||||
export function GetApiURL(appKey: string, options?: ZalvenaOptions): string | undefined {
|
||||
const server = appKey.split('-')[1];
|
||||
if (server === 'SH') {
|
||||
if (!options?.host) {
|
||||
console.warn(`Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.`);
|
||||
console.warn(`Add the 'host' option to your 'init' function. 'init('key', {host: ''})'.`);
|
||||
return;
|
||||
}
|
||||
|
||||
return `${options.host}/api/v0/event`;
|
||||
}
|
||||
|
||||
return `${options.host}/api/v0/event`;
|
||||
}
|
||||
|
||||
const host = options?.host ?? _hosts[region];
|
||||
return `${host}/api/v0/event`;
|
||||
const host = options?.host ?? _hosts[server];
|
||||
return `${host}/api/v0/event`;
|
||||
}
|
||||
|
||||
export async function sendEvent(opts: {
|
||||
apiUrl: string;
|
||||
appKey?: string;
|
||||
sessionId: string;
|
||||
locale?: string;
|
||||
isDevelopment?: boolean;
|
||||
appVersion?: string;
|
||||
sdkVersion: string;
|
||||
eventName: string;
|
||||
props?: Record<string, string | number | boolean>;
|
||||
// Event Function
|
||||
export async function SendEvent(opts: {
|
||||
apiUrl: string;
|
||||
appKey?: string;
|
||||
sessionId: string;
|
||||
locale?: string;
|
||||
isDevelopment?: boolean;
|
||||
appVersion?: string;
|
||||
sdkVersion: string;
|
||||
eventName: string;
|
||||
props?: Record<string, string | number | boolean>;
|
||||
}): Promise<void> {
|
||||
if (!isInBrowser) {
|
||||
console.warn(`Zalvena: trackEvent requires a browser environment. Event "${opts.eventName}" will be discarded.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!opts.appKey) {
|
||||
console.warn(`Zalvena: init must be called before trackEvent. Event "${opts.eventName}" will be discarded.`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(opts.apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Key': opts.appKey,
|
||||
},
|
||||
credentials: 'omit',
|
||||
body: JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: opts.sessionId,
|
||||
eventName: opts.eventName,
|
||||
systemProps: {
|
||||
locale: opts.locale ?? getBrowserLocale(),
|
||||
isDebug: opts.isDevelopment ?? getIsDevelopment(),
|
||||
appVersion: opts.appVersion ?? '',
|
||||
sdkVersion: opts.sdkVersion,
|
||||
},
|
||||
props: opts.props,
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.status >= 300) {
|
||||
const responseBody = await response.text();
|
||||
console.warn(`Failed to send event "${opts.eventName}": ${response.status} ${responseBody}`);
|
||||
if (!isInBrowser) {
|
||||
console.warn(
|
||||
`Zalvena: trackEvent requires a browser environment. Event "${opts.eventName}" will be discarded.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!opts.appKey) {
|
||||
console.warn(`Zalvena: init must be called before trackEvent. Event "${opts.eventName}" will be discarded.`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(opts.apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Key': opts.appKey,
|
||||
},
|
||||
credentials: 'omit',
|
||||
body: JSON.stringify({
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: opts.sessionId,
|
||||
eventName: opts.eventName,
|
||||
systemProps: {
|
||||
locale: opts.locale ?? getBrowserLocale(),
|
||||
isDebug: opts.isDevelopment ?? getIsDevelopment(),
|
||||
appVersion: opts.appVersion ?? '',
|
||||
sdkVersion: opts.sdkVersion,
|
||||
},
|
||||
props: opts.props,
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.status >= 300) {
|
||||
const responseBody = await response.text();
|
||||
console.warn(`Failed to send event "${opts.eventName}": ${response.status} ${responseBody}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`Failed to send event "${opts.eventName}"`);
|
||||
console.warn(e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`Failed to send event "${opts.eventName}"`);
|
||||
console.warn(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Browser Locale
|
||||
function getBrowserLocale(): string | undefined {
|
||||
if (defaultLocale) {
|
||||
if (defaultLocale) {
|
||||
return defaultLocale;
|
||||
}
|
||||
|
||||
if (typeof navigator === 'undefined') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (navigator.languages.length > 0) {
|
||||
defaultLocale = navigator.languages[0];
|
||||
} else {
|
||||
defaultLocale = navigator.language;
|
||||
}
|
||||
|
||||
return defaultLocale;
|
||||
}
|
||||
|
||||
if (typeof navigator === 'undefined') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (navigator.languages.length > 0) {
|
||||
defaultLocale = navigator.languages[0];
|
||||
} else {
|
||||
defaultLocale = navigator.language;
|
||||
}
|
||||
|
||||
return defaultLocale;
|
||||
}
|
||||
|
||||
// If the environment is on the `localhost` URL,
|
||||
// send development data instead of production
|
||||
function getIsDevelopment(): boolean {
|
||||
if (location.hostname === 'localhost') {
|
||||
defaultIsDevelopment = true;
|
||||
return defaultIsDevelopment;
|
||||
} else {
|
||||
defaultIsDevelopment = false;
|
||||
return defaultIsDevelopment;
|
||||
}
|
||||
if (location.hostname === 'localhost') {
|
||||
defaultIsDevelopment = true;
|
||||
return defaultIsDevelopment;
|
||||
} else {
|
||||
defaultIsDevelopment = false;
|
||||
return defaultIsDevelopment;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,76 +0,0 @@
|
|||
# Zalvena
|
||||
|
||||
## 0.5.0
|
||||
|
||||
- Removed examples
|
||||
- Removed Eslintrc file
|
||||
- Removed package lock file
|
||||
- Removed Engines from `packages.json`
|
||||
- Removed `/.changeset/`
|
||||
- Removed `/.github/`
|
||||
- Removed `/.vscode/`
|
||||
- Removed other packages: `["angular", "react", "browser"]`
|
||||
- Removed `isInBrowserExtension` from `shared.ts`
|
||||
- Removed `isInBrowserExtension` from `shared.ts`
|
||||
- Updated license from MIT to MIT-NON-AI
|
||||
- Updated the package `@types/node` from `v20.5.7` to `v22.13.4`
|
||||
- Updated the package `prettier` from `v3.0.3` to `v3.5.1`
|
||||
- Updated the package `tsup` from `v7.2.0` to `v8.3.6`
|
||||
- Updated the package `turbo` from `v1.10.13` to `v2.4.2`
|
||||
- Added SudoVanilla's Zalvena instance to `regions` list as `SV`
|
||||
- Using Bun over NPM
|
||||
|
||||
You can view other changes by viewing the commit history.
|
||||
|
||||
---
|
||||
|
||||
# Officially from Aptabase
|
||||
|
||||
## 0.4.3
|
||||
|
||||
- Move browser locale and is debug reads from import to first event to better support SSR
|
||||
|
||||
## 0.4.2
|
||||
|
||||
- Fix error when running on chrome
|
||||
|
||||
## 0.4.1
|
||||
|
||||
- Support for custom API path
|
||||
|
||||
## 0.4.0
|
||||
|
||||
- Internal refactor
|
||||
|
||||
## 0.3.2
|
||||
|
||||
- better version of the session id generator
|
||||
|
||||
## 0.3.1
|
||||
|
||||
- use new session id format
|
||||
|
||||
## 0.3.0
|
||||
|
||||
- Added an option to specify `isDebug` at init time
|
||||
|
||||
## 0.2.0
|
||||
|
||||
- Some internal refactor to support the new `@aptabase/react` package
|
||||
|
||||
## 0.1.3
|
||||
|
||||
- Add automatic session timeout after 1 hour of inactivity
|
||||
|
||||
## 0.1.2
|
||||
|
||||
- Added support for automatic segregation of Debug/Release events
|
||||
|
||||
## 0.1.1
|
||||
|
||||
- Refactor on session generator
|
||||
|
||||
## 0.1.0
|
||||
|
||||
- Move to Rollup 3
|
||||
- Output both CJS and ESM modules
|
|
@ -1,35 +1,35 @@
|
|||
{
|
||||
"name": "@sudovanilla/zalvena",
|
||||
"version": "1.1.0",
|
||||
"type": "module",
|
||||
"description": "JavaScript SDK for Zalvena",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"require": "./dist/index.cjs",
|
||||
"import": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://ark.sudovanilla.org/korbs/zalvena",
|
||||
"directory": "packages/web"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://ark.sudovanilla.org/korbs/zalvena/issues"
|
||||
},
|
||||
"homepage": "https://ark.sudovanilla.org/korbs/zalvena",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "tsup",
|
||||
"prepublishOnly": "bun run build"
|
||||
},
|
||||
"files": [
|
||||
"LICENSE",
|
||||
"dist",
|
||||
"package.json"
|
||||
]
|
||||
"name": "@sudovanilla/zalvena",
|
||||
"version": "1.1.0",
|
||||
"type": "module",
|
||||
"description": "JavaScript SDK for Zalvena",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"require": "./dist/index.cjs",
|
||||
"import": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://ark.sudovanilla.org/korbs/zalvena",
|
||||
"directory": "packages/web"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://ark.sudovanilla.org/korbs/zalvena/issues"
|
||||
},
|
||||
"homepage": "https://ark.sudovanilla.org/korbs/zalvena",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "tsup",
|
||||
"prepublishOnly": "bun run build"
|
||||
},
|
||||
"files": [
|
||||
"LICENSE",
|
||||
"dist",
|
||||
"package.json"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { getApiUrl, inMemorySessionId, sendEvent, validateAppKey, type ZalvenaOptions } from '../../shared';
|
||||
import { GetApiURL, InMemorySessionId, SendEvent, ValidateAppKey, type ZalvenaOptions } from '../../shared';
|
||||
|
||||
// Session expires after 1 hour of inactivity
|
||||
const SESSION_TIMEOUT = 1 * 60 * 60;
|
||||
const sdkVersion = `Zalvena-web@${process.env.PKG_VERSION}`;
|
||||
|
||||
|
@ -10,27 +9,27 @@ let _options: ZalvenaOptions | undefined;
|
|||
|
||||
export { type ZalvenaOptions };
|
||||
|
||||
export function init(appKey: string, options?: ZalvenaOptions) {
|
||||
if (!validateAppKey(appKey)) return;
|
||||
export function Init(appKey: string, options?: ZalvenaOptions) {
|
||||
if (!ValidateAppKey(appKey)) return;
|
||||
|
||||
_apiUrl = options?.apiUrl ?? getApiUrl(appKey, options);
|
||||
_appKey = appKey;
|
||||
_options = options;
|
||||
_apiUrl = options?.apiUrl ?? GetApiURL(appKey, options);
|
||||
_appKey = appKey;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
export async function trackEvent(eventName: string, props?: Record<string, string | number | boolean>): Promise<void> {
|
||||
if (!_apiUrl) return;
|
||||
export async function Event(eventName: string, props?: Record<string, string | number | boolean>): Promise<void> {
|
||||
if (!_apiUrl) return;
|
||||
|
||||
const sessionId = inMemorySessionId(SESSION_TIMEOUT);
|
||||
const sessionId = InMemorySessionId(SESSION_TIMEOUT);
|
||||
|
||||
await sendEvent({
|
||||
apiUrl: _apiUrl,
|
||||
sessionId,
|
||||
appKey: _appKey,
|
||||
isDevelopment: _options?.isDevelopment,
|
||||
appVersion: _options?.appVersion,
|
||||
sdkVersion,
|
||||
eventName,
|
||||
props,
|
||||
});
|
||||
await SendEvent({
|
||||
apiUrl: _apiUrl,
|
||||
sessionId,
|
||||
appKey: _appKey,
|
||||
isDevelopment: _options?.isDevelopment,
|
||||
appVersion: _options?.appVersion,
|
||||
sdkVersion,
|
||||
eventName,
|
||||
props,
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2022",
|
||||
"strict": true,
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react-jsx",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"types": ["@types"]
|
||||
},
|
||||
"declaration": true,
|
||||
"declarationDir": "./dist"
|
||||
}
|
||||
"compilerOptions": {
|
||||
"target": "es2022",
|
||||
"strict": true,
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react-jsx",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"types": ["@types"]
|
||||
},
|
||||
"declaration": true,
|
||||
"declarationDir": "./dist"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,14 +2,14 @@ import { defineConfig } from 'tsup';
|
|||
const { version } = require('./package.json');
|
||||
|
||||
export default defineConfig({
|
||||
entry: ['src/index.ts'],
|
||||
format: ['cjs', 'esm'],
|
||||
dts: true,
|
||||
splitting: false,
|
||||
minify: true,
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
env: {
|
||||
PKG_VERSION: version,
|
||||
},
|
||||
entry: ['src/index.ts'],
|
||||
format: ['cjs', 'esm'],
|
||||
dts: true,
|
||||
splitting: false,
|
||||
minify: true,
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
env: {
|
||||
PKG_VERSION: version,
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue