@temps-sdk/analytics-browser 0.0.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,102 @@
1
+ # @temps-sdk/analytics-browser
2
+
3
+ Vanilla JS analytics SDK for Temps. Use it programmatically or as a zero-config CDN script.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @temps-sdk/analytics-browser
9
+ ```
10
+
11
+ ## Programmatic
12
+
13
+ ```ts
14
+ import { init, trackEvent } from "@temps-sdk/analytics-browser";
15
+
16
+ init({
17
+ basePath: "/api/_temps",
18
+ domain: "example.com",
19
+ enableSessionRecording: false,
20
+ });
21
+
22
+ // Later in your app:
23
+ trackEvent("signup", { plan: "pro" });
24
+ ```
25
+
26
+ After `init()`, a global `window.temps` handle is exposed for convenience.
27
+
28
+ ## CDN script tag (zero config)
29
+
30
+ ```html
31
+ <script
32
+ defer
33
+ src="https://cdn.jsdelivr.net/npm/@temps-sdk/analytics-browser@0.0.1/dist/temps.min.js"
34
+ ></script>
35
+ ```
36
+
37
+ By default, the script posts to the current site's relative `/api/_temps/*`
38
+ endpoints. The script CDN origin is never used for ingest requests.
39
+
40
+ All `data-*` attributes on the script tag map to `AnalyticsOptions`
41
+ (kebab-case to camelCase), and are only needed for overrides.
42
+
43
+ ```html
44
+ <script
45
+ defer
46
+ src="https://cdn.jsdelivr.net/npm/@temps-sdk/analytics-browser@0.0.1/dist/temps.min.js"
47
+ data-session-recording="true"
48
+ ></script>
49
+ ```
50
+
51
+ For apps deployed on Temps, `/api/_temps/*` is handled by the platform. For
52
+ external apps, add a same-origin rewrite or proxy for `/api/_temps/*` to the
53
+ Temps instance.
54
+
55
+ ### Attributes
56
+
57
+ | Attribute | Type | Notes |
58
+ |---|---|---|
59
+ | `data-domain` | string | Overrides detected hostname |
60
+ | `data-base-path` | string | Optional override. Defaults to `/api/_temps` |
61
+ | `data-disabled` | `"true"` \| `"false"` | Kill switch |
62
+ | `data-ignore-localhost` | `"true"` \| `"false"` | Default: true |
63
+ | `data-auto-track-pageviews` | `"true"` \| `"false"` | Default: true |
64
+ | `data-auto-track-page-leave` | `"true"` \| `"false"` | Default: true |
65
+ | `data-auto-track-speed-analytics` | `"true"` \| `"false"` | Default: true |
66
+ | `data-auto-track-engagement` | `"true"` \| `"false"` | Default: true |
67
+ | `data-heartbeat-interval` | number (ms) | Default: 30000 |
68
+ | `data-session-recording` | `"true"` \| `"false"` | Default: false |
69
+ | `data-session-sample-rate` | 0.0 - 1.0 | Default: 1.0 |
70
+ | `data-excluded-paths` | csv | e.g. `/admin/*,/settings/*` |
71
+
72
+ ## HTML attributes for declarative events
73
+
74
+ ```html
75
+ <button temps-event-name="cta_click" temps-data-section="hero">Get started</button>
76
+ ```
77
+
78
+ Any element with `temps-event-name` fires an event on click. All `temps-data-*`
79
+ attributes are sent as event properties.
80
+
81
+ ## API
82
+
83
+ ```ts
84
+ import { init, getAnalytics, trackEvent, trackPageview, destroy } from "@temps-sdk/analytics-browser";
85
+ import { createAnalytics } from "@temps-sdk/analytics-browser"; // re-exported from core
86
+ ```
87
+
88
+ `init()` replaces any previous instance (useful for hot reload); `destroy()` tears it down.
89
+
90
+ ## Publishing
91
+
92
+ `bun run build` emits:
93
+
94
+ - `dist/index.js` and `dist/auto.js` for ESM imports
95
+ - `dist/temps.js` and `dist/temps.min.js` for CDN script tags
96
+
97
+ Before publishing, run:
98
+
99
+ ```bash
100
+ bun run test:run
101
+ npm publish --access public
102
+ ```
@@ -0,0 +1,2 @@
1
+ import "./auto";
2
+ //# sourceMappingURL=auto-full.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-full.d.ts","sourceRoot":"","sources":["../src/auto-full.ts"],"names":[],"mappings":"AAcA,OAAO,QAAQ,CAAC"}
@@ -0,0 +1,14 @@
1
+ // Full build entry: force bun to inline rrweb + @rrweb/packer into this single-file IIFE.
2
+ //
3
+ // `@temps-sdk/analytics-core`'s barrel (index.ts) statically re-exports SessionRecorder, so
4
+ // importing it here makes SessionRecorder (and therefore rrweb/@rrweb/packer) statically
5
+ // reachable from the entry. bun then inlines rrweb; the dynamic import("./SessionRecorder")
6
+ // inside Analytics and the dynamic import("rrweb")/import("@rrweb/packer") inside
7
+ // SessionRecorder all resolve to the already-bundled copy (rewritten to
8
+ // Promise.resolve().then(), zero dangling import()) -> recording is fully functional here.
9
+ //
10
+ // This build is published WITHOUT the --external flags, so rrweb stays inlined (unlike the
11
+ // light build which strips it via --external).
12
+ import { SessionRecorder } from "@temps-sdk/analytics-core";
13
+ void SessionRecorder; // reference the value so it isn't tree-shaken before inlining
14
+ import "./auto"; // identical readDataset()+boot() auto-init path as the light build
package/dist/auto.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=auto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto.d.ts","sourceRoot":"","sources":["../src/auto.ts"],"names":[],"mappings":""}
package/dist/auto.js ADDED
@@ -0,0 +1,71 @@
1
+ import { init } from "./index";
2
+ /**
3
+ * Auto-init entrypoint. Reads configuration from the currently-executing
4
+ * <script> tag's `data-*` attributes and boots Temps analytics on
5
+ * DOMContentLoaded.
6
+ *
7
+ * Usage:
8
+ * <script
9
+ * defer
10
+ * src="https://cdn.jsdelivr.net/npm/@temps-sdk/analytics-browser@0.0.1/dist/temps.min.js">
11
+ * </script>
12
+ */
13
+ function readDataset() {
14
+ if (typeof document === "undefined")
15
+ return {};
16
+ const script = document.currentScript
17
+ ?? document.querySelector("script[data-temps]")
18
+ ?? Array.from(document.scripts).reverse().find((s) => !!s.dataset?.domain || !!s.dataset?.basePath)
19
+ ?? null;
20
+ if (!script)
21
+ return {};
22
+ const ds = script.dataset;
23
+ const opts = {};
24
+ const recording = {};
25
+ if (ds.basePath)
26
+ opts.basePath = ds.basePath;
27
+ if (ds.domain)
28
+ opts.domain = ds.domain;
29
+ if (ds.disabled)
30
+ opts.disabled = ds.disabled === "true";
31
+ if (ds.ignoreLocalhost)
32
+ opts.ignoreLocalhost = ds.ignoreLocalhost === "true";
33
+ if (ds.autoTrackPageviews)
34
+ opts.autoTrackPageviews = ds.autoTrackPageviews === "true";
35
+ if (ds.autoTrackPageLeave)
36
+ opts.autoTrackPageLeave = ds.autoTrackPageLeave === "true";
37
+ if (ds.pageLeaveEventName)
38
+ opts.pageLeaveEventName = ds.pageLeaveEventName;
39
+ if (ds.autoTrackSpeedAnalytics)
40
+ opts.autoTrackSpeedAnalytics = ds.autoTrackSpeedAnalytics === "true";
41
+ if (ds.autoTrackEngagement)
42
+ opts.autoTrackEngagement = ds.autoTrackEngagement === "true";
43
+ if (ds.heartbeatInterval)
44
+ opts.heartbeatInterval = Number(ds.heartbeatInterval);
45
+ if (ds.inactivityTimeout)
46
+ opts.inactivityTimeout = Number(ds.inactivityTimeout);
47
+ if (ds.engagementThreshold)
48
+ opts.engagementThreshold = Number(ds.engagementThreshold);
49
+ if (ds.sessionRecording)
50
+ opts.enableSessionRecording = ds.sessionRecording === "true";
51
+ if (ds.sessionSampleRate)
52
+ recording.sessionSampleRate = Number(ds.sessionSampleRate);
53
+ if (ds.maskAllInputs)
54
+ recording.maskAllInputs = ds.maskAllInputs === "true";
55
+ if (ds.excludedPaths)
56
+ recording.excludedPaths = ds.excludedPaths.split(",").map((s) => s.trim());
57
+ if (Object.keys(recording).length > 0)
58
+ opts.sessionRecordingConfig = recording;
59
+ return opts;
60
+ }
61
+ function boot() {
62
+ if (typeof window === "undefined")
63
+ return;
64
+ init(readDataset());
65
+ }
66
+ if (typeof document !== "undefined" && document.readyState === "loading") {
67
+ document.addEventListener("DOMContentLoaded", boot, { once: true });
68
+ }
69
+ else {
70
+ boot();
71
+ }
@@ -0,0 +1,16 @@
1
+ import { type AnalyticsApi, type AnalyticsOptions, type JsonValue } from "@temps-sdk/analytics-core";
2
+ export * from "@temps-sdk/analytics-core";
3
+ /**
4
+ * Initialize Temps analytics. Safe to call multiple times — subsequent calls
5
+ * replace the previous instance and clean up its listeners.
6
+ */
7
+ export declare function init(options?: AnalyticsOptions): AnalyticsApi;
8
+ /** Returns the current instance, or throws if `init()` was not called. */
9
+ export declare function getAnalytics(): AnalyticsApi;
10
+ /** Convenience wrapper — throws if uninitialized. */
11
+ export declare function trackEvent(eventName: string, data?: Record<string, JsonValue>): Promise<void>;
12
+ /** Convenience wrapper — throws if uninitialized. */
13
+ export declare function trackPageview(): void;
14
+ /** Tear down the instance and remove listeners. */
15
+ export declare function destroy(): void;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACf,MAAM,2BAA2B,CAAC;AAEnC,cAAc,2BAA2B,CAAC;AAS1C;;;GAGG;AACH,wBAAgB,IAAI,CAAC,OAAO,GAAE,gBAAqB,GAAG,YAAY,CASjE;AAED,0EAA0E;AAC1E,wBAAgB,YAAY,IAAI,YAAY,CAO3C;AAED,qDAAqD;AACrD,wBAAgB,UAAU,CACxB,SAAS,EAAE,MAAM,EACjB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAC/B,OAAO,CAAC,IAAI,CAAC,CAEf;AAED,qDAAqD;AACrD,wBAAgB,aAAa,IAAI,IAAI,CAEpC;AAED,mDAAmD;AACnD,wBAAgB,OAAO,IAAI,IAAI,CAM9B"}
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ import { createAnalytics, } from "@temps-sdk/analytics-core";
2
+ export * from "@temps-sdk/analytics-core";
3
+ let instance = null;
4
+ /**
5
+ * Initialize Temps analytics. Safe to call multiple times — subsequent calls
6
+ * replace the previous instance and clean up its listeners.
7
+ */
8
+ export function init(options = {}) {
9
+ if (instance) {
10
+ instance.destroy();
11
+ }
12
+ instance = createAnalytics(options);
13
+ if (typeof window !== "undefined") {
14
+ window.temps = instance;
15
+ }
16
+ return instance;
17
+ }
18
+ /** Returns the current instance, or throws if `init()` was not called. */
19
+ export function getAnalytics() {
20
+ if (!instance) {
21
+ throw new Error("Temps analytics has not been initialized. Call init() or use the /auto entrypoint.");
22
+ }
23
+ return instance;
24
+ }
25
+ /** Convenience wrapper — throws if uninitialized. */
26
+ export function trackEvent(eventName, data) {
27
+ return getAnalytics().trackEvent(eventName, data);
28
+ }
29
+ /** Convenience wrapper — throws if uninitialized. */
30
+ export function trackPageview() {
31
+ getAnalytics().trackPageview();
32
+ }
33
+ /** Tear down the instance and remove listeners. */
34
+ export function destroy() {
35
+ instance?.destroy();
36
+ instance = null;
37
+ if (typeof window !== "undefined") {
38
+ delete window.temps;
39
+ }
40
+ }