also looking at this
feat(stx): one-line stx integration (plugins: ['@stacksjs/ts-analytics']) + fix library build
#123
11 files
+331
-13
| @@ -41,6 +41,31 @@ The validated pattern for adding ts-analytics to any site (docs sites included): | ||
| 41 | 41 | |
| 42 | 42 | 3. **Optional hardening**: append `?stealth=true` for the unlisted beacon path, or serve it first-party from your own domain — see the [ad-blocker resilience guide](docs/guide/tracking-script.md#ad-blocker-resilience-first-party-proxy). Error tracking without analytics: grab the DSN + SDK snippet from the same Settings page. |
| 43 | 43 | |
| 44 | ### stx framework (one config line) | |
| 45 | ||
| 46 | On an [stx](https://github.com/stacksjs/stx) app there's no `index.html` to paste a snippet into. Add the plugin and your app ID — that's the whole install (mirrors `nuxt-fathom`): | |
| 47 | ||
| 48 | ```ts | |
| 49 | // stx.config.ts | |
| 50 | export default { | |
| 51 | plugins: ['@stacksjs/ts-analytics'], | |
| 52 | analytics: { | |
| 53 | appId: 'YOUR_APP_ID', | |
| 54 | // apiEndpoint defaults to '/api/analytics' (events POST to `${apiEndpoint}/collect`) | |
| 55 | }, | |
| 56 | } | |
| 57 | ``` | |
| 58 | ||
| 59 | It injects the SPA-aware tracker before `</head>` on every server-rendered page — pageviews fire on the first load **and** on client-side (`<StxLink>`) navigations, since the tracker patches `history.pushState`. | |
| 60 | ||
| 61 | You can also pass options inline: `plugins: [['@stacksjs/ts-analytics', { appId: 'YOUR_APP_ID' }]]`. | |
| 62 | ||
| 63 | Notes: | |
| 64 | ||
| 65 | - **The plugin is the package's default export**, so `plugins: ['@stacksjs/ts-analytics']` resolves to it directly. Non-stx code should use the **named** exports (`import { Analytics } from '@stacksjs/ts-analytics'`). | |
| 66 | - **It owns the `analytics` config key** and is mutually exclusive with stx's native single-driver analytics — if you've set `analytics.driver` to a native provider (`fathom`, `plausible`, …), ts-analytics backs off (and logs a warning). | |
| 67 | - **Mount a collector** for `${apiEndpoint}/collect` (e.g. `mountAnalyticsRoutes` / `createBunRouter` from this package), or point `apiEndpoint` at a hosted ts-analytics instance — otherwise beacons hit a 404. | |
| 68 | ||
| 44 | 69 | ## Quick Start |
| 45 | 70 | |
| 46 | 71 | ### 1. Set Up the Analytics Store |