@stacksjs/analytics 0.75.515 → 0.75.518
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/dist/capture.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{db,sqlDateTime}from"@stacksjs/database/runtime";const ASSET_EXT=/\.(?:css|js|mjs|map|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|eot|txt|xml|json|webmanifest|mp[34]|webm|pdf|zip)$/i;export function isPageviewRequest(method,pathname,headers){if(method!=="GET")return
|
|
1
|
+
import{db,sqlDateTime}from"@stacksjs/database/runtime";const ASSET_EXT=/\.(?:css|js|mjs|map|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|eot|txt|xml|json|webmanifest|mp[34]|webm|pdf|zip)$/i;export function isPageviewRequest(method,pathname,headers){if(method!=="GET")return false;if(ASSET_EXT.test(pathname))return false;if(pathname.startsWith("/api/")||pathname.startsWith("/__")||pathname.startsWith("/_"))return false;const accept=headers.get("accept")??"";if(accept&&!accept.includes("text/html")&&!accept.includes("*/*"))return false;const agent=(headers.get("user-agent")??"").toLowerCase();if(!agent)return false;if(/bot|crawler|spider|preview|monitor|curl|wget|python-requests|headless/.test(agent))return false;return true}export function classifyAgent(userAgent){const agent=userAgent.toLowerCase();const device=/ipad|tablet/.test(agent)?"tablet":/mobi|iphone|android/.test(agent)?"mobile":"desktop";const browser=agent.includes("edg/")?"edge":agent.includes("opr/")||agent.includes("opera")?"opera":agent.includes("firefox/")?"firefox":agent.includes("chrome/")||agent.includes("crios/")?"chrome":agent.includes("safari/")?"safari":"other";return{device,browser}}export function referrerHost(referrer,ownHost){if(!referrer)return null;try{const host=new URL(referrer).host.toLowerCase();return host&&host!==ownHost.toLowerCase()?host:null}catch{return null}}export async function recordPageview(req){try{const url=new URL(req.url);if(!isPageviewRequest(req.method,url.pathname,req.headers))return;const agent=req.headers.get("user-agent")??"";const{device,browser}=classifyAgent(agent);const now=sqlDateTime(new Date);const host=req.headers.get("x-forwarded-host")?.split(",")[0]?.trim()||url.host;await db.insertInto("analytics_events").values({name:"pageview",category:"web",path:url.pathname,properties:JSON.stringify({host,referrer:referrerHost(req.headers.get("referer"),host),device,browser}),created_at:now,updated_at:now}).execute()}catch{}}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{renderHeadTags}from"./shared";export const ANALYTICSHQ_DEFAULT_SCRIPT="https://analyticshq.org/script.js";export function getAnalyticsHqHead(config,context={}){if(!config.siteId)return[];const attributes={src:config.scriptUrl||ANALYTICSHQ_DEFAULT_SCRIPT,"data-site":config.siteId,defer:""};if(config.respectDnt
|
|
1
|
+
import{renderHeadTags}from"./shared";export const ANALYTICSHQ_DEFAULT_SCRIPT="https://analyticshq.org/script.js";export function getAnalyticsHqHead(config,context={}){if(!config.siteId)return[];const attributes={src:config.scriptUrl||ANALYTICSHQ_DEFAULT_SCRIPT,"data-site":config.siteId,defer:""};if(config.respectDnt===false)attributes["data-respect-dnt"]="false";if(config.vitals===false)attributes["data-vitals"]="false";if(context.environment)attributes["data-environment"]=context.environment;return[["script",attributes]]}export function generateAnalyticsHqScript(config){const tags=getAnalyticsHqHead(config);if(!tags.length)return"";return`<!-- Stacks AnalyticsHQ -->
|
|
2
2
|
${renderHeadTags(tags)}`}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jsString,renderHeadTags}from"./shared";export const GOOGLE_ANALYTICS_SCRIPT_URL="https://www.googletagmanager.com/gtag/js";export function getGoogleAnalyticsHead(config){if(!config.trackingId)return[];const id=jsString(config.trackingId)
|
|
1
|
+
import{jsString,renderHeadTags}from"./shared";export const GOOGLE_ANALYTICS_SCRIPT_URL="https://www.googletagmanager.com/gtag/js";export function getGoogleAnalyticsHead(config){if(!config.trackingId)return[];const id=jsString(config.trackingId);const options=config.debug?`,{debug_mode:true}`:"";return[["script",{src:`${GOOGLE_ANALYTICS_SCRIPT_URL}?id=${encodeURIComponent(config.trackingId)}`,async:""}],["script",{innerHTML:`window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config',${id}${options});`}]]}export function generateGoogleAnalyticsScript(config){const tags=getGoogleAnalyticsHead(config);if(!tags.length)return"";return`<!-- Stacks Google Analytics -->
|
|
2
2
|
${renderHeadTags(tags)}`}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import{renderHeadTags}from"./shared";export function getSelfHostedAnalyticsHead(config){if(!config.siteId||!config.apiEndpoint)return[];return[["script",{"data-site":config.siteId,"data-api":config.apiEndpoint,defer:"",innerHTML:generateInlineScript(config)}]]}export function generateSelfHostedScript(config){const tags=getSelfHostedAnalyticsHead(config);if(!tags.length)return"";return`<!-- Stacks Self-Hosted Analytics -->
|
|
2
|
-
${renderHeadTags(tags)}`}function generateInlineScript(config){const honorDnt=config.honorDnt
|
|
2
|
+
${renderHeadTags(tags)}`}function generateInlineScript(config){const honorDnt=config.honorDnt?`if(n.doNotTrack==="1")return;`:"";const hashTracking=config.trackHashChanges?`w.addEventListener('hashchange',pv);`:"";const outboundTracking=config.trackOutboundLinks?`d.addEventListener('click',function(e){var a=e.target.closest('a');if(a&&a.hostname!==location.hostname){t('outbound',{url:a.href});}});`:"";return`(function(){
|
|
3
3
|
'use strict';
|
|
4
4
|
var d=document,w=window,n=navigator,s=d.currentScript;
|
|
5
5
|
var site=s.dataset.site,api=s.dataset.api;
|
package/dist/drivers/shared.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export function escapeAttr(str){return str.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">")}export function jsString(value){return JSON.stringify(value).replace(/</g,"\\u003C").replace(/>/g,"\\u003E")}export function renderHeadTag([tag,attrs]){const body=attrs.innerHTML??""
|
|
1
|
+
export function escapeAttr(str){return str.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">")}export function jsString(value){return JSON.stringify(value).replace(/</g,"\\u003C").replace(/>/g,"\\u003E")}export function renderHeadTag([tag,attrs]){const body=attrs.innerHTML??"";const rendered=Object.entries(attrs).filter(([key])=>key!=="innerHTML").map(([key,value])=>value===""?` ${key}`:` ${key}="${escapeAttr(value)}"`).join("");return`<${tag}${rendered}>${body}</${tag}>`}export function renderHeadTags(tags){return tags.map(renderHeadTag).join(`
|
|
2
2
|
`)}
|
package/dist/registry.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
+
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 new 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===false)return[];if(config.environments!==undefined){const result=initializeIntegration(config,({environment})=>resolveAnalyticsHead(config,driver,environment));return result.initialized?result.value:[]}return resolveAnalyticsHead(config,driver)}function resolveAnalyticsHead(config,driver,environment){if(!isAnalyticsDriver(driver)){throw new 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,{environment})}default:{const unhandled=driver;throw new Error(`Analytics driver "${String(unhandled)}" 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.518",
|
|
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.518",
|
|
55
|
+
"@stacksjs/env": "0.75.518",
|
|
56
|
+
"@stacksjs/types": "0.75.518"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"better-dx": "^0.2.24"
|