@summalytics/js 0.1.0 → 0.1.2
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 +58 -0
- package/dist/index.cjs +105 -0
- package/dist/index.d.cts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +60 -52
- package/package.json +17 -5
- package/src/index.ts +0 -9
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,105 @@
|
|
|
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
|
+
await fetch(endpoint, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: {
|
|
65
|
+
"Content-Type": "application/json",
|
|
66
|
+
"x-summalytics-key": config.writeKey
|
|
67
|
+
},
|
|
68
|
+
body,
|
|
69
|
+
keepalive: true
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const track = async (eventName, payload = {}) => {
|
|
73
|
+
var _a2, _b;
|
|
74
|
+
await send({
|
|
75
|
+
requestId: (_a2 = payload.requestId) != null ? _a2 : createRequestId(),
|
|
76
|
+
eventName,
|
|
77
|
+
timestamp: payload.timestamp,
|
|
78
|
+
path: payload.path,
|
|
79
|
+
referrer: payload.referrer,
|
|
80
|
+
sessionId: (_b = payload.sessionId) != null ? _b : sessionId,
|
|
81
|
+
value: payload.value,
|
|
82
|
+
properties: payload.properties
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
const pageview = async (payload = {}) => {
|
|
86
|
+
var _a2, _b;
|
|
87
|
+
const path = (_a2 = payload.path) != null ? _a2 : typeof window !== "undefined" ? window.location.pathname : "/";
|
|
88
|
+
const referrer = (_b = payload.referrer) != null ? _b : typeof document !== "undefined" ? document.referrer : "";
|
|
89
|
+
await track("pageview", __spreadProps(__spreadValues({}, payload), {
|
|
90
|
+
path,
|
|
91
|
+
referrer
|
|
92
|
+
}));
|
|
93
|
+
};
|
|
94
|
+
if (config.autoPageview !== false && typeof window !== "undefined") {
|
|
95
|
+
void pageview();
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
track,
|
|
99
|
+
pageview
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
+
0 && (module.exports = {
|
|
104
|
+
createSummalytics
|
|
105
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -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.d.ts
ADDED
|
@@ -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,83 @@
|
|
|
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 !==
|
|
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 !==
|
|
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
|
-
|
|
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
|
-
|
|
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)
|
|
27
|
-
if (accepted) {
|
|
28
|
-
return
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
await fetch(config.endpoint, {
|
|
33
|
-
method: 'POST',
|
|
40
|
+
const body = JSON.stringify(payload);
|
|
41
|
+
await fetch(endpoint, {
|
|
42
|
+
method: "POST",
|
|
34
43
|
headers: {
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
"Content-Type": "application/json",
|
|
45
|
+
"x-summalytics-key": config.writeKey
|
|
37
46
|
},
|
|
38
47
|
body,
|
|
39
|
-
keepalive: true
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
|
|
48
|
+
keepalive: true
|
|
49
|
+
});
|
|
50
|
+
};
|
|
43
51
|
const track = async (eventName, payload = {}) => {
|
|
52
|
+
var _a2, _b;
|
|
44
53
|
await send({
|
|
45
|
-
requestId: payload.requestId
|
|
54
|
+
requestId: (_a2 = payload.requestId) != null ? _a2 : createRequestId(),
|
|
46
55
|
eventName,
|
|
47
56
|
timestamp: payload.timestamp,
|
|
48
57
|
path: payload.path,
|
|
49
58
|
referrer: payload.referrer,
|
|
50
|
-
sessionId: payload.sessionId
|
|
59
|
+
sessionId: (_b = payload.sessionId) != null ? _b : sessionId,
|
|
51
60
|
value: payload.value,
|
|
52
|
-
properties: payload.properties
|
|
53
|
-
})
|
|
54
|
-
}
|
|
55
|
-
|
|
61
|
+
properties: payload.properties
|
|
62
|
+
});
|
|
63
|
+
};
|
|
56
64
|
const pageview = async (payload = {}) => {
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
await track(
|
|
61
|
-
...payload,
|
|
65
|
+
var _a2, _b;
|
|
66
|
+
const path = (_a2 = payload.path) != null ? _a2 : typeof window !== "undefined" ? window.location.pathname : "/";
|
|
67
|
+
const referrer = (_b = payload.referrer) != null ? _b : typeof document !== "undefined" ? document.referrer : "";
|
|
68
|
+
await track("pageview", __spreadProps(__spreadValues({}, payload), {
|
|
62
69
|
path,
|
|
63
|
-
referrer
|
|
64
|
-
})
|
|
70
|
+
referrer
|
|
71
|
+
}));
|
|
72
|
+
};
|
|
73
|
+
if (config.autoPageview !== false && typeof window !== "undefined") {
|
|
74
|
+
void pageview();
|
|
65
75
|
}
|
|
66
|
-
|
|
67
|
-
if (config.autoPageview !== false && typeof window !== 'undefined') {
|
|
68
|
-
void pageview()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
76
|
return {
|
|
72
77
|
track,
|
|
73
|
-
pageview
|
|
74
|
-
}
|
|
78
|
+
pageview
|
|
79
|
+
};
|
|
75
80
|
}
|
|
81
|
+
export {
|
|
82
|
+
createSummalytics
|
|
83
|
+
};
|
package/package.json
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@summalytics/js",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Privacy-first analytics SDK for JavaScript and TypeScript",
|
|
5
|
+
"license": "MIT",
|
|
4
6
|
"type": "module",
|
|
5
|
-
"main": "dist/index.
|
|
7
|
+
"main": "dist/index.cjs",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
6
10
|
"exports": {
|
|
7
|
-
".":
|
|
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
|
}
|
package/src/index.ts
CHANGED
|
@@ -39,15 +39,6 @@ export function createSummalytics(config: SummalyticsConfig) {
|
|
|
39
39
|
const send = async (payload: Record<string, unknown>) => {
|
|
40
40
|
const body = JSON.stringify(payload)
|
|
41
41
|
|
|
42
|
-
if (typeof navigator !== 'undefined' && navigator.sendBeacon) {
|
|
43
|
-
const beaconUrl = `${endpoint}${endpoint.includes('?') ? '&' : '?'}key=${encodeURIComponent(config.writeKey)}`
|
|
44
|
-
const blob = new Blob([body], { type: 'application/json' })
|
|
45
|
-
const accepted = navigator.sendBeacon(beaconUrl, blob)
|
|
46
|
-
if (accepted) {
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
42
|
await fetch(endpoint, {
|
|
52
43
|
method: 'POST',
|
|
53
44
|
headers: {
|