Support for custom API path

This commit is contained in:
goenning 2023-12-29 09:22:45 -03:00
parent 348898fc82
commit c980afd786
7 changed files with 34 additions and 17 deletions

View file

@ -1,3 +1,7 @@
## 0.3.1
- Support for custom API path
## 0.3.0
- Internal refactor

View file

@ -1,6 +1,6 @@
{
"name": "@aptabase/react",
"version": "0.3.0",
"version": "0.3.1",
"type": "module",
"description": "React SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"main": "./dist/index.cjs",

View file

@ -1,13 +1,14 @@
'use client';
import { createContext, useContext, useEffect } from 'react';
import { inMemorySessionId, sendEvent, validateAppKey, type AptabaseOptions } from '../../shared';
import { getApiUrl, inMemorySessionId, sendEvent, validateAppKey, type AptabaseOptions } from '../../shared';
// Session expires after 1 hour of inactivity
const SESSION_TIMEOUT = 1 * 60 * 60;
const sdkVersion = `aptabase-react@${process.env.PKG_VERSION}`;
let _appKey = '';
let _apiUrl: string | undefined;
let _options: AptabaseOptions | undefined;
type ContextProps = {
@ -18,14 +19,18 @@ type ContextProps = {
function init(appKey: string, options?: AptabaseOptions) {
if (!validateAppKey(appKey)) return;
_apiUrl = getApiUrl(appKey, options);
_appKey = appKey;
_options = options;
}
async function trackEvent(eventName: string, props?: Record<string, string | number | boolean>): Promise<void> {
if (!_apiUrl) return;
const sessionId = inMemorySessionId(SESSION_TIMEOUT);
await sendEvent({
apiUrl: _apiUrl,
sessionId,
appKey: _appKey,
isDebug: _options?.isDebug,