@savant-realms/sentinel-analytics-realm
v0.0.3
Published
TypeScript/JavaScript SDK for Sentinel Analytics Realm
Maintainers
Readme
Sentinel Analytics Realm - TypeScript/JavaScript SDK
TypeScript/JavaScript SDK for the Sentinel Analytics Realm analytics platform. Works in both Node.js and browser environments.
Installation
npm install @savant-realms/sentinel-analytics-realmor
yarn add @savant-realms/sentinel-analytics-realmUsage
Basic Setup
import { SentinelAnalyticsRealmPlugin } from '@savant-realms/sentinel-analytics-realm';
const analytics = new SentinelAnalyticsRealmPlugin({
projectId: 'your-project-id',
appId: 'your-app-id',
env: 'prod', // or 'dev', 'staging'
accessToken: 'your-access-token', // optional
baseUrl: 'https://api.sentinelanalyticsrealm.com', // optional, defaults to this
});Environment Variables (Node.js only)
You can also configure the SDK using environment variables:
export SENTINEL_ANALYTICS_REALM_PROJECT_ID="your-project-id"
export SENTINEL_ANALYTICS_REALM_APP_ID="your-app-id"
export SENTINEL_ANALYTICS_REALM_ENV="prod"
export SENTINEL_ANALYTICS_REALM_URL="https://api.sentinelanalyticsrealm.com"Then initialize without parameters:
const analytics = new SentinelAnalyticsRealmPlugin();Tracking Events
Single Event
await analytics.track('page_view', {
visitorId: 'visitor-123',
userId: 'user-456',
sessionId: 'session-789',
category: 'navigation',
action: 'view',
label: 'Home Page',
properties: {
path: '/home',
referrer: 'https://example.com',
},
context: {
userAgent: navigator.userAgent,
screenWidth: window.screen.width,
},
});Batch Events
await analytics.trackBatch([
{
name: 'page_view',
visitorId: 'visitor-123',
properties: { path: '/home' },
},
{
name: 'button_click',
visitorId: 'visitor-123',
properties: { button: 'signup' },
},
]);Identity Linking
await analytics.identify('user-456', 'visitor-123', {
email: '[email protected]',
name: 'John Doe',
plan: 'premium',
});JavaScript (CommonJS)
const { SentinelAnalyticsRealmPlugin } = require('@savant-realms/sentinel-analytics-realm');
const analytics = new SentinelAnalyticsRealmPlugin({
projectId: 'your-project-id',
appId: 'your-app-id',
});
analytics.track('page_view', {
visitorId: 'visitor-123',
});Browser Usage
For browser usage, you'll need a bundler like Webpack, Vite, or Rollup that supports CommonJS modules, or use a CDN that provides ES modules.
Alternatively, you can use a bundler to create a browser-compatible build:
// In your bundler configuration, ensure the SDK is included
import { SentinelAnalyticsRealmPlugin } from '@savant-realms/sentinel-analytics-realm';API Reference
SentinelAnalyticsRealmPlugin
Main class for interacting with the Sentinel Analytics Realm API.
Constructor
new SentinelAnalyticsRealmPlugin(config?: SentinelAnalyticsRealmConfig)Config Options:
projectId?: string- Your project IDappId?: string- Your application IDenv?: string- Environment ('prod', 'dev', 'staging'), defaults to 'prod'accessToken?: string- Optional access token for authenticationbaseUrl?: string- API base URL, defaults to 'https://api.sentinelanalyticsrealm.com'
Methods
track(name, eventData?, timeout?)
Track a single event.
name: string- Event name (required)eventData?: EventData- Event data objecttimeout?: number- Request timeout in milliseconds (default: 5000)- Returns:
Promise<ApiResponse | null>
trackBatch(events, timeout?)
Track multiple events in one call.
events: EventData[]- Array of event data objectstimeout?: number- Request timeout in milliseconds (default: 10000)- Returns:
Promise<ApiResponse | null>
identify(userId, visitorId?, traits?)
Convenience method for identity linking.
userId: string- User ID (required)visitorId?: string- Visitor ID (optional)traits?: Record<string, any>- User traits/properties- Returns:
Promise<ApiResponse | null>
EventData Interface
interface EventData {
name: string;
visitorId?: string;
userId?: string;
sessionId?: string;
category?: string;
action?: string;
label?: string;
value?: number;
properties?: Record<string, any>;
context?: Record<string, any>;
origin?: string; // 'real' or 'seed', defaults to 'real'
}Error Handling
The SDK follows a fail-silent approach similar to the Python SDK. If an error occurs during tracking, the methods return null instead of throwing an exception. This ensures your application continues to function even if analytics tracking fails.
TypeScript Support
This package includes full TypeScript type definitions. No additional @types package is required.
Development
Building
npm install
npm run buildTesting
npm testDeployment
To publish the package to npm, use the deployment script:
# Set your npm token (or add to .env file)
export NPM_TOKEN=your_npm_token_here
# Run deployment (builds, tests, and publishes)
./scripts/deploy.sh
# Or run in dry-run mode (builds and validates, but doesn't publish)
DRY_RUN=true ./scripts/deploy.shThe deployment script will:
- Install dependencies
- Build the TypeScript package
- Run tests (if configured)
- Validate package.json
- Publish to npm (if
NPM_TOKENis set) - Optionally publish to GitLab Package Registry (if
GITLAB_NPM_TOKENandCI_PROJECT_URLare set)
You can also publish manually:
npm run build
npm publishLicense
MIT
Author
Savant Realms
