saashound-next v0.0.2
SaasHound Next Documentation
SaasHound Next empowers you to track events, send metrics, identify users, and analyze key interactions across your SaaS applications. This documentation covers both client-side and server-side usage.
Installation
To install SaasHound Next in your project:
npm install saashound-next
or
yarn add saashound-next
Client-Side Usage
Setting Up SaasHoundProvider
Wrap your application with the SaasHoundProvider
at the top level of your application.
import React from 'react';
import { SaasHoundProvider } from 'saashound-next';
const App: React.FC = () => (
<SaasHoundProvider token="API_TOKEN" project="PROJECT_NAME">
{/* Rest of Application */}
</SaasHoundProvider>
);
useSaasHound Hook
Use the useSaasHound
hook in your components to access tracking methods.
Methods Available
track(options: LogEventParams)
: Tracks custom events.identify(options: IdentifyParams)
: Identifies user traits.setUserId(userId?: string)
: Sets the user ID.clearUserId()
: Clears the user ID.
Example Usage
import React from 'react';
import { useSaasHound } from 'saashound-next';
const YourComponent: React.FC = () => {
const { logEvent, sendMetric, identify, setUserId } = useSaasHound();
const handleEvent = async () => {
await setUserId('naomi-nagata');
await logEvent({
channel: 'navigation',
title: 'Docking Successful',
userId: 'naomi-nagata',
icon: '🛳️',
notify: true,
description: 'Successful docking at Tycho Station.',
tags: {
dock: 'Bay 7',
status: 'secured',
crewCount: 11,
},
});
await sendMetric({
title: 'Fuel Reserves',
value: 45,
icon: '⛽',
increment: false,
});
await identify({
userId: 'naomi-nagata',
properties: {
name: 'Naomi Nagata',
email: 'naomi@roci.com',
role: 'Chief Engineer',
},
});
};
return (
<button onClick={handleEvent}>Log Event</button>
);
};
Server-Side Usage
The server-side library provides additional capabilities for handling events and metrics from your backend.
Importing SaasHound
import { SaasHound, getSaasHound, initializeSaasHound } from 'saashound-next';
Setting Up SaasHound
Initialization
Call initializeSaasHound
once at the startup of your application:
import { initializeSaasHound } from 'saashound-next';
const saasHound = initializeSaasHound({
token: 'API_TOKEN',
project: 'PROJECT_NAME',
});
You can later retrieve the instance using getSaasHound
:
import { getSaasHound } from 'saashound-next';
const saasHound = getSaasHound();
SaasHound Methods
logEvent(eventData: CaptureParams)
Logs a custom event on the server.
await saasHound.logEvent({
channel: 'navigation',
title: 'API Request Received',
userId: 'service-bot',
description: 'Request to /api/v1/items received successfully.',
});
sendMetric(metricData: MetricParams)
Tracks numerical data points for analytics.
await saasHound.sendMetric({
title: 'API Latency',
value: 250,
increment: false,
});
identify(identifyData: IdentifyParams)
Tracks user-specific traits for analytics.
await saasHound.identify({
userId: 'user-456',
properties: {
name: 'John Doe',
email: 'john.doe@example.com',
},
});
setUserId(userId?: string)
Sets the user ID in the current SaasHound instance:
saasHound.setUserId('user-456');
clearUserId()
Clears the current user ID from the instance, useful during logout:
saasHound.clearUserId();
Example Usage in a Server
import { initializeSaasHound } from 'saashound-next';
const saasHound = initializeSaasHound({
token: 'API_TOKEN',
project: 'PROJECT_NAME',
});
async function trackEvents() {
await saasHound.logEvent({
channel: 'backend',
title: 'Order Processed',
userId: 'customer-789',
description: 'Processed order #12345.',
});
await saasHound.sendMetric({
title: 'Orders Processed Today',
value: 100,
increment: true,
});
await saasHound.identify({
userId: 'customer-789',
properties: {
name: 'Jane Smith',
email: 'jane.smith@example.com',
},
});
}
Notes
- Ensure you only call
initializeSaasHound
once at application startup. - The server implementation automatically configures headers for authentication.
Documentation Reference
For the official documentation, please visit:
Let me know if further edits are needed!