helm-analytics 1.0.0

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.
Files changed (3) hide show
  1. package/README.md +38 -0
  2. package/index.js +54 -0
  3. package/package.json +27 -0
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Helm Analytics Node.js SDK
2
+
3
+ Official Node.js middleware for [Helm Analytics](https://helm-analytics.com).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install helm-analytics
9
+ ```
10
+
11
+ ## Quick Start (Express)
12
+
13
+ ```javascript
14
+ const express = require('express');
15
+ const HelmAnalytics = require('helm-analytics');
16
+
17
+ const app = express();
18
+ const helm = new HelmAnalytics({ siteId: 'YOUR_SITE_ID' });
19
+
20
+ // Use as middleware for all routes
21
+ app.use(helm.middleware());
22
+
23
+ app.get('/', (req, res) => {
24
+ res.send('Hello World');
25
+ });
26
+
27
+ app.listen(3000);
28
+ ```
29
+
30
+ ## Manual Tracking
31
+
32
+ ```javascript
33
+ app.post('/purchase', (req, res) => {
34
+ // ... process purchase ...
35
+ helm.track(req, 'conversion', { revenue: 50.00 });
36
+ res.json({ success: true });
37
+ });
38
+ ```
package/index.js ADDED
@@ -0,0 +1,54 @@
1
+ const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
2
+
3
+ class HelmAnalytics {
4
+ constructor(options = {}) {
5
+ this.siteId = options.siteId || process.env.HELM_SITE_ID;
6
+ this.apiUrl = options.apiUrl || 'https://api-sentinel.getmusterup.com/track';
7
+
8
+ if (!this.siteId) {
9
+ console.warn('HelmAnalytics: No Site ID provided. Tracking will be disabled.');
10
+ }
11
+ }
12
+
13
+ // Generic track method
14
+ async track(req, eventType = 'pageview', metadata = {}) {
15
+ if (!this.siteId) return;
16
+
17
+ try {
18
+ const payload = {
19
+ siteId: this.siteId,
20
+ url: req.originalUrl || req.url,
21
+ userAgent: req.headers['user-agent'] || '',
22
+ referrer: req.headers['referer'] || req.headers['referrer'] || '',
23
+ // ClientIP logic: Headers or socket
24
+ clientIp: req.headers['x-forwarded-for'] ? req.headers['x-forwarded-for'].split(',')[0].trim() : (req.socket ? req.socket.remoteAddress : ''),
25
+ eventType: eventType,
26
+ isServerSide: true,
27
+ ...metadata
28
+ };
29
+
30
+ // Fire and forget (don't await in middleware usually, but here we define the promise)
31
+ fetch(this.apiUrl, {
32
+ method: 'POST',
33
+ body: JSON.stringify(payload),
34
+ headers: { 'Content-Type': 'application/json' }
35
+ }).catch(err => {
36
+ // Silent error in background
37
+ });
38
+
39
+ } catch (err) {
40
+ // Catch synchronous errors
41
+ }
42
+ }
43
+
44
+ // Express Middleware
45
+ middleware() {
46
+ return (req, res, next) => {
47
+ // don't track static files if easy to detect, but usually backend handles API routes
48
+ this.track(req);
49
+ next();
50
+ };
51
+ }
52
+ }
53
+
54
+ module.exports = HelmAnalytics;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "helm-analytics",
3
+ "version": "1.0.0",
4
+ "description": "Official Node.js Middleware for Helm Analytics",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "analytics",
11
+ "helm",
12
+ "middleware",
13
+ "express"
14
+ ],
15
+ "author": "Helm Analytics",
16
+ "license": "Apache-2.0",
17
+ "dependencies": {
18
+ "node-fetch": "^3.3.2"
19
+ },
20
+ "engines": {
21
+ "node": ">=14.0.0"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/Sentinel-Analytics/sentinel-mvp/tree/master/sdk/node"
26
+ }
27
+ }