@summalytics/js 0.1.0 → 0.1.1

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.
package/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # @summalytics/js
2
+
3
+ Privacy-first analytics SDK for JavaScript and TypeScript. Works in any browser environment — no cookies, no PII.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @summalytics/js
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { createSummalytics } from '@summalytics/js'
15
+
16
+ const analytics = createSummalytics({
17
+ writeKey: 'YOUR_WRITE_KEY',
18
+ // endpoint defaults to https://summalytics.ai/api/ingest
19
+ })
20
+
21
+ // Track a custom event
22
+ analytics.track('signup_completed', { value: 1 })
23
+
24
+ // Track a pageview manually
25
+ analytics.pageview({ path: '/pricing' })
26
+ ```
27
+
28
+ By default, one pageview is fired automatically on `createSummalytics()` in browser environments. Disable it with `autoPageview: false`.
29
+
30
+ ```ts
31
+ const analytics = createSummalytics({
32
+ writeKey: 'YOUR_WRITE_KEY',
33
+ autoPageview: false,
34
+ })
35
+ ```
36
+
37
+ ## Config
38
+
39
+ | Option | Type | Default | Description |
40
+ |---|---|---|---|
41
+ | `writeKey` | `string` | required | Your project write key |
42
+ | `endpoint` | `string` | `https://summalytics.ai/api/ingest` | Ingest endpoint |
43
+ | `autoPageview` | `boolean` | `true` | Fire a pageview on init |
44
+
45
+ ## Self-hosting
46
+
47
+ If you run your own Summalytics instance, pass your endpoint:
48
+
49
+ ```ts
50
+ const analytics = createSummalytics({
51
+ writeKey: 'YOUR_WRITE_KEY',
52
+ endpoint: 'https://yourapp.com/api/ingest',
53
+ })
54
+ ```
55
+
56
+ ## License
57
+
58
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __export = (target, all) => {
24
+ for (var name in all)
25
+ __defProp(target, name, { get: all[name], enumerable: true });
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+
37
+ // src/index.ts
38
+ var index_exports = {};
39
+ __export(index_exports, {
40
+ createSummalytics: () => createSummalytics
41
+ });
42
+ module.exports = __toCommonJS(index_exports);
43
+ var DEFAULT_ENDPOINT = "https://summalytics.ai/api/ingest";
44
+ function createSessionId() {
45
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
46
+ return crypto.randomUUID();
47
+ }
48
+ return `session_${Math.random().toString(36).slice(2)}`;
49
+ }
50
+ function createRequestId() {
51
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
52
+ return crypto.randomUUID();
53
+ }
54
+ return `req_${Math.random().toString(36).slice(2)}`;
55
+ }
56
+ function createSummalytics(config) {
57
+ var _a;
58
+ const sessionId = createSessionId();
59
+ const endpoint = (_a = config.endpoint) != null ? _a : DEFAULT_ENDPOINT;
60
+ const send = async (payload) => {
61
+ const body = JSON.stringify(payload);
62
+ if (typeof navigator !== "undefined" && navigator.sendBeacon) {
63
+ const beaconUrl = `${endpoint}${endpoint.includes("?") ? "&" : "?"}key=${encodeURIComponent(config.writeKey)}`;
64
+ const blob = new Blob([body], { type: "application/json" });
65
+ const accepted = navigator.sendBeacon(beaconUrl, blob);
66
+ if (accepted) {
67
+ return;
68
+ }
69
+ }
70
+ await fetch(endpoint, {
71
+ method: "POST",
72
+ headers: {
73
+ "Content-Type": "application/json",
74
+ "x-summalytics-key": config.writeKey
75
+ },
76
+ body,
77
+ keepalive: true
78
+ });
79
+ };
80
+ const track = async (eventName, payload = {}) => {
81
+ var _a2, _b;
82
+ await send({
83
+ requestId: (_a2 = payload.requestId) != null ? _a2 : createRequestId(),
84
+ eventName,
85
+ timestamp: payload.timestamp,
86
+ path: payload.path,
87
+ referrer: payload.referrer,
88
+ sessionId: (_b = payload.sessionId) != null ? _b : sessionId,
89
+ value: payload.value,
90
+ properties: payload.properties
91
+ });
92
+ };
93
+ const pageview = async (payload = {}) => {
94
+ var _a2, _b;
95
+ const path = (_a2 = payload.path) != null ? _a2 : typeof window !== "undefined" ? window.location.pathname : "/";
96
+ const referrer = (_b = payload.referrer) != null ? _b : typeof document !== "undefined" ? document.referrer : "";
97
+ await track("pageview", __spreadProps(__spreadValues({}, payload), {
98
+ path,
99
+ referrer
100
+ }));
101
+ };
102
+ if (config.autoPageview !== false && typeof window !== "undefined") {
103
+ void pageview();
104
+ }
105
+ return {
106
+ track,
107
+ pageview
108
+ };
109
+ }
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ createSummalytics
113
+ });
@@ -0,0 +1,20 @@
1
+ interface SummalyticsConfig {
2
+ endpoint?: string;
3
+ writeKey: string;
4
+ autoPageview?: boolean;
5
+ }
6
+ interface TrackPayload {
7
+ value?: number;
8
+ properties?: Record<string, unknown>;
9
+ timestamp?: string;
10
+ path?: string;
11
+ referrer?: string;
12
+ sessionId?: string;
13
+ requestId?: string;
14
+ }
15
+ declare function createSummalytics(config: SummalyticsConfig): {
16
+ track: (eventName: string, payload?: TrackPayload) => Promise<void>;
17
+ pageview: (payload?: Omit<TrackPayload, "value">) => Promise<void>;
18
+ };
19
+
20
+ export { type SummalyticsConfig, type TrackPayload, createSummalytics };
@@ -0,0 +1,20 @@
1
+ interface SummalyticsConfig {
2
+ endpoint?: string;
3
+ writeKey: string;
4
+ autoPageview?: boolean;
5
+ }
6
+ interface TrackPayload {
7
+ value?: number;
8
+ properties?: Record<string, unknown>;
9
+ timestamp?: string;
10
+ path?: string;
11
+ referrer?: string;
12
+ sessionId?: string;
13
+ requestId?: string;
14
+ }
15
+ declare function createSummalytics(config: SummalyticsConfig): {
16
+ track: (eventName: string, payload?: TrackPayload) => Promise<void>;
17
+ pageview: (payload?: Omit<TrackPayload, "value">) => Promise<void>;
18
+ };
19
+
20
+ export { type SummalyticsConfig, type TrackPayload, createSummalytics };
package/dist/index.js CHANGED
@@ -1,75 +1,91 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+
21
+ // src/index.ts
22
+ var DEFAULT_ENDPOINT = "https://summalytics.ai/api/ingest";
1
23
  function createSessionId() {
2
- if (typeof crypto !== 'undefined' && crypto.randomUUID) {
3
- return crypto.randomUUID()
24
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
25
+ return crypto.randomUUID();
4
26
  }
5
-
6
- return `session_${Math.random().toString(36).slice(2)}`
27
+ return `session_${Math.random().toString(36).slice(2)}`;
7
28
  }
8
-
9
29
  function createRequestId() {
10
- if (typeof crypto !== 'undefined' && crypto.randomUUID) {
11
- return crypto.randomUUID()
30
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
31
+ return crypto.randomUUID();
12
32
  }
13
-
14
- return `req_${Math.random().toString(36).slice(2)}`
33
+ return `req_${Math.random().toString(36).slice(2)}`;
15
34
  }
16
-
17
- export function createSummalytics(config) {
18
- const sessionId = createSessionId()
19
-
35
+ function createSummalytics(config) {
36
+ var _a;
37
+ const sessionId = createSessionId();
38
+ const endpoint = (_a = config.endpoint) != null ? _a : DEFAULT_ENDPOINT;
20
39
  const send = async (payload) => {
21
- const body = JSON.stringify(payload)
22
-
23
- if (typeof navigator !== 'undefined' && navigator.sendBeacon) {
24
- const beaconUrl = `${config.endpoint}${config.endpoint.includes('?') ? '&' : '?'}key=${encodeURIComponent(config.writeKey)}`
25
- const blob = new Blob([body], { type: 'application/json' })
26
- const accepted = navigator.sendBeacon(beaconUrl, blob)
40
+ const body = JSON.stringify(payload);
41
+ if (typeof navigator !== "undefined" && navigator.sendBeacon) {
42
+ const beaconUrl = `${endpoint}${endpoint.includes("?") ? "&" : "?"}key=${encodeURIComponent(config.writeKey)}`;
43
+ const blob = new Blob([body], { type: "application/json" });
44
+ const accepted = navigator.sendBeacon(beaconUrl, blob);
27
45
  if (accepted) {
28
- return
46
+ return;
29
47
  }
30
48
  }
31
-
32
- await fetch(config.endpoint, {
33
- method: 'POST',
49
+ await fetch(endpoint, {
50
+ method: "POST",
34
51
  headers: {
35
- 'Content-Type': 'application/json',
36
- 'x-summalytics-key': config.writeKey,
52
+ "Content-Type": "application/json",
53
+ "x-summalytics-key": config.writeKey
37
54
  },
38
55
  body,
39
- keepalive: true,
40
- })
41
- }
42
-
56
+ keepalive: true
57
+ });
58
+ };
43
59
  const track = async (eventName, payload = {}) => {
60
+ var _a2, _b;
44
61
  await send({
45
- requestId: payload.requestId ?? createRequestId(),
62
+ requestId: (_a2 = payload.requestId) != null ? _a2 : createRequestId(),
46
63
  eventName,
47
64
  timestamp: payload.timestamp,
48
65
  path: payload.path,
49
66
  referrer: payload.referrer,
50
- sessionId: payload.sessionId ?? sessionId,
67
+ sessionId: (_b = payload.sessionId) != null ? _b : sessionId,
51
68
  value: payload.value,
52
- properties: payload.properties,
53
- })
54
- }
55
-
69
+ properties: payload.properties
70
+ });
71
+ };
56
72
  const pageview = async (payload = {}) => {
57
- const path = payload.path ?? (typeof window !== 'undefined' ? window.location.pathname : '/')
58
- const referrer = payload.referrer ?? (typeof document !== 'undefined' ? document.referrer : '')
59
-
60
- await track('pageview', {
61
- ...payload,
73
+ var _a2, _b;
74
+ const path = (_a2 = payload.path) != null ? _a2 : typeof window !== "undefined" ? window.location.pathname : "/";
75
+ const referrer = (_b = payload.referrer) != null ? _b : typeof document !== "undefined" ? document.referrer : "";
76
+ await track("pageview", __spreadProps(__spreadValues({}, payload), {
62
77
  path,
63
- referrer,
64
- })
78
+ referrer
79
+ }));
80
+ };
81
+ if (config.autoPageview !== false && typeof window !== "undefined") {
82
+ void pageview();
65
83
  }
66
-
67
- if (config.autoPageview !== false && typeof window !== 'undefined') {
68
- void pageview()
69
- }
70
-
71
84
  return {
72
85
  track,
73
- pageview,
74
- }
86
+ pageview
87
+ };
75
88
  }
89
+ export {
90
+ createSummalytics
91
+ };
package/package.json CHANGED
@@ -1,16 +1,28 @@
1
1
  {
2
2
  "name": "@summalytics/js",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
+ "description": "Privacy-first analytics SDK for JavaScript and TypeScript",
5
+ "license": "MIT",
4
6
  "type": "module",
5
- "main": "dist/index.js",
7
+ "main": "dist/index.cjs",
8
+ "module": "dist/index.js",
9
+ "types": "dist/index.d.ts",
6
10
  "exports": {
7
- ".": "./dist/index.js"
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ }
8
16
  },
9
17
  "publishConfig": {
10
18
  "access": "public"
11
19
  },
12
20
  "files": [
13
21
  "dist",
14
- "src"
15
- ]
22
+ "src",
23
+ "README.md"
24
+ ],
25
+ "scripts": {
26
+ "build": "tsup"
27
+ }
16
28
  }