|
| 1 | +# Aptabase JavaScript SDKs |
| 2 | + |
| 3 | +> Privacy-first, open-source analytics for Web, React, Angular, and Browser Extension apps. This monorepo contains 4 packages: @aptabase/web, @aptabase/react, @aptabase/angular, and @aptabase/browser. All packages are GDPR-compliant and require no cookie banners. |
| 4 | + |
| 5 | +## @aptabase/web — SDK for Web Apps (SPAs) |
| 6 | + |
| 7 | +Install: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm add @aptabase/web |
| 11 | +``` |
| 12 | + |
| 13 | +Initialize: |
| 14 | + |
| 15 | +```js |
| 16 | +import { init } from '@aptabase/web'; |
| 17 | + |
| 18 | +init('<YOUR_APP_KEY>'); |
| 19 | +``` |
| 20 | + |
| 21 | +Track events: |
| 22 | + |
| 23 | +```js |
| 24 | +import { trackEvent } from '@aptabase/web'; |
| 25 | + |
| 26 | +trackEvent('page_view'); |
| 27 | +trackEvent('play_music', { name: 'Here comes the sun' }); |
| 28 | +``` |
| 29 | + |
| 30 | +The `init` function accepts an optional second parameter: |
| 31 | + |
| 32 | +```js |
| 33 | +init('<YOUR_APP_KEY>', { |
| 34 | + host: 'https://self-hosted.example.com', // Self-hosted Aptabase URL |
| 35 | + apiUrl: 'https://proxy.example.com/api/v0/event', // Custom API endpoint (reverse proxy) |
| 36 | + appVersion: '1.0.0', // App version string |
| 37 | + isDebug: false, // Override debug mode detection |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +Notes: |
| 42 | +- Designed for Single-Page Applications (SPAs). Each full page reload starts a new session. |
| 43 | +- Sessions auto-rotate after 1 hour of inactivity. |
| 44 | +- Property values must be strings, numbers, or booleans. |
| 45 | +- `trackEvent` runs in the background — no need to await it. |
| 46 | +- Debug mode is auto-detected from `NODE_ENV` or `localhost` hostname. |
| 47 | + |
| 48 | +## @aptabase/react — SDK for React / Next.js / Remix / Vite |
| 49 | + |
| 50 | +Install: |
| 51 | + |
| 52 | +```bash |
| 53 | +npm add @aptabase/react |
| 54 | +``` |
| 55 | + |
| 56 | +### Next.js App Router setup |
| 57 | + |
| 58 | +```tsx |
| 59 | +import { AptabaseProvider } from '@aptabase/react'; |
| 60 | + |
| 61 | +export default function RootLayout({ children }: { children: React.ReactNode }) { |
| 62 | + return ( |
| 63 | + <html lang="en"> |
| 64 | + <body> |
| 65 | + <AptabaseProvider appKey="<YOUR_APP_KEY>">{children}</AptabaseProvider> |
| 66 | + </body> |
| 67 | + </html> |
| 68 | + ); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Next.js Pages Router setup |
| 73 | + |
| 74 | +```tsx |
| 75 | +import { AptabaseProvider } from '@aptabase/react'; |
| 76 | +import type { AppProps } from 'next/app'; |
| 77 | + |
| 78 | +export default function App({ Component, pageProps }: AppProps) { |
| 79 | + return ( |
| 80 | + <AptabaseProvider appKey="<YOUR_APP_KEY>"> |
| 81 | + <Component {...pageProps} /> |
| 82 | + </AptabaseProvider> |
| 83 | + ); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Remix setup |
| 88 | + |
| 89 | +```tsx |
| 90 | +import { AptabaseProvider } from '@aptabase/react'; |
| 91 | +import { RemixBrowser } from '@remix-run/react'; |
| 92 | +import { startTransition, StrictMode } from 'react'; |
| 93 | +import { hydrateRoot } from 'react-dom/client'; |
| 94 | + |
| 95 | +startTransition(() => { |
| 96 | + hydrateRoot( |
| 97 | + document, |
| 98 | + <StrictMode> |
| 99 | + <AptabaseProvider appKey="<YOUR_APP_KEY>"> |
| 100 | + <RemixBrowser /> |
| 101 | + </AptabaseProvider> |
| 102 | + </StrictMode>, |
| 103 | + ); |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +### Create React App / Vite setup |
| 108 | + |
| 109 | +```tsx |
| 110 | +import { AptabaseProvider } from '@aptabase/react'; |
| 111 | + |
| 112 | +ReactDOM.createRoot(root).render( |
| 113 | + <React.StrictMode> |
| 114 | + <AptabaseProvider appKey="<YOUR_APP_KEY>"> |
| 115 | + <YourApp /> |
| 116 | + </AptabaseProvider> |
| 117 | + </React.StrictMode>, |
| 118 | +); |
| 119 | +``` |
| 120 | + |
| 121 | +### Track events with useAptabase hook |
| 122 | + |
| 123 | +```tsx |
| 124 | +'use client'; |
| 125 | + |
| 126 | +import { useAptabase } from '@aptabase/react'; |
| 127 | + |
| 128 | +export function MyComponent() { |
| 129 | + const { trackEvent } = useAptabase(); |
| 130 | + |
| 131 | + function handleClick() { |
| 132 | + trackEvent('button_click', { label: 'signup' }); |
| 133 | + } |
| 134 | + |
| 135 | + return <button onClick={handleClick}>Sign Up</button>; |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +`AptabaseProvider` accepts optional `options` prop with the same fields as `@aptabase/web` init options (`host`, `apiUrl`, `appVersion`, `isDebug`). |
| 140 | + |
| 141 | +Notes: |
| 142 | +- Uses React Context — wrap your app root with `AptabaseProvider`. |
| 143 | +- `useAptabase()` returns `{ trackEvent, trackError }`. |
| 144 | +- Events fired before initialization completes are queued and sent automatically. |
| 145 | +- The `'use client'` directive is required for components using `useAptabase` in Next.js App Router. |
| 146 | + |
| 147 | +## @aptabase/angular — SDK for Angular Apps |
| 148 | + |
| 149 | +Install: |
| 150 | + |
| 151 | +```bash |
| 152 | +npm add @aptabase/angular |
| 153 | +``` |
| 154 | + |
| 155 | +### Standalone API setup |
| 156 | + |
| 157 | +```ts |
| 158 | +import { provideAptabaseAnalytics } from '@aptabase/angular'; |
| 159 | + |
| 160 | +bootstrapApplication(AppComponent, { |
| 161 | + providers: [provideAptabaseAnalytics('<YOUR_APP_KEY>')], |
| 162 | +}).catch((err) => console.error(err)); |
| 163 | +``` |
| 164 | + |
| 165 | +### NgModule setup |
| 166 | + |
| 167 | +```ts |
| 168 | +import { AptabaseAnalyticsModule } from '@aptabase/angular'; |
| 169 | + |
| 170 | +@NgModule({ |
| 171 | + declarations: [AppComponent], |
| 172 | + imports: [AptabaseAnalyticsModule.forRoot('<YOUR_APP_KEY>')], |
| 173 | + bootstrap: [AppComponent], |
| 174 | +}) |
| 175 | +export class AppModule {} |
| 176 | +``` |
| 177 | + |
| 178 | +### Track events via service injection |
| 179 | + |
| 180 | +```ts |
| 181 | +import { AptabaseAnalyticsService } from '@aptabase/angular'; |
| 182 | + |
| 183 | +export class AppComponent { |
| 184 | + constructor(private _analyticsService: AptabaseAnalyticsService) {} |
| 185 | + |
| 186 | + onAction() { |
| 187 | + this._analyticsService.trackEvent('action_performed'); |
| 188 | + this._analyticsService.trackEvent('item_added', { itemId: 'abc123', price: 9.99 }); |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +Both `provideAptabaseAnalytics` and `AptabaseAnalyticsModule.forRoot` accept an optional second parameter: |
| 194 | + |
| 195 | +```ts |
| 196 | +type AptabaseOptions = { |
| 197 | + host?: string; // Self-hosted Aptabase URL |
| 198 | + apiUrl?: string; // Custom API endpoint (reverse proxy) |
| 199 | + appVersion?: string; // App version |
| 200 | + isDebug?: boolean; // Override debug mode |
| 201 | +}; |
| 202 | +``` |
| 203 | + |
| 204 | +Notes: |
| 205 | +- Supports both standalone API (`provideAptabaseAnalytics`) and NgModule (`AptabaseAnalyticsModule.forRoot`). |
| 206 | +- Inject `AptabaseAnalyticsService` in any component or service to track events. |
| 207 | + |
| 208 | +## @aptabase/browser — SDK for Browser/Chrome Extensions |
| 209 | + |
| 210 | +Install: |
| 211 | + |
| 212 | +```bash |
| 213 | +npm add @aptabase/browser |
| 214 | +``` |
| 215 | + |
| 216 | +Initialize in your **background script** (service worker): |
| 217 | + |
| 218 | +```js |
| 219 | +import { init } from '@aptabase/browser'; |
| 220 | + |
| 221 | +init('<YOUR_APP_KEY>'); |
| 222 | +``` |
| 223 | + |
| 224 | +Track events from **anywhere** in your extension (background, popup, content scripts): |
| 225 | + |
| 226 | +```js |
| 227 | +import { trackEvent } from '@aptabase/browser'; |
| 228 | + |
| 229 | +trackEvent('popup_opened'); |
| 230 | +trackEvent('feature_used', { feature: 'dark_mode' }); |
| 231 | +``` |
| 232 | + |
| 233 | +Init options: |
| 234 | + |
| 235 | +```js |
| 236 | +init('<YOUR_APP_KEY>', { |
| 237 | + host: 'https://self-hosted.example.com', |
| 238 | + apiUrl: 'https://proxy.example.com/api/v0/event', |
| 239 | + appVersion: '1.0.0', |
| 240 | + isDebug: false, // Default: auto-detected from installType |
| 241 | +}); |
| 242 | +``` |
| 243 | + |
| 244 | +Notes: |
| 245 | +- `init()` must be called in the background script (service worker), NOT in popup or content scripts. |
| 246 | +- `trackEvent()` can be called from any context — events from popup/content scripts are relayed to the background script via `chrome.runtime.sendMessage`. |
| 247 | +- Debug mode is auto-detected: `development` installType → debug, store install → production. |
| 248 | +- App version is auto-read from `chrome.runtime.getManifest().version`. |
| 249 | +- Session ID is persisted in `chrome.storage.local` to survive service worker restarts. |
| 250 | + |
| 251 | +## Cross-Discovery |
| 252 | + |
| 253 | +For all Aptabase SDKs (Swift, Kotlin, Flutter, Electron, Tauri, .NET MAUI, React Native, Python, Unity, Unreal, C++), see: https://aptabase.com/llms.txt |
0 commit comments