|
| 1 | +/** |
| 2 | + * Self-Hosted Analytics Driver |
| 3 | + * |
| 4 | + * This driver generates a minimal, privacy-focused tracking script |
| 5 | + * that sends analytics data to your own API endpoint (powered by dynamodb-tooling analytics). |
| 6 | + * |
| 7 | + * Features: |
| 8 | + * - No cookies required |
| 9 | + * - Privacy-focused (hashed visitor IDs) |
| 10 | + * - Do Not Track support |
| 11 | + * - Page view tracking |
| 12 | + * - Custom event tracking |
| 13 | + * - Outbound link tracking (optional) |
| 14 | + * - Hash-based routing support (optional) |
| 15 | + */ |
| 16 | + |
| 17 | +export interface SelfHostedConfig { |
| 18 | + /** Site ID for tracking */ |
| 19 | + siteId: string |
| 20 | + /** API endpoint URL for collecting analytics */ |
| 21 | + apiEndpoint: string |
| 22 | + /** Honor Do Not Track browser setting */ |
| 23 | + honorDnt?: boolean |
| 24 | + /** Track hash changes as page views */ |
| 25 | + trackHashChanges?: boolean |
| 26 | + /** Track outbound link clicks */ |
| 27 | + trackOutboundLinks?: boolean |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Generate the self-hosted analytics tracking script |
| 32 | + */ |
| 33 | +export function generateSelfHostedScript(config: SelfHostedConfig): string { |
| 34 | + if (!config.siteId || !config.apiEndpoint) { |
| 35 | + return '' |
| 36 | + } |
| 37 | + |
| 38 | + const siteId = escapeAttr(config.siteId) |
| 39 | + const apiEndpoint = escapeAttr(config.apiEndpoint) |
| 40 | + const honorDnt = config.honorDnt ? `if(n.doNotTrack==="1")return;` : '' |
| 41 | + const hashTracking = config.trackHashChanges ? `w.addEventListener('hashchange',pv);` : '' |
| 42 | + const outboundTracking = config.trackOutboundLinks |
| 43 | + ? ` |
| 44 | + d.addEventListener('click',function(e){ |
| 45 | + var a=e.target.closest('a'); |
| 46 | + if(a&&a.hostname!==location.hostname){ |
| 47 | + t('outbound',{url:a.href}); |
| 48 | + } |
| 49 | + });` |
| 50 | + : '' |
| 51 | + |
| 52 | + return `<!-- Stacks Self-Hosted Analytics --> |
| 53 | +<script data-site="${siteId}" data-api="${apiEndpoint}" defer> |
| 54 | +(function(){ |
| 55 | + 'use strict'; |
| 56 | + var d=document,w=window,n=navigator,s=d.currentScript; |
| 57 | + var site=s.dataset.site,api=s.dataset.api; |
| 58 | + ${honorDnt} |
| 59 | + var q=[],sid=Math.random().toString(36).slice(2); |
| 60 | + function t(e,p){ |
| 61 | + var x=new XMLHttpRequest(); |
| 62 | + x.open('POST',api+'/collect',true); |
| 63 | + x.setRequestHeader('Content-Type','application/json'); |
| 64 | + x.send(JSON.stringify({ |
| 65 | + s:site,sid:sid,e:e,p:p||{}, |
| 66 | + u:location.href,r:d.referrer,t:d.title, |
| 67 | + sw:screen.width,sh:screen.height |
| 68 | + })); |
| 69 | + } |
| 70 | + function pv(){t('pageview');} |
| 71 | + ${hashTracking} |
| 72 | + ${outboundTracking} |
| 73 | + if(d.readyState==='complete')pv(); |
| 74 | + else w.addEventListener('load',pv); |
| 75 | + w.stacksAnalytics={track:function(n,v){t('event',{name:n,value:v});}}; |
| 76 | +})(); |
| 77 | +</script>` |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Generate the head tag for including self-hosted analytics |
| 82 | + * Compatible with Stacks docs config.ts head array format |
| 83 | + */ |
| 84 | +export function getSelfHostedAnalyticsHead(config: SelfHostedConfig): [string, Record<string, string>][] { |
| 85 | + if (!config.siteId || !config.apiEndpoint) { |
| 86 | + return [] |
| 87 | + } |
| 88 | + |
| 89 | + // Return as an inline script tag configuration |
| 90 | + // Note: For VitePress/Stacks docs, inline scripts need special handling |
| 91 | + return [ |
| 92 | + ['script', { |
| 93 | + 'data-site': config.siteId, |
| 94 | + 'data-api': config.apiEndpoint, |
| 95 | + 'defer': '', |
| 96 | + 'innerHTML': generateInlineScript(config), |
| 97 | + }], |
| 98 | + ] |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Generate just the inline script content (without script tags) |
| 103 | + */ |
| 104 | +function generateInlineScript(config: SelfHostedConfig): string { |
| 105 | + const honorDnt = config.honorDnt ? `if(n.doNotTrack==="1")return;` : '' |
| 106 | + const hashTracking = config.trackHashChanges ? `w.addEventListener('hashchange',pv);` : '' |
| 107 | + const outboundTracking = config.trackOutboundLinks |
| 108 | + ? `d.addEventListener('click',function(e){var a=e.target.closest('a');if(a&&a.hostname!==location.hostname){t('outbound',{url:a.href});}});` |
| 109 | + : '' |
| 110 | + |
| 111 | + return `(function(){ |
| 112 | +'use strict'; |
| 113 | +var d=document,w=window,n=navigator,s=d.currentScript; |
| 114 | +var site=s.dataset.site,api=s.dataset.api; |
| 115 | +${honorDnt} |
| 116 | +var q=[],sid=Math.random().toString(36).slice(2); |
| 117 | +function t(e,p){ |
| 118 | +var x=new XMLHttpRequest(); |
| 119 | +x.open('POST',api+'/collect',true); |
| 120 | +x.setRequestHeader('Content-Type','application/json'); |
| 121 | +x.send(JSON.stringify({s:site,sid:sid,e:e,p:p||{},u:location.href,r:d.referrer,t:d.title,sw:screen.width,sh:screen.height})); |
| 122 | +} |
| 123 | +function pv(){t('pageview');} |
| 124 | +${hashTracking} |
| 125 | +${outboundTracking} |
| 126 | +if(d.readyState==='complete')pv(); |
| 127 | +else w.addEventListener('load',pv); |
| 128 | +w.stacksAnalytics={track:function(n,v){t('event',{name:n,value:v});}}; |
| 129 | +})();` |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Escape attribute value for safe HTML insertion |
| 134 | + */ |
| 135 | +function escapeAttr(str: string): string { |
| 136 | + return str |
| 137 | + .replace(/&/g, '&') |
| 138 | + .replace(/"/g, '"') |
| 139 | + .replace(/'/g, ''') |
| 140 | + .replace(/</g, '<') |
| 141 | + .replace(/>/g, '>') |
| 142 | +} |
0 commit comments