@journium/react 1.0.1 → 1.0.2

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/readme.md CHANGED
@@ -27,6 +27,19 @@ yarn add @journium/react
27
27
 
28
28
  ## Basic Setup
29
29
 
30
+ ### Environment Variables
31
+ First, create a `.env.local` file in your project root:
32
+
33
+ **Note:** For CRA projects, use `REACT_APP_` prefix:
34
+ ```env
35
+ REACT_APP_JOURNIUM_PUBLISHABLE_KEY=your-actual-publishable-key-here
36
+ ```
37
+
38
+ **Note:** For Vite projects, use `VITE_` prefix:
39
+ ```env
40
+ VITE_JOURNIUM_PUBLISHABLE_KEY=your-actual-publishable-key-here
41
+ ```
42
+
30
43
  ### Initialize Journium
31
44
  Wrap your app with the `JourniumProvider` to enable analytics throughout your React application.
32
45
 
@@ -39,7 +52,7 @@ function Root() {
39
52
  return (
40
53
  <JourniumProvider
41
54
  config={{
42
- publishableKey: 'your-journium-publishable-key'
55
+ publishableKey: process.env.REACT_APP_JOURNIUM_PUBLISHABLE_KEY!
43
56
  }}
44
57
  >
45
58
  <App />
@@ -50,6 +63,38 @@ function Root() {
50
63
  export default Root;
51
64
  ```
52
65
 
66
+ **Vite Example:**
67
+ ```jsx
68
+ // main.tsx
69
+ import React from 'react';
70
+ import ReactDOM from 'react-dom/client';
71
+ import { JourniumProvider } from '@journium/react';
72
+ import App from './App';
73
+
74
+ ReactDOM.createRoot(document.getElementById('root')!).render(
75
+ <React.StrictMode>
76
+ <JourniumProvider
77
+ config={{
78
+ publishableKey: import.meta.env.VITE_JOURNIUM_PUBLISHABLE_KEY!
79
+ }}
80
+ >
81
+ <App />
82
+ </JourniumProvider>
83
+ </React.StrictMode>
84
+ );
85
+ ```
86
+
87
+ **Vite .env file:**
88
+ ```env
89
+ VITE_JOURNIUM_PUBLISHABLE_KEY=your-actual-publishable-key-here
90
+ ```
91
+
92
+ **For Next.js applications, use the dedicated [`@journium/nextjs`](../journium-nextjs) package instead.**
93
+
94
+ ### API Classes and Types
95
+
96
+ The main analytics class is `JourniumAnalytics`, available through the React hooks and context.
97
+
53
98
  ### Track a Custom Event
54
99
  Use the `useTrackEvent` hook to track custom events from any component.
55
100
 
@@ -119,9 +164,9 @@ function LogoutButton() {
119
164
  ```
120
165
 
121
166
  ## Advanced Setup
122
-
123
167
  You can override default configurations and control autocapture:
124
168
 
169
+ **React/CRA Example:**
125
170
  ```jsx
126
171
  import React from 'react';
127
172
  import { JourniumProvider } from '@journium/react';
@@ -131,13 +176,14 @@ function Root() {
131
176
  return (
132
177
  <JourniumProvider
133
178
  config={{
134
- publishableKey: 'your-journium-publishable-key',
135
- apiHost: 'https://your-custom-instance.com', // Optional: defaults to 'https://events.journium.app'
136
- config: {
137
- debug: true, // Enable debug logging
179
+ publishableKey: process.env.REACT_APP_JOURNIUM_PUBLISHABLE_KEY!,
180
+ apiHost: 'https://events.journium.app',
181
+ options: {
182
+ debug: process.env.REACT_APP_JOURNIUM_DEBUG === 'true',
138
183
  flushAt: 5, // Send events after N events
139
184
  flushInterval: 10000, // Send events every N milliseconds
140
185
  sessionTimeout: 1800000, // Session timeout (30 minutes)
186
+ autoTrackPageviews: true, // Automatically track pageview events (default: true)
141
187
  autocapture: { // Configure automatic event capture
142
188
  captureClicks: true, // Track click events
143
189
  captureFormSubmits: true, // Track form submissions
@@ -156,156 +202,167 @@ function Root() {
156
202
  export default Root;
157
203
  ```
158
204
 
159
- ## API Reference
205
+ **Vite Example:**
206
+ ```jsx
207
+ import React from 'react';
208
+ import { JourniumProvider } from '@journium/react';
209
+ import App from './App';
160
210
 
161
- ### `<JourniumProvider>`
162
- Provider component to initialize Journium throughout your React app.
211
+ function Root() {
212
+ return (
213
+ <JourniumProvider
214
+ config={{
215
+ publishableKey: import.meta.env.VITE_JOURNIUM_PUBLISHABLE_KEY!,
216
+ apiHost: 'https://events.journium.app',
217
+ options: {
218
+ debug: import.meta.env.VITE_JOURNIUM_DEBUG === 'true',
219
+ flushAt: 5,
220
+ flushInterval: 10000,
221
+ sessionTimeout: 1800000,
222
+ autoTrackPageviews: true,
223
+ autocapture: {
224
+ captureClicks: true,
225
+ captureFormSubmits: true,
226
+ captureFormChanges: false,
227
+ ignoreClasses: ['no-track', 'sensitive'],
228
+ ignoreElements: ['input[type="password"]']
229
+ }
230
+ }
231
+ }}
232
+ >
233
+ <App />
234
+ </JourniumProvider>
235
+ );
236
+ }
163
237
 
164
- ```jsx
165
- <JourniumProvider
166
- config={{
167
- publishableKey: 'your-journium-publishable-key',
168
- apiHost: 'https://events.journium.app', // Optional
169
- config: { /* optional local config */ }
170
- }}
171
- >
172
- <App />
173
- </JourniumProvider>
238
+ export default Root;
174
239
  ```
175
240
 
176
- ### `useTrackEvent()`
177
- Hook for tracking custom events with optional properties.
178
-
179
- ```jsx
180
- import { useTrackEvent } from '@journium/react';
241
+ ## API Reference
181
242
 
182
- function Component() {
183
- const trackEvent = useTrackEvent();
243
+ ### Components
184
244
 
185
- const handlePurchase = () => {
186
- trackEvent('purchase_completed', {
187
- product_id: 'prod_123',
188
- price: 29.99,
189
- currency: 'USD'
190
- });
191
- };
245
+ #### `<JourniumProvider>`
246
+ Provider component that initializes Journium analytics throughout your React application.
192
247
 
193
- return <button onClick={handlePurchase}>Buy Now</button>;
194
- }
195
- ```
248
+ **Props:**
249
+ - `config: JourniumConfig` - Configuration object for Journium
250
+ - `publishableKey: string` - Your Journium publishable key (required)
251
+ - `apiHost?: string` - Custom API endpoint (optional, defaults to 'https://events.journium.app')
252
+ - `options?: JourniumLocalOptions` - Local configuration options (optional)
253
+ - `children: ReactNode` - React children to wrap
196
254
 
197
- ### `useIdentify()`
198
- Hook for identifying users on login or signup.
255
+ ### Hooks
199
256
 
200
- ```jsx
201
- import { useIdentify } from '@journium/react';
257
+ #### `useTrackEvent()`
258
+ Returns a function to track custom events.
202
259
 
203
- function Component() {
204
- const identify = useIdentify();
260
+ **Returns:** `(event: string, properties?: Record<string, unknown>) => void`
261
+ - `event: string` - Event name to track
262
+ - `properties?: Record<string, unknown>` - Optional event properties
205
263
 
206
- const handleLogin = (userId, userAttributes) => {
207
- identify(userId, userAttributes);
208
- };
264
+ #### `useIdentify()`
265
+ Returns a function to identify users.
209
266
 
210
- return <LoginForm onLogin={handleLogin} />;
211
- }
212
- ```
267
+ **Returns:** `(distinctId: string, attributes?: Record<string, unknown>) => void`
268
+ - `distinctId: string` - Unique user identifier
269
+ - `attributes?: Record<string, unknown>` - Optional user attributes
213
270
 
214
- ### `useReset()`
215
- Hook for resetting user identity on logout.
271
+ #### `useReset()`
272
+ Returns a function to reset user identity (typically on logout).
216
273
 
217
- ```jsx
218
- import { useReset } from '@journium/react';
274
+ **Returns:** `() => void`
219
275
 
220
- function Component() {
221
- const reset = useReset();
276
+ #### `useTrackPageview()`
277
+ Returns a function to manually track pageview events.
222
278
 
223
- const handleLogout = () => {
224
- reset();
225
- };
279
+ **Returns:** `(properties?: Record<string, unknown>) => void`
280
+ - `properties?: Record<string, unknown>` - Optional pageview properties
226
281
 
227
- return <button onClick={handleLogout}>Logout</button>;
228
- }
229
- ```
282
+ #### `useAutoTrackPageview()`
283
+ Automatically tracks pageview when component mounts or dependencies change.
230
284
 
231
- ### `useTrackPageview()`
232
- Hook for manual pageview tracking.
285
+ **Parameters:**
286
+ - `dependencies?: React.DependencyList` - Dependencies to watch for changes (defaults to empty array)
287
+ - `properties?: Record<string, unknown>` - Optional pageview properties
233
288
 
234
- ```jsx
235
- import { useTrackPageview } from '@journium/react';
289
+ **Returns:** `void`
236
290
 
237
- function Component() {
238
- const trackPageview = useTrackPageview();
291
+ #### `useAutocapture()`
292
+ Returns functions to control automatic event capture.
239
293
 
240
- const handleSpecialPageview = () => {
241
- trackPageview({
242
- page_type: 'modal',
243
- content_type: 'pricing_calculator'
244
- });
245
- };
294
+ **Returns:** `{ startAutocapture: () => void, stopAutocapture: () => void }`
295
+ - `startAutocapture()` - Enable automatic event capture
296
+ - `stopAutocapture()` - Disable automatic event capture
246
297
 
247
- return <button onClick={handleSpecialPageview}>Track Modal View</button>;
248
- }
249
- ```
298
+ #### `useJournium()`
299
+ Returns the Journium context for advanced use cases.
250
300
 
251
- ### `useAutoTrackPageview()`
252
- Hook for automatic pageview tracking when components mount or dependencies change.
301
+ **Returns:** `JourniumContextValue`
302
+ - `analytics: JourniumAnalytics | null` - The analytics instance
303
+ - `config: JourniumConfig | null` - The configuration object
304
+ - `effectiveOptions: JourniumLocalOptions | null` - The effective options (merged local and remote)
253
305
 
254
- ```jsx
255
- import { useAutoTrackPageview } from '@journium/react';
306
+ ### Types
256
307
 
257
- function ProductPage({ productId, category }) {
258
- // Tracks pageview when component mounts or productId changes
259
- useAutoTrackPageview([productId], {
260
- page_type: 'product_detail',
261
- product_id: productId,
262
- category: category
263
- });
308
+ #### `JourniumConfig`
309
+ Configuration object for initializing Journium.
264
310
 
265
- return <div>Product {productId}</div>;
311
+ ```typescript
312
+ interface JourniumConfig {
313
+ publishableKey: string;
314
+ apiHost?: string;
315
+ options?: JourniumLocalOptions;
266
316
  }
267
317
  ```
268
318
 
269
- ### `useAutocapture()`
270
- Hook for controlling automatic event capture.
271
-
272
- ```jsx
273
- import { useAutocapture } from '@journium/react';
274
-
275
- function ConsentBanner() {
276
- const { startAutocapture, stopAutocapture } = useAutocapture();
277
-
278
- const handleAccept = () => {
279
- startAutocapture();
319
+ #### `JourniumLocalOptions`
320
+ Local configuration options that can be set on the client.
321
+
322
+ ```typescript
323
+ interface JourniumLocalOptions {
324
+ debug?: boolean; // Enable debug logging
325
+ flushAt?: number; // Number of events before auto-flush
326
+ flushInterval?: number; // Flush interval in milliseconds
327
+ autocapture?: boolean | AutocaptureOptions; // Auto-capture configuration
328
+ autoTrackPageviews?: boolean; // Automatic pageview tracking
329
+ sessionTimeout?: number; // Session timeout in milliseconds
330
+ sampling?: {
331
+ enabled?: boolean;
332
+ rate?: number;
280
333
  };
281
-
282
- const handleDecline = () => {
283
- stopAutocapture();
334
+ features?: {
335
+ enableGeolocation?: boolean;
336
+ enableSessionRecording?: boolean;
337
+ enablePerformanceTracking?: boolean;
284
338
  };
285
-
286
- return (
287
- <div>
288
- <button onClick={handleAccept}>Accept</button>
289
- <button onClick={handleDecline}>Decline</button>
290
- </div>
291
- );
292
339
  }
293
340
  ```
294
341
 
295
- ### `useJournium()`
296
- Hook for direct access to the Journium instance for advanced use cases.
297
-
298
- ```jsx
299
- import { useJournium } from '@journium/react';
300
-
301
- function Component() {
302
- const { journium } = useJournium();
303
-
304
- const handleAdvancedTracking = () => {
305
- journium?.track('custom_event', { advanced: true });
306
- journium?.flush();
307
- };
308
-
309
- return <button onClick={handleAdvancedTracking}>Advanced Track</button>;
342
+ #### `AutocaptureOptions`
343
+ Configuration for automatic event capture.
344
+
345
+ ```typescript
346
+ interface AutocaptureOptions {
347
+ captureClicks?: boolean; // Capture click events
348
+ captureFormSubmits?: boolean; // Capture form submissions
349
+ captureFormChanges?: boolean; // Capture form field changes
350
+ captureTextSelection?: boolean; // Capture text selection events
351
+ ignoreClasses?: string[]; // CSS classes to ignore
352
+ ignoreElements?: string[]; // HTML elements to ignore
353
+ captureContentText?: boolean; // Capture element text content
310
354
  }
311
- ```
355
+ ```
356
+
357
+ #### `JourniumAnalytics`
358
+ The main analytics class instance available through hooks.
359
+
360
+ **Methods:**
361
+ - `track(event: string, properties?: Record<string, unknown>): void` - Track custom event
362
+ - `identify(distinctId: string, attributes?: Record<string, unknown>): void` - Identify user
363
+ - `reset(): void` - Reset user identity
364
+ - `capturePageview(properties?: Record<string, unknown>): void` - Track pageview
365
+ - `startAutocapture(): void` - Start automatic event capture
366
+ - `stopAutocapture(): void` - Stop automatic event capture
367
+ - `flush(): void` - Flush pending events immediately
368
+ - `getEffectiveOptions(): JourniumLocalOptions` - Get effective configuration