mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
Mechanically re-derived: bin/debrand runs over a fresh import of posthog/posthog@fa1a9a60d4 (2026-08-06). The pipeline (.hanzo/workflows), the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md and the squashed initial migration are restored from the shipped tree, same as the previous import. The prune's import-closure guard kept 21 products the new core now imports; dropping them is a refactor now, recorded rather than forced.
54 lines
2.7 KiB
TypeScript
54 lines
2.7 KiB
TypeScript
export type SnippetOption = {
|
|
content: string
|
|
enabled: boolean
|
|
comment?: string
|
|
}
|
|
|
|
export const DEFAULT_SNIPPET_METHODS =
|
|
'init capture register register_once register_for_session unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group identify setPersonProperties setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags resetGroups onFeatureFlags addFeatureFlagsHandler onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey getNextSurveyStep'.split(
|
|
' '
|
|
)
|
|
|
|
export function snippetFunctions(methods: readonly string[], arrayJs = '/static/array.js'): string {
|
|
const snippetMethods = methods.join(' ')
|
|
|
|
return `!function(t,e){var o,n,p,r;e.__SV||(window.insights && window.insights.__loaded)||(window.insights=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}p||((p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.hanzo.ai","-assets.i.hanzo.ai")+"${arrayJs}",p.onerror=function(){p=null},(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r));var u=e;for(void 0!==a?u=e[a]=[]:a="insights",u.people=u.people||[],u.toString=function(t){var e="insights";return"insights"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="${snippetMethods}".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.insights||[]);`
|
|
}
|
|
|
|
export interface BuildJsHtmlSnippetConfig {
|
|
projectToken: string
|
|
methods: string[]
|
|
options: Record<string, SnippetOption>
|
|
indent?: number
|
|
arrayJs?: string
|
|
scriptAttributes?: string
|
|
}
|
|
|
|
export function buildJsHtmlSnippet({
|
|
projectToken,
|
|
methods,
|
|
options,
|
|
indent = 0,
|
|
arrayJs,
|
|
scriptAttributes,
|
|
}: BuildJsHtmlSnippetConfig): string {
|
|
const scriptTag = scriptAttributes ? `<script ${scriptAttributes}>` : '<script>'
|
|
|
|
return [
|
|
scriptTag,
|
|
` ${snippetFunctions(methods, arrayJs)}`,
|
|
` insights.init('${projectToken}', {`,
|
|
...Object.entries(options)
|
|
.map(([key, value]) => {
|
|
if (value.enabled) {
|
|
return ` ${key}: '${value.content}',${value.comment ? ` // ${value.comment}` : ''}`
|
|
}
|
|
})
|
|
.filter(Boolean),
|
|
` })`,
|
|
'</script>',
|
|
]
|
|
.map((x) => ' '.repeat(indent) + x)
|
|
.join('\n')
|
|
}
|