@temps-sdk/svelte-analytics 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,84 @@
1
+ # @temps-sdk/svelte-analytics
2
+
3
+ Temps analytics for Svelte 4 and 5 — stores, actions, and helpers.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @temps-sdk/svelte-analytics
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```ts
14
+ // src/routes/+layout.ts (or src/main.ts)
15
+ import { initTempsAnalytics } from "@temps-sdk/svelte-analytics";
16
+
17
+ initTempsAnalytics({
18
+ basePath: "/api/_temps",
19
+ enableSessionRecording: false,
20
+ });
21
+ ```
22
+
23
+ ## Track events
24
+
25
+ ```svelte
26
+ <script>
27
+ import { getTempsAnalytics } from "@temps-sdk/svelte-analytics";
28
+ const analytics = getTempsAnalytics();
29
+ </script>
30
+
31
+ <button on:click={() => analytics.trackEvent("cta_click", { variant: "blue" })}>
32
+ Track me
33
+ </button>
34
+
35
+ <!-- or declaratively -->
36
+ <button temps-event-name="cta_click" temps-data-section="hero">Get started</button>
37
+ ```
38
+
39
+ ## Track visibility (Svelte action)
40
+
41
+ ```svelte
42
+ <script>
43
+ import { trackVisibility } from "@temps-sdk/svelte-analytics";
44
+ </script>
45
+
46
+ <section use:trackVisibility={{ eventName: "pricing_viewed", threshold: 0.75 }}>
47
+ Pricing Plans
48
+ </section>
49
+ ```
50
+
51
+ ## Engagement store
52
+
53
+ ```svelte
54
+ <script>
55
+ import { engagementStore } from "@temps-sdk/svelte-analytics";
56
+ const engagement = engagementStore({ heartbeatInterval: 15000 });
57
+ </script>
58
+
59
+ <p>Engaged: {$engagement.engagement_time_seconds}s</p>
60
+ ```
61
+
62
+ ## Session recording
63
+
64
+ ```svelte
65
+ <script>
66
+ import { sessionRecordingStore } from "@temps-sdk/svelte-analytics";
67
+ const recording = sessionRecordingStore(false);
68
+ </script>
69
+
70
+ <button on:click={recording.toggle}>
71
+ Recording: {$recording ? "ON" : "OFF"}
72
+ </button>
73
+ ```
74
+
75
+ ## API parity
76
+
77
+ | React | Svelte |
78
+ |---|---|
79
+ | `<TempsAnalyticsProvider>` | `initTempsAnalytics(options)` at app root |
80
+ | `useTempsAnalytics()` | `getTempsAnalytics()` / `analyticsStore` |
81
+ | `useTrackEvent()` | `getTempsAnalytics().trackEvent(...)` |
82
+ | `useScrollVisibility()` | `use:trackVisibility={{ ... }}` |
83
+ | `useEngagementTracking()` | `engagementStore({ ... })` |
84
+ | `useSessionRecordingControl()` | `sessionRecordingStore()` |
@@ -0,0 +1,23 @@
1
+ import type { Action } from "svelte/action";
2
+ import type { JsonValue } from "@temps-sdk/analytics-core";
3
+ export interface TrackVisibilityOptions {
4
+ eventName?: string;
5
+ eventData?: Record<string, JsonValue>;
6
+ threshold?: number;
7
+ root?: Element | null;
8
+ rootMargin?: string;
9
+ once?: boolean;
10
+ enabled?: boolean;
11
+ }
12
+ /**
13
+ * Svelte action that fires an analytics event the first time (or every time,
14
+ * if `once: false`) a node scrolls into view.
15
+ *
16
+ * ```svelte
17
+ * <section use:trackVisibility={{ eventName: "pricing_viewed" }}>
18
+ * Pricing
19
+ * </section>
20
+ * ```
21
+ */
22
+ export declare const trackVisibility: Action<HTMLElement, TrackVisibilityOptions | undefined>;
23
+ //# sourceMappingURL=actions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAG3D,MAAM,WAAW,sBAAsB;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,WAAW,EAAE,sBAAsB,GAAG,SAAS,CAqDnF,CAAC"}
@@ -0,0 +1,48 @@
1
+ import { getTempsAnalytics } from "./client";
2
+ /**
3
+ * Svelte action that fires an analytics event the first time (or every time,
4
+ * if `once: false`) a node scrolls into view.
5
+ *
6
+ * ```svelte
7
+ * <section use:trackVisibility={{ eventName: "pricing_viewed" }}>
8
+ * Pricing
9
+ * </section>
10
+ * ```
11
+ */
12
+ export const trackVisibility = (node, params) => {
13
+ let opts = params ?? {};
14
+ let hasTracked = false;
15
+ let observer = null;
16
+ const start = () => {
17
+ if (opts.enabled === false)
18
+ return;
19
+ if (typeof IntersectionObserver === "undefined")
20
+ return;
21
+ const { eventName = "component_visible", eventData, threshold = 0.5, root = null, rootMargin = "0px", once = true, } = opts;
22
+ observer = new IntersectionObserver((entries) => {
23
+ entries.forEach((entry) => {
24
+ if (entry.isIntersecting && (!once || !hasTracked)) {
25
+ void getTempsAnalytics().trackEvent(eventName, eventData);
26
+ hasTracked = true;
27
+ }
28
+ });
29
+ }, { root, rootMargin, threshold });
30
+ observer.observe(node);
31
+ };
32
+ const stop = () => {
33
+ observer?.disconnect();
34
+ observer = null;
35
+ };
36
+ start();
37
+ return {
38
+ update(next) {
39
+ opts = next ?? {};
40
+ hasTracked = false;
41
+ stop();
42
+ start();
43
+ },
44
+ destroy() {
45
+ stop();
46
+ },
47
+ };
48
+ };
@@ -0,0 +1,20 @@
1
+ import { type Readable } from "svelte/store";
2
+ import { type AnalyticsApi, type AnalyticsOptions } from "@temps-sdk/analytics-core";
3
+ /**
4
+ * Initialize Temps analytics. Call once at app startup (e.g. in your root
5
+ * layout `.svelte` file or a `+layout.ts`).
6
+ *
7
+ * ```ts
8
+ * import { initTempsAnalytics } from "@temps-sdk/svelte-analytics";
9
+ * initTempsAnalytics({ basePath: "/api/_temps" });
10
+ * ```
11
+ */
12
+ export declare function initTempsAnalytics(options?: AnalyticsOptions): AnalyticsApi;
13
+ /** Returns the current instance, throws if not initialized. */
14
+ export declare function getTempsAnalytics(): AnalyticsApi;
15
+ /**
16
+ * Svelte readable store exposing the analytics instance once initialized.
17
+ * Emits `null` until `initTempsAnalytics()` is called.
18
+ */
19
+ export declare const analyticsStore: Readable<AnalyticsApi | null>;
20
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACtB,MAAM,2BAA2B,CAAC;AAInC;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,gBAAqB,GAAG,YAAY,CAI/E;AAED,+DAA+D;AAC/D,wBAAgB,iBAAiB,IAAI,YAAY,CAOhD;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,YAAY,GAAG,IAAI,CAYxD,CAAC"}
package/dist/client.js ADDED
@@ -0,0 +1,39 @@
1
+ import { readable } from "svelte/store";
2
+ import { createAnalytics, } from "@temps-sdk/analytics-core";
3
+ let instance = null;
4
+ /**
5
+ * Initialize Temps analytics. Call once at app startup (e.g. in your root
6
+ * layout `.svelte` file or a `+layout.ts`).
7
+ *
8
+ * ```ts
9
+ * import { initTempsAnalytics } from "@temps-sdk/svelte-analytics";
10
+ * initTempsAnalytics({ basePath: "/api/_temps" });
11
+ * ```
12
+ */
13
+ export function initTempsAnalytics(options = {}) {
14
+ if (instance)
15
+ instance.destroy();
16
+ instance = createAnalytics(options);
17
+ return instance;
18
+ }
19
+ /** Returns the current instance, throws if not initialized. */
20
+ export function getTempsAnalytics() {
21
+ if (!instance) {
22
+ throw new Error("Temps analytics has not been initialized. Call initTempsAnalytics() first.");
23
+ }
24
+ return instance;
25
+ }
26
+ /**
27
+ * Svelte readable store exposing the analytics instance once initialized.
28
+ * Emits `null` until `initTempsAnalytics()` is called.
29
+ */
30
+ export const analyticsStore = readable(null, (set) => {
31
+ set(instance);
32
+ const id = setInterval(() => {
33
+ if (instance) {
34
+ set(instance);
35
+ clearInterval(id);
36
+ }
37
+ }, 50);
38
+ return () => clearInterval(id);
39
+ });
@@ -0,0 +1,10 @@
1
+ import { type Readable } from "svelte/store";
2
+ import { type EngagementData, type EngagementTrackerOptions } from "@temps-sdk/analytics-core";
3
+ export type EngagementStore = Readable<EngagementData>;
4
+ /**
5
+ * Returns a readable store that emits engagement data on every heartbeat.
6
+ * The underlying tracker is created on first subscription and destroyed when
7
+ * the last subscriber unsubscribes.
8
+ */
9
+ export declare function engagementStore(options?: Omit<EngagementTrackerOptions, "onHeartbeat" | "onPageLeave">): EngagementStore;
10
+ //# sourceMappingURL=engagement.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engagement.d.ts","sourceRoot":"","sources":["../src/engagement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,wBAAwB,EAC9B,MAAM,2BAA2B,CAAC;AAEnC,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAC,cAAc,CAAC,CAAC;AAWvD;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,OAAO,GAAE,IAAI,CAAC,wBAAwB,EAAE,aAAa,GAAG,aAAa,CAAM,GAC1E,eAAe,CAUjB"}
@@ -0,0 +1,27 @@
1
+ import { readable } from "svelte/store";
2
+ import { EngagementTracker, } from "@temps-sdk/analytics-core";
3
+ const EMPTY = {
4
+ engagement_time_seconds: 0,
5
+ total_time_seconds: 0,
6
+ heartbeat_count: 0,
7
+ is_engaged: false,
8
+ is_visible: true,
9
+ time_since_last_activity: 0,
10
+ };
11
+ /**
12
+ * Returns a readable store that emits engagement data on every heartbeat.
13
+ * The underlying tracker is created on first subscription and destroyed when
14
+ * the last subscriber unsubscribes.
15
+ */
16
+ export function engagementStore(options = {}) {
17
+ return readable(EMPTY, (set) => {
18
+ if (typeof window === "undefined")
19
+ return;
20
+ const tracker = new EngagementTracker({
21
+ ...options,
22
+ onHeartbeat: (data) => set(data),
23
+ onPageLeave: (data) => set(data),
24
+ });
25
+ return () => tracker.destroy();
26
+ });
27
+ }
@@ -0,0 +1,6 @@
1
+ export * from "@temps-sdk/analytics-core";
2
+ export { initTempsAnalytics, getTempsAnalytics, analyticsStore } from "./client";
3
+ export { trackVisibility, type TrackVisibilityOptions } from "./actions";
4
+ export { engagementStore, type EngagementStore } from "./engagement";
5
+ export { sessionRecordingStore, type SessionRecordingStore } from "./sessionRecording";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AACjF,OAAO,EAAE,eAAe,EAAE,KAAK,sBAAsB,EAAE,MAAM,WAAW,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,KAAK,qBAAqB,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "@temps-sdk/analytics-core";
2
+ export { initTempsAnalytics, getTempsAnalytics, analyticsStore } from "./client";
3
+ export { trackVisibility } from "./actions";
4
+ export { engagementStore } from "./engagement";
5
+ export { sessionRecordingStore } from "./sessionRecording";
@@ -0,0 +1,12 @@
1
+ import { type Writable } from "svelte/store";
2
+ export interface SessionRecordingStore extends Writable<boolean> {
3
+ enable: () => void;
4
+ disable: () => void;
5
+ toggle: () => void;
6
+ }
7
+ /**
8
+ * Svelte-native mirror of the React `useSessionRecordingControl` hook.
9
+ * The returned store is a writable boolean + convenience helpers.
10
+ */
11
+ export declare function sessionRecordingStore(defaultEnabled?: boolean): SessionRecordingStore;
12
+ //# sourceMappingURL=sessionRecording.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessionRecording.d.ts","sourceRoot":"","sources":["../src/sessionRecording.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAKvD,MAAM,WAAW,qBAAsB,SAAQ,QAAQ,CAAC,OAAO,CAAC;IAC9D,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAcD;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,cAAc,UAAQ,GAAG,qBAAqB,CAwCnF"}
@@ -0,0 +1,61 @@
1
+ import { writable } from "svelte/store";
2
+ import { getTempsAnalytics } from "./client";
3
+ const STORAGE_KEY = "temps_session_recording_enabled";
4
+ function readInitial(defaultEnabled) {
5
+ if (typeof localStorage === "undefined")
6
+ return defaultEnabled;
7
+ const stored = localStorage.getItem(STORAGE_KEY);
8
+ if (stored === null)
9
+ return defaultEnabled;
10
+ return stored === "true";
11
+ }
12
+ function persist(value) {
13
+ if (typeof localStorage === "undefined")
14
+ return;
15
+ localStorage.setItem(STORAGE_KEY, String(value));
16
+ }
17
+ /**
18
+ * Svelte-native mirror of the React `useSessionRecordingControl` hook.
19
+ * The returned store is a writable boolean + convenience helpers.
20
+ */
21
+ export function sessionRecordingStore(defaultEnabled = false) {
22
+ const initial = readInitial(defaultEnabled);
23
+ const store = writable(initial);
24
+ const apply = (value) => {
25
+ const analytics = getTempsAnalytics();
26
+ if (value)
27
+ analytics.enableSessionRecording();
28
+ else
29
+ analytics.disableSessionRecording();
30
+ persist(value);
31
+ };
32
+ return {
33
+ subscribe: store.subscribe,
34
+ set(value) {
35
+ store.set(value);
36
+ apply(value);
37
+ },
38
+ update(updater) {
39
+ store.update((prev) => {
40
+ const next = updater(prev);
41
+ apply(next);
42
+ return next;
43
+ });
44
+ },
45
+ enable() {
46
+ store.set(true);
47
+ apply(true);
48
+ },
49
+ disable() {
50
+ store.set(false);
51
+ apply(false);
52
+ },
53
+ toggle() {
54
+ store.update((prev) => {
55
+ const next = !prev;
56
+ apply(next);
57
+ return next;
58
+ });
59
+ },
60
+ };
61
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@temps-sdk/svelte-analytics",
3
+ "version": "0.0.1",
4
+ "description": "Temps analytics for Svelte — stores, actions, and helpers for events, Web Vitals, engagement, and session replay.",
5
+ "private": false,
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "scripts": {
20
+ "build:js": "tsc -p tsconfig.json",
21
+ "build:types": "tsc -p tsconfig.types.json",
22
+ "build": "bun run build:js && bun run build:types",
23
+ "clean": "rm -rf dist",
24
+ "prepublishOnly": "bun run clean && bun run build"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "peerDependencies": {
30
+ "svelte": "^4.2.20 || >=5.53.5"
31
+ },
32
+ "dependencies": {
33
+ "@temps-sdk/analytics-core": "0.0.2"
34
+ },
35
+ "devDependencies": {
36
+ "svelte": "^5.53.5",
37
+ "typescript": "^5.5.4"
38
+ },
39
+ "sideEffects": false
40
+ }