@sparq/analytics-js
v1.1.3
Published
JavaScript/TypeScript client for Sparq Analytics. Track search queries, user events, and analytics data in your web applications.
Downloads
15
Readme
@sparq/analytics-js
JavaScript/TypeScript client for Sparq Analytics. Track search queries, user events, and analytics data in your web applications.
Installation
npm install @sparq/analytics-jsUsage
Initialize the client
import StAnalyticsClient from '@sparq/analytics-js';
const analytics = new StAnalyticsClient('your-app-id', 'your-search-token');Set a custom user ID (optional)
analytics.setUser('user-123');Set global properties
Add properties that will be included with every event:
analytics.setGlobalProps({
platform: 'web',
version: '1.0.0'
});Track search queries
analytics.searchQuery(searchResponse, 'title', 'collection-id');Track empty search results
analytics.emptySearchResults(searchResponse, isFilterApplied, 'collection-id');Send custom events
analytics.sendEvent('event-name', {
key: 'value'
}, 'collection-id');API Reference
new StAnalyticsClient(appUniqueId, searchToken)
Creates a new analytics client instance.
appUniqueId- Your application's unique identifiersearchToken- Your Sparq search token for authentication
setUser(userId)
Sets a custom user ID for tracking. Returns the client instance for chaining.
setGlobalProps(properties)
Sets global properties that will be merged with all event data.
sendEvent(eventName, eventData, collectionUniqueId?)
Sends a custom analytics event.
eventName- Name of the eventeventData- Object containing event datacollectionUniqueId- Optional collection identifier
searchQuery(searchResponse, label, collectionUniqueId?)
Tracks a search query event with results.
searchResponse- The search response objectlabel- The field to use as the result labelcollectionUniqueId- Optional collection identifier
emptySearchResults(searchResponse, isFilterApplied, collectionUniqueId?)
Tracks when a search returns no results.
searchResponse- The search response objectisFilterApplied- Whether filters were applied to the searchcollectionUniqueId- Optional collection identifier
License
MIT
