-
Notifications
You must be signed in to change notification settings - Fork 6
chore: Add tracking to Typesense [EDU-1005] #867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9472f35
typesense enhancement
justinegeffen cd7277c
Merge branch 'master' into typesense-context
justinegeffen 4f6e1b4
Added better tracking
justinegeffen 32c3d47
Security improvements
justinegeffen 8f37d2f
Update clientside-scripts.js
justinegeffen cce263c
Update clientside-scripts.js
justinegeffen 1729226
Update clientside-scripts.js
justinegeffen 9e849d8
Update clientside-scripts.js
justinegeffen ae3265c
Updates and improvements
justinegeffen c8e8f3f
Merge branch 'typesense-context' of https://github.com/seqeralabs/doc…
justinegeffen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,142 @@ | ||
| var allowedHost = 'docs.seqera.io'; | ||
| var hotjarAppID = 3836890; | ||
|
|
||
| function canProceed() { | ||
| if (typeof window === 'undefined') return false; | ||
| if (window.location.hostname !== allowedHost) return false; | ||
| return true; | ||
| } | ||
|
|
||
| function addScripts() { | ||
| // Sanitize search queries to prevent PII leakage | ||
| function sanitizeQuery(query) { | ||
| return query | ||
| // Redact potential API keys (20+ char alphanumeric strings) | ||
| .replace(/(^|[^A-Za-z0-9_-])([A-Za-z0-9_-]{20,})(?=[^A-Za-z0-9_-]|$)/g, '$1[REDACTED_KEY]') | ||
| // Redact IPv4 addresses (0-255 per octet) | ||
| .replace(/\b(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\b/g, '[IP]') | ||
| // Redact AWS keys | ||
| .replace(/\b(AKIA|ASIA)[A-Z0-9]{16}\b/g, '[AWS_KEY]') | ||
| .replace(/aws_[a-z_]+/gi, '[AWS_KEY]') | ||
| // Redact common secret patterns | ||
| .replace(/\b(secret|token|password|key)[=:]\S+/gi, '[REDACTED]') | ||
| // Redact email addresses | ||
| .replace(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g, '[EMAIL]') | ||
| // Limit length | ||
| .substring(0, 200); | ||
| } | ||
|
|
||
| // Helper to get the current search query from the DocSearch input | ||
| function getCurrentQuery() { | ||
| const input = document.querySelector('.DocSearch-Input, input[type="search"]'); | ||
| return input ? input.value.trim() : ''; | ||
| } | ||
|
|
||
| // Safe PostHog event tracking with error handling | ||
| function trackEvent(eventName, properties) { | ||
| try { | ||
| if (window.posthog && typeof window.posthog.capture === 'function') { | ||
| window.posthog.capture(eventName, properties); | ||
| return true; | ||
| } | ||
| } catch (e) { | ||
| console.warn('PostHog tracking failed:', e); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // Track search queries to PostHog | ||
| function trackSearch() { | ||
| if(!canProceed()) return; | ||
|
justinegeffen marked this conversation as resolved.
|
||
|
|
||
| // HotJar | ||
| (function(h,o,t,j,a,r){ | ||
| h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)}; | ||
| h._hjSettings={hjid:hotjarAppID,hjsv:6}; | ||
| a=o.getElementsByTagName('head')[0]; | ||
| r=o.createElement('script');r.async=1; | ||
| r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv; | ||
| a.appendChild(r); | ||
| })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv='); | ||
| let lastSearchTime = 0; | ||
| let lastNoResultsTime = 0; | ||
| let lastClickTime = 0; | ||
| const RATE_LIMIT_MS = 500; | ||
| const REDACTED_TOKENS = ['[REDACTED_KEY]', '[IP]', '[AWS_KEY]', '[REDACTED]', '[EMAIL]']; | ||
|
|
||
| const observer = new MutationObserver(() => { | ||
| const searchInput = document.querySelector('.DocSearch-Input, input[type="search"]'); | ||
| if (searchInput && !searchInput.dataset.searchTracked) { | ||
| searchInput.dataset.searchTracked = 'true'; | ||
|
|
||
|
justinegeffen marked this conversation as resolved.
|
||
| let searchTimeout; | ||
| searchInput.addEventListener('input', (e) => { | ||
| clearTimeout(searchTimeout); | ||
| searchTimeout = setTimeout(() => { | ||
| const now = Date.now(); | ||
|
|
||
| if (now - lastSearchTime < RATE_LIMIT_MS) { | ||
| return; | ||
| } | ||
|
|
||
| const query = e.target.value.trim(); | ||
| if (query.length > 2) { | ||
| const sanitizedQuery = sanitizeQuery(query); | ||
|
|
||
| // Only track if query isn't entirely redacted | ||
| if (sanitizedQuery && sanitizedQuery !== '[REDACTED_KEY]') { | ||
|
justinegeffen marked this conversation as resolved.
|
||
| window.posthog.capture('docs_search', { | ||
| search_query: sanitizedQuery, | ||
| page: window.location.pathname | ||
| }); | ||
| lastSearchTime = now; | ||
| } | ||
| } | ||
| }, 1000); | ||
|
justinegeffen marked this conversation as resolved.
|
||
| }); | ||
| } | ||
|
|
||
| // Track "no results" state | ||
| const noResults = document.querySelector('.DocSearch-NoResults'); | ||
| if (noResults && !noResults.dataset.noResultsTracked) { | ||
| noResults.dataset.noResultsTracked = 'true'; | ||
|
|
||
| const now = Date.now(); | ||
| if (now - lastNoResultsTime < RATE_LIMIT_MS) return; | ||
|
|
||
| const query = getCurrentQuery(); | ||
| if (query.length > 2) { | ||
| const sanitizedQuery = sanitizeQuery(query); | ||
| if (sanitizedQuery && !REDACTED_TOKENS.some(token => sanitizedQuery === token)) { | ||
| if (trackEvent('docs_search_no_results', { | ||
| search_query: sanitizedQuery, | ||
| page: window.location.pathname | ||
| })) { | ||
| lastNoResultsTime = now; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Track result clicks | ||
| const hits = document.querySelectorAll('.DocSearch-Hit a'); | ||
| hits.forEach((hit) => { | ||
| if (!hit.dataset.clickTracked) { | ||
| hit.dataset.clickTracked = 'true'; | ||
| hit.addEventListener('click', () => { | ||
| const now = Date.now(); | ||
| if (now - lastClickTime < RATE_LIMIT_MS) return; | ||
|
|
||
| const query = getCurrentQuery(); | ||
| if (query.length > 2) { | ||
| const sanitizedQuery = sanitizeQuery(query); | ||
| if (sanitizedQuery && !REDACTED_TOKENS.some(token => sanitizedQuery === token)) { | ||
| if (trackEvent('docs_search_click', { | ||
| search_query: sanitizedQuery, | ||
| result_url: hit.getAttribute('href'), | ||
| page: window.location.pathname | ||
| })) { | ||
| lastClickTime = now; | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // Optimize: observe only header/nav instead of entire body | ||
| const searchContainer = document.querySelector('header, nav, .navbar') || document.body; | ||
| observer.observe(searchContainer, { childList: true, subtree: true }); | ||
|
justinegeffen marked this conversation as resolved.
|
||
| } | ||
|
|
||
| addScripts(); | ||
| trackSearch(); | ||
|
justinegeffen marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.