@stacksjs/analytics 0.75.15 → 0.75.16
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 +28 -5
- package/dist/registry.d.ts +3 -3
- package/dist/registry.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -12,16 +12,39 @@ This package integrates analytics features into your application.
|
|
|
12
12
|
bun install -d @stacksjs/analytics
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
Render the driver selected in `config/analytics.ts`:
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
|
-
import {
|
|
18
|
+
import { generateAnalyticsScript } from '@stacksjs/analytics'
|
|
19
|
+
import { analytics } from '@stacksjs/config'
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
// property: 'value'
|
|
22
|
-
// })
|
|
21
|
+
const head = generateAnalyticsScript(analytics)
|
|
23
22
|
```
|
|
24
23
|
|
|
24
|
+
AnalyticsHQ is the default driver. It emits no tags until
|
|
25
|
+
`drivers.analyticshq.siteId` is set, so a new application never sends a broken
|
|
26
|
+
or unidentified event. Name another driver explicitly to use it instead.
|
|
27
|
+
|
|
28
|
+
Remote analytics can be limited to explicit application environments:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import type { AnalyticsConfig } from '@stacksjs/types'
|
|
32
|
+
|
|
33
|
+
export default {
|
|
34
|
+
driver: 'analyticshq',
|
|
35
|
+
environments: ['production', 'staging'],
|
|
36
|
+
drivers: {
|
|
37
|
+
analyticshq: { siteId: 'YOUR_APP_ID' },
|
|
38
|
+
},
|
|
39
|
+
} satisfies AnalyticsConfig
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Stacks resolves `APP_ENV` before selecting or initializing the driver. An
|
|
43
|
+
excluded environment emits no tracking tag. `enabled: false` always wins, an
|
|
44
|
+
empty allowlist disables every environment, and local environments can opt in
|
|
45
|
+
by naming their exact label. Leaving `environments` unset preserves existing
|
|
46
|
+
application behavior.
|
|
47
|
+
|
|
25
48
|
Learn more in the docs.
|
|
26
49
|
|
|
27
50
|
## 🧪 Testing
|
package/dist/registry.d.ts
CHANGED
|
@@ -7,9 +7,9 @@ export declare function isAnalyticsDriver(value: unknown): value is AnalyticsDri
|
|
|
7
7
|
/**
|
|
8
8
|
* Resolve the configured driver's head tags.
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* missing the config it needs.
|
|
10
|
+
* Defaults to AnalyticsHQ when no driver is named. Returns an empty array while
|
|
11
|
+
* that default has no site id. Throws when a driver is unknown, or when an
|
|
12
|
+
* explicitly selected third-party driver is missing the config it needs.
|
|
13
13
|
*/
|
|
14
14
|
export declare function getAnalyticsHead(config: AnalyticsConfig): AnalyticsHeadTag[];
|
|
15
15
|
/**
|
package/dist/registry.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{initializeIntegration}from"@stacksjs/env";import{getAnalyticsHqHead}from"./drivers/analyticshq";import{getFathomAnalyticsHead}from"./drivers/fathom";import{getGoogleAnalyticsHead}from"./drivers/google-analytics";import{getPlausibleAnalyticsHead}from"./drivers/plausible";import{getSelfHostedAnalyticsHead}from"./drivers/self-hosted";import{renderHeadTags}from"./drivers/shared";export const ANALYTICS_DRIVERS=["google-analytics","fathom","plausible","self-hosted","analyticshq"];export function isAnalyticsDriver(value){return typeof value==="string"&&ANALYTICS_DRIVERS.includes(value)}function misconfigured(driver,key,field){return Error(`Analytics driver "${driver}" is selected but not configured: set \`drivers.${key}.${field}\` in config/analytics.ts, or change \`driver\`.`)}export function getAnalyticsHead(config){const driver=config.driver??"analyticshq";if(config.enabled===!1)return[];if(config.environments!==void 0){const result=initializeIntegration(config,()=>resolveAnalyticsHead(config,driver));return result.initialized?result.value:[]}return resolveAnalyticsHead(config,driver)}function resolveAnalyticsHead(config,driver){if(!isAnalyticsDriver(driver))throw Error(`Unknown analytics driver "${String(driver)}". Available drivers: ${ANALYTICS_DRIVERS.join(", ")}.`);switch(driver){case"google-analytics":{const options=config.drivers?.googleAnalytics;if(!options?.trackingId)throw misconfigured(driver,"googleAnalytics","trackingId");return getGoogleAnalyticsHead(options)}case"fathom":{const options=config.drivers?.fathom;if(!options?.siteId)throw misconfigured(driver,"fathom","siteId");return getFathomAnalyticsHead(options)}case"plausible":{const options=config.drivers?.plausible;if(!options?.domain)throw misconfigured(driver,"plausible","domain");return getPlausibleAnalyticsHead(options)}case"self-hosted":{const options=config.drivers?.selfHosted;if(!options?.siteId)throw misconfigured(driver,"selfHosted","siteId");if(!options.apiEndpoint)throw misconfigured(driver,"selfHosted","apiEndpoint");return getSelfHostedAnalyticsHead(options)}case"analyticshq":{const options=config.drivers?.analyticshq;if(!options?.siteId)return[];return getAnalyticsHqHead(options)}default:throw Error(`Analytics driver "${String(driver)}" has no implementation.`)}}export function generateAnalyticsScript(config){const tags=getAnalyticsHead(config);if(!tags.length)return"";return renderHeadTags(tags)}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/analytics",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.16",
|
|
6
6
|
"description": "Stacks Analytics. Privacy-friendly.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -51,9 +51,9 @@
|
|
|
51
51
|
"prepublishOnly": "bun run build"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@stacksjs/database": "0.75.
|
|
55
|
-
"@stacksjs/env": "0.75.
|
|
56
|
-
"@stacksjs/types": "0.75.
|
|
54
|
+
"@stacksjs/database": "0.75.16",
|
|
55
|
+
"@stacksjs/env": "0.75.16",
|
|
56
|
+
"@stacksjs/types": "0.75.16"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"better-dx": "^0.2.24"
|