@hitkeep/tracker 2.12.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Pascale Beier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,164 @@
1
+ # @hitkeep/tracker
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@hitkeep/tracker.svg)](https://www.npmjs.com/package/@hitkeep/tracker)
4
+ [![license](https://img.shields.io/npm/l/@hitkeep/tracker.svg)](https://github.com/PascaleBeier/hitkeep/blob/main/LICENSE)
5
+ [![zero dependencies](https://img.shields.io/badge/dependencies-0-brightgreen.svg)](https://www.npmjs.com/package/@hitkeep/tracker?activeTab=dependencies)
6
+ [![types included](https://img.shields.io/badge/types-included-blue.svg)](https://www.npmjs.com/package/@hitkeep/tracker)
7
+
8
+ Typed, cookieless web analytics tracking for **React, Vue, Angular, Astro**, and any other bundler-based frontend — a privacy-friendly Google Analytics alternative that stores no cookies and needs no consent banner for basic measurement.
9
+
10
+ This is the official browser SDK for [HitKeep](https://hitkeep.com), a self-hostable, GDPR-friendly analytics platform that runs as a single Go binary. The package bundles the same tracker that ships as HitKeep's `hk.js` snippet — compiled from the same source — as a fully typed ES module.
11
+
12
+ - **Cookieless by default** — no cookies, no cross-site identifiers, no consent banner for basic analytics
13
+ - **Framework-native** — no `<script>` tag, no `window.hk` casts, full TypeScript types
14
+ - **Automatic tracking** — pageviews, SPA route changes, outbound links, file downloads, form submits
15
+ - **Typed e-commerce events** — `view_item`, `add_to_cart`, `begin_checkout`, `purchase`
16
+ - **Tiny and self-contained** — ~3.7 kB gzipped, zero runtime dependencies, ESM + CJS
17
+
18
+ ## Install
19
+
20
+ ```sh
21
+ npm install @hitkeep/tracker
22
+ ```
23
+
24
+ ## Quick start
25
+
26
+ ```ts
27
+ import { init, track } from '@hitkeep/tracker';
28
+
29
+ init({ host: 'https://your-hitkeep.example.com' });
30
+
31
+ track('signup_clicked', { plan: 'pro' });
32
+ ```
33
+
34
+ `host` is the origin (or path prefix) your HitKeep instance serves the tracker from — the same URL the `hk.js` snippet would load from, including [custom tracking domains](https://hitkeep.com/guides/tracking/custom-tracking-domains/). Pageviews are captured automatically, including history-based SPA navigations, so no router wiring is required.
35
+
36
+ ## Framework examples
37
+
38
+ ### React and Next.js
39
+
40
+ ```tsx
41
+ 'use client';
42
+
43
+ import { useEffect } from 'react';
44
+ import { cleanup, init } from '@hitkeep/tracker';
45
+
46
+ export function Analytics() {
47
+ useEffect(() => {
48
+ init({ host: 'https://your-hitkeep.example.com' });
49
+ return cleanup;
50
+ }, []);
51
+
52
+ return null;
53
+ }
54
+ ```
55
+
56
+ ### Vue and Nuxt
57
+
58
+ ```ts
59
+ import { createApp } from 'vue';
60
+ import { init } from '@hitkeep/tracker';
61
+ import App from './App.vue';
62
+
63
+ init({ host: 'https://your-hitkeep.example.com' });
64
+
65
+ createApp(App).mount('#app');
66
+ ```
67
+
68
+ ### Angular
69
+
70
+ ```ts
71
+ import { ApplicationConfig, provideAppInitializer } from '@angular/core';
72
+ import { init } from '@hitkeep/tracker';
73
+
74
+ export const appConfig: ApplicationConfig = {
75
+ providers: [provideAppInitializer(() => init({ host: 'https://your-hitkeep.example.com' }))]
76
+ };
77
+ ```
78
+
79
+ ### Astro
80
+
81
+ ```astro
82
+ <script>
83
+ import { init } from '@hitkeep/tracker';
84
+
85
+ init({ host: 'https://your-hitkeep.example.com' });
86
+ </script>
87
+ ```
88
+
89
+ Full walkthroughs for each framework, including Next.js App Router and Astro view transitions, are in the [npm package guide](https://hitkeep.com/guides/tracking/npm-package/).
90
+
91
+ ## API
92
+
93
+ | Export | Purpose |
94
+ | :--- | :--- |
95
+ | `init(config)` | Start the tracker and return a handle. Idempotent. |
96
+ | `track(name, properties?)` | Record a custom event. Calls before `init` are queued. |
97
+ | `trackPageview()` | Send a manual pageview. |
98
+ | `cleanup()` | Remove listeners and allow re-initialization. |
99
+ | `blockTrackingForMe()` / `enableTrackingForMe()` / `isTrackingEnabled()` | Visitor opt-out controls. |
100
+ | `trackViewItem` / `trackAddToCart` / `trackBeginCheckout` / `trackPurchase` | Typed [e-commerce events](https://hitkeep.com/guides/analytics/ecommerce/). |
101
+
102
+ ### Configuration
103
+
104
+ ```ts
105
+ init({
106
+ host: 'https://your-hitkeep.example.com', // required
107
+ autoCapturePageviews: true, // send a pageview on init
108
+ autoTrackSpaNavigation: true, // pageviews on pushState/replaceState/popstate
109
+ outboundLinks: true, // outbound_click events
110
+ fileDownloads: true, // file_download events
111
+ formSubmissions: true, // form_submit events
112
+ webVitals: false, // load the Web Vitals bundle from `${host}/hk-vitals.js`
113
+ useBeacon: true, // prefer navigator.sendBeacon
114
+ respectDoNotTrack: true, // drop tracking when DNT is enabled
115
+ captureOnLocalhost: false, // localhost is blocked by default
116
+ bindToWindow: true // expose window.hk.event for snippet-compatible callers
117
+ });
118
+ ```
119
+
120
+ Each option maps to a `hk.js` data attribute where one exists — see the [tracker architecture guide](https://hitkeep.com/guides/tracking/tracker-architecture/) for the delivery, retry, and storage behavior shared by both.
121
+
122
+ ### E-commerce
123
+
124
+ ```ts
125
+ import { trackPurchase } from '@hitkeep/tracker';
126
+
127
+ trackPurchase({
128
+ transaction_id: 'tx-1042',
129
+ value: 49.9,
130
+ currency: 'EUR',
131
+ items: [{ item_id: 'sku-1', item_name: 'Starter Plan', quantity: 1, price: 49.9 }]
132
+ });
133
+ ```
134
+
135
+ ### Visitor opt-out
136
+
137
+ This package bundles the tracker into your application, so content blockers do not filter it. Offer visitors an explicit opt-out:
138
+
139
+ ```ts
140
+ import { blockTrackingForMe, enableTrackingForMe, isTrackingEnabled } from '@hitkeep/tracker';
141
+ ```
142
+
143
+ The opt-out persists in `localStorage` under `hk_ignore` and is honored by both this package and the `hk.js` snippet.
144
+
145
+ ## What you need to run it
146
+
147
+ The package sends data to a HitKeep instance. You can [self-host HitKeep](https://hitkeep.com/guides/installation/) as a single binary, Docker image, or Helm chart, or use [HitKeep Cloud](https://cloud.hitkeep.eu/signup?plan=free&billing=monthly&utm_source=npm&utm_medium=referral&utm_campaign=tracker_package). HitKeep is open source under the MIT license — the source for this package lives in [PascaleBeier/hitkeep](https://github.com/PascaleBeier/hitkeep).
148
+
149
+ ## Documentation
150
+
151
+ - [NPM package guide](https://hitkeep.com/guides/tracking/npm-package/) — framework integration, full API reference, differences from the snippet
152
+ - [Tracker architecture](https://hitkeep.com/guides/tracking/tracker-architecture/) — delivery, retries, SPA handling, storage boundaries
153
+ - [Custom events](https://hitkeep.com/guides/tracking/custom-events/) — event naming and property conventions
154
+ - [Automatic events](https://hitkeep.com/guides/tracking/automatic-events/) — outbound clicks, downloads, form submits
155
+ - [E-commerce analytics](https://hitkeep.com/guides/analytics/ecommerce/) — purchase funnel tracking
156
+ - [Self-hosting HitKeep](https://hitkeep.com/guides/installation/) — binary, Docker Compose, Kubernetes
157
+
158
+ ## Versioning
159
+
160
+ This package is versioned in lockstep with HitKeep itself and published with every HitKeep release. Keep the major version aligned with your HitKeep instance.
161
+
162
+ ## License
163
+
164
+ MIT © Pascale Beier — see [LICENSE](https://github.com/PascaleBeier/hitkeep/blob/main/LICENSE).
@@ -0,0 +1,83 @@
1
+ export type EventProperties = Record<string, unknown>;
2
+ interface HitKeepEvent {
3
+ name: string;
4
+ properties: EventProperties;
5
+ }
6
+ type EventSender = (name: string, properties?: EventProperties) => void;
7
+ export interface TrackerOptions {
8
+ collectDnt: boolean;
9
+ disableBeacon: boolean;
10
+ disableSpaTracking: boolean;
11
+ disableOutboundTracking: boolean;
12
+ disableDownloadTracking: boolean;
13
+ disableFormTracking: boolean;
14
+ enableWebVitals: boolean;
15
+ trackerSource: string;
16
+ trackerVersion: string;
17
+ }
18
+ export interface TrackerEndpoints {
19
+ pageEndpoint: string;
20
+ eventEndpoint: string;
21
+ webVitalsEndpoint: string;
22
+ /** Resolved only when Web Vitals are enabled; `null` keeps the split bundle unloaded. */
23
+ webVitalsBundleUrl: string | null;
24
+ }
25
+ export interface TrackerRuntimeConfig extends TrackerOptions, TrackerEndpoints {
26
+ capturePageviews: boolean;
27
+ captureOnLocalhost: boolean;
28
+ bindToWindow: boolean;
29
+ }
30
+ export interface TrackerHandle {
31
+ track: EventSender;
32
+ trackPageview: () => void;
33
+ cleanup: () => void;
34
+ }
35
+ export type HitKeepWindow = Window & typeof globalThis & {
36
+ hk?: {
37
+ event?: EventSender;
38
+ _bootstrapped?: boolean;
39
+ _webVitals?: WebVitalsTrackerContext;
40
+ _webVitalsLoaded?: boolean;
41
+ };
42
+ };
43
+ export interface WebVitalsPayload {
44
+ n: string;
45
+ v: number;
46
+ p: string;
47
+ nt?: string;
48
+ mid?: string;
49
+ sid: string;
50
+ pid: string;
51
+ tsrc: string;
52
+ tv: string;
53
+ ua?: string;
54
+ }
55
+ export interface WebVitalsTrackerContext {
56
+ emit: (payload: WebVitalsPayload) => void;
57
+ getPath: () => string;
58
+ sessionId: string;
59
+ pageId: () => string;
60
+ trackerSource: string;
61
+ trackerVersion: string;
62
+ userAgent: string;
63
+ }
64
+ export declare function readTrackerOptions(scriptEl: Element): TrackerOptions;
65
+ export declare function isTrackerBlocked(hostname: string, userAgent: string, doNotTrack: string | null | undefined, collectDnt: boolean, captureOnLocalhost?: boolean): boolean;
66
+ export declare function isTrackingOptedOut(win: Window): boolean;
67
+ export declare function setTrackingOptOut(win: Window, optedOut: boolean): void;
68
+ export declare function sanitizeTrackedUrl(rawUrl: string, baseUrl: string | URL): URL | null;
69
+ export declare function sanitizeTrackedPath(url: URL): string;
70
+ export declare function resolveTrackerUrl(scriptUrl: URL, relativePath: string): string;
71
+ export declare function resolveWebVitalsBundleUrl(scriptUrl: URL, bundleName?: string): string;
72
+ /**
73
+ * Single source of truth for the tracker's wire paths, shared by the `hk.js` snippet
74
+ * (resolving against its own script URL) and the npm package (resolving against `host`).
75
+ */
76
+ export declare function resolveTrackerEndpoints(baseUrl: URL, enableWebVitals: boolean): TrackerEndpoints;
77
+ export declare function extractDownloadExtension(url: URL): string | null;
78
+ export declare function hasExplicitTrackingTag(element: Element | null): boolean;
79
+ export declare function classifyLinkEvent(link: HTMLAnchorElement | HTMLAreaElement, currentUrl: URL): HitKeepEvent | null;
80
+ export declare function classifyFormSubmit(form: HTMLFormElement, currentUrl: URL, submitter?: Element | null): HitKeepEvent | null;
81
+ export declare function createTracker(win: HitKeepWindow, config: TrackerRuntimeConfig): TrackerHandle | null;
82
+ export declare function bootstrapTracker(win?: HitKeepWindow): void;
83
+ export {};
package/dist/core.d.ts ADDED
@@ -0,0 +1,83 @@
1
+ export type EventProperties = Record<string, unknown>;
2
+ interface HitKeepEvent {
3
+ name: string;
4
+ properties: EventProperties;
5
+ }
6
+ type EventSender = (name: string, properties?: EventProperties) => void;
7
+ export interface TrackerOptions {
8
+ collectDnt: boolean;
9
+ disableBeacon: boolean;
10
+ disableSpaTracking: boolean;
11
+ disableOutboundTracking: boolean;
12
+ disableDownloadTracking: boolean;
13
+ disableFormTracking: boolean;
14
+ enableWebVitals: boolean;
15
+ trackerSource: string;
16
+ trackerVersion: string;
17
+ }
18
+ export interface TrackerEndpoints {
19
+ pageEndpoint: string;
20
+ eventEndpoint: string;
21
+ webVitalsEndpoint: string;
22
+ /** Resolved only when Web Vitals are enabled; `null` keeps the split bundle unloaded. */
23
+ webVitalsBundleUrl: string | null;
24
+ }
25
+ export interface TrackerRuntimeConfig extends TrackerOptions, TrackerEndpoints {
26
+ capturePageviews: boolean;
27
+ captureOnLocalhost: boolean;
28
+ bindToWindow: boolean;
29
+ }
30
+ export interface TrackerHandle {
31
+ track: EventSender;
32
+ trackPageview: () => void;
33
+ cleanup: () => void;
34
+ }
35
+ export type HitKeepWindow = Window & typeof globalThis & {
36
+ hk?: {
37
+ event?: EventSender;
38
+ _bootstrapped?: boolean;
39
+ _webVitals?: WebVitalsTrackerContext;
40
+ _webVitalsLoaded?: boolean;
41
+ };
42
+ };
43
+ export interface WebVitalsPayload {
44
+ n: string;
45
+ v: number;
46
+ p: string;
47
+ nt?: string;
48
+ mid?: string;
49
+ sid: string;
50
+ pid: string;
51
+ tsrc: string;
52
+ tv: string;
53
+ ua?: string;
54
+ }
55
+ export interface WebVitalsTrackerContext {
56
+ emit: (payload: WebVitalsPayload) => void;
57
+ getPath: () => string;
58
+ sessionId: string;
59
+ pageId: () => string;
60
+ trackerSource: string;
61
+ trackerVersion: string;
62
+ userAgent: string;
63
+ }
64
+ export declare function readTrackerOptions(scriptEl: Element): TrackerOptions;
65
+ export declare function isTrackerBlocked(hostname: string, userAgent: string, doNotTrack: string | null | undefined, collectDnt: boolean, captureOnLocalhost?: boolean): boolean;
66
+ export declare function isTrackingOptedOut(win: Window): boolean;
67
+ export declare function setTrackingOptOut(win: Window, optedOut: boolean): void;
68
+ export declare function sanitizeTrackedUrl(rawUrl: string, baseUrl: string | URL): URL | null;
69
+ export declare function sanitizeTrackedPath(url: URL): string;
70
+ export declare function resolveTrackerUrl(scriptUrl: URL, relativePath: string): string;
71
+ export declare function resolveWebVitalsBundleUrl(scriptUrl: URL, bundleName?: string): string;
72
+ /**
73
+ * Single source of truth for the tracker's wire paths, shared by the `hk.js` snippet
74
+ * (resolving against its own script URL) and the npm package (resolving against `host`).
75
+ */
76
+ export declare function resolveTrackerEndpoints(baseUrl: URL, enableWebVitals: boolean): TrackerEndpoints;
77
+ export declare function extractDownloadExtension(url: URL): string | null;
78
+ export declare function hasExplicitTrackingTag(element: Element | null): boolean;
79
+ export declare function classifyLinkEvent(link: HTMLAnchorElement | HTMLAreaElement, currentUrl: URL): HitKeepEvent | null;
80
+ export declare function classifyFormSubmit(form: HTMLFormElement, currentUrl: URL, submitter?: Element | null): HitKeepEvent | null;
81
+ export declare function createTracker(win: HitKeepWindow, config: TrackerRuntimeConfig): TrackerHandle | null;
82
+ export declare function bootstrapTracker(win?: HitKeepWindow): void;
83
+ export {};