@app-city/analytics-node
v0.5.0
Published
Node.js analytics SDK for App City
Downloads
58
Readme
@app-city/analytics-node
A Node.js analytics SDK for App City.
Installation
npm install @app-city/analytics-node
# or
yarn add @app-city/analytics-node
# or
pnpm add @app-city/analytics-nodeUsage
const AppCityAnalytics = require('@app-city/analytics-node');
// Initialize the SDK with your API key
AppCityAnalytics.init('your-api-key');
// Track an event
AppCityAnalytics.track('server_started', {
timestamp: new Date().toISOString(),
node_version: process.version
});Express.js Middleware
const express = require('express');
const AppCityAnalytics = require('@app-city/analytics-node');
const app = express();
// Initialize the SDK
AppCityAnalytics.init('your-api-key', {
debug: process.env.NODE_ENV === 'development'
});
// Create middleware to track API requests
app.use((req, res, next) => {
const startTime = Date.now();
// Capture original end method to track when response finishes
const originalEnd = res.end;
res.end = function(...args) {
const duration = Date.now() - startTime;
AppCityAnalytics.track('api_request', {
path: req.path,
method: req.method,
statusCode: res.statusCode,
duration,
user_agent: req.headers['user-agent']
});
originalEnd.apply(res, args);
};
next();
});
// Your routes here...
// Ensure events are sent before the process exits
process.on('SIGTERM', async () => {
console.log('Shutting down...');
await AppCityAnalytics.flush();
process.exit(0);
});API Reference
init(apiKey, options?)
Initializes the SDK with your API key and optional configuration.
AppCityAnalytics.init('your-api-key', {
apiUrl: 'https://api.appcity.io',
batchSize: 10,
flushInterval: 5000,
maxRetries: 3,
retryBackoff: 1000,
debug: false
});track(event, properties?)
Tracks an event with optional properties.
AppCityAnalytics.track('database_query', {
operation: 'SELECT',
table: 'users',
duration: 120,
rows: 50
});flush()
Manually flushes the event queue.
await AppCityAnalytics.flush();For more examples and full API documentation, refer to the full documentation.
License
MIT
