JavaScript utility library to dynamically load tracking script from KEWA instance and track events with automatic page view tracking
npm install kewa-tracker-webA JavaScript utility library to dynamically load tracking script from KEWA instance and track events with automatic page view tracking.
- ✅ Dynamic Tracking script injection from KEWA instances
- ✅ Automatic page view tracking for SPAs (Single Page Applications)
- ✅ Custom event tracking
- ✅ Promise-based API
- ✅ TypeScript support
- ✅ Works with React, Vue, Angular, and vanilla JavaScript
- ✅ UMD and ESM module support
``bash`
npm install kewa-tracker-web
`javascript
import { initKewaTracker, sendPageview, trackEvent } from 'kewa-tracker-web';
// Initialize the tracker
await initKewaTracker({
baseUrl: 'https://your-kewa-instance.com',
projectId:'39r48kjbddkj', // Project Idenifier (optional)
disableTracking: boolean, // Complteley disable tracking
disablePageviewTracking: boolean, // Disable pageview tracking completely including manual pageview tracking
disableEventTracking: boolean, // Disable event tracking
customPageviewTracking: boolean, // Disable automatic pageview tracking and use manual pageview tracking by calling sendPageview()
});
// Send a custom pageview
await sendPageview({
url: '/custom-page',
title: 'Custom Page Title'
});
// Track custom events
await trackEvent('button_click', {
button_id: 'header-cta',
page: '/landing'
});
// Can be used to set the contact information for the current user.
// Example: You can set the contact information for the current user when the user logs in or registers.
await setKewaContact({
email: 'test@example.com',
first_name: 'John',
last_name: 'Doe'
});
// Reset the contact information for the current user.
// Example: You can reset the contact information for the current user when the user logs out.
await resetKewaContact();
// Track app login event.
await appLogin({
email: 'test@example.com', // Required
});
// Track app logout event.
await appLogout();
`
`javascript
const { initKewaTracker, sendPageview, trackEvent, setKewaContact, resetKewaContact, appLogin, appLogout } = require('kewa-tracker-web');
// Same API as above
`
`html`
Initialize the Kewa tracker with your KEWA instance configuration.
Parameters:
- config.baseUrl (string, required): The base URL of your KEWA instanceconfig.projectId
- (string, optional): The project identifierconfig.disableTracking
- (boolean, optional): Completely disable trackingconfig.disablePageviewTracking
- (boolean, optional): Disable pageview trackingconfig.disableEventTracking
- (boolean, optional): Disable event trackingconfig.customPageviewTracking
- (boolean, optional): Disable automatic pageview tracking and use manual pageview trackingPromise
Returns:
Example:
`javascript`
await initKewaTracker({
baseUrl: 'https://your-kewa-instance.com',
projectId: '39r48kjbddkj', // Optional
disableTracking: false,
disablePageviewTracking: false,
disableEventTracking: false,
customPageviewTracking: false
});
Send a pageview event to track page visits.
Parameters:
- props (object, optional): Additional properties for the pageviewurl
- (string): Custom URL to tracktitle
- (string): Page title
- Any other custom properties
Returns: Promise
Example:
`javascript
// Send current page
await sendPageview();
// Send custom page
await sendPageview({
url: '/custom-path',
title: 'Custom Page'
});
`
Track custom events with optional data.
Parameters:
- event (string, required): Event namedata
- (object, optional): Event data/propertiescontact
- (object, optional): Contact informationtimestamp
- (number, optional): Custom timestamp
Returns: Promise
Example:
`javascript`
await trackEvent('purchase', {
product_id: 'abc123',
value: 29.99,
currency: 'USD'
});
Set the contact information for the current user. Example: You can set the contact information for the current user when the user logs in or registers.
Parameters:
- contact (object, required): Contact information
Returns: Promise
Example:
`javascript`
await setKewaContact({
email: 'test@example.com',
first_name: 'John',
last_name: 'Doe'
});
Reset the contact information for the current user. Example: You can reset the contact information for the current user when the user logs out.
Returns: Promise
Example:
`javascript`
await resetKewaContact();
Track app login event.
Parameters:
- contact (object, required): Contact informationparams
- (object, optional): Additional parameters
Returns: Promise
Example:
`javascript`
await appLogin({
email: 'test@example.com', // Required
});
Track app logout event.
Parameters:
- contact (object, optional): Contact informationparams
- (object, optional): Additional parameters
Returns: Promise
Example:
`javascript`
await appLogout();
The library automatically tracks page changes in Single Page Applications (SPAs) by listening to:
- history.pushState()history.replaceState()
- popstate
- events
This means you don't need to manually call sendPageview() for navigation in React Router, Vue Router, etc.
You can disable automatic page tracking by setting disablePageviewTracking to true in the initKewaTracker() function. d
The library includes TypeScript definitions:
`typescript
interface KewaConfig {
baseUrl: string;
projectId: string;
disableTracking?: boolean;
disablePageviewTracking?: boolean;
disableEventTracking?: boolean;
customPageviewTracking?: boolean;
}
interface PageviewProps {
url?: string;
title?: string;
[key: string]: any;
}
declare function initKewaTracker(config: KewaConfig): Promise
declare function sendPageview(props?: PageviewProps): Promise
declare function trackEvent(
event: string,
data?: Record
contact?: any,
timestamp?: number
): Promise
declare function setKewaContact(contact: any): Promise
declare function resetKewaContact(): Promise
declare function appLogin(contact: any, params?: any): Promise
declare function appLogout(contact: any, params?: any): Promise
`
The library provides helpful error messages:
`javascript`
try {
await initKewaTracker({
baseUrl: 'https://your-kewa-instance.com',
projectId: '39r48kjbddkj', // Optional
});
} catch (error) {
console.error('Failed to initialize tracker:', error.message);
}
- Modern browsers with ES2017+ support
- Internet Explorer 11+ (with polyfills)
- Node.js 14+
1. Fork the repository
2. Create your feature branch (git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature'
3. Commit your changes ()git push origin feature/amazing-feature`)
4. Push to the branch (
5. Open a Pull Request
MIT License. See LICENSE file for details.
- 📖 Documentation
- 🐛 Issue Tracker
- 💬 Discussions