Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Dec 17, 2024
1 parent db79e6d commit 1c119ed
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
63 changes: 40 additions & 23 deletions src/__tests__/site-apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ describe('SiteApps', () => {
let emitCaptureEvent: ((eventName: string, eventPayload: CaptureResult) => void) | undefined
let removeCaptureHook = jest.fn()

const token = 'testtoken'

const defaultConfig: Partial<PostHogConfig> = {
token: 'testtoken',
token: token,
api_host: 'https://test.com',
persistence: 'memory',
}
Expand All @@ -40,7 +42,7 @@ describe('SiteApps', () => {
}),
}

delete assignableWindow._POSTHOG_JS_APPS
delete assignableWindow._POSTHOG_REMOTE_CONFIG
delete assignableWindow.POSTHOG_DEBUG

removeCaptureHook = jest.fn()
Expand Down Expand Up @@ -249,7 +251,12 @@ describe('SiteApps', () => {
})

it('does not load site apps if new global loader exists', () => {
assignableWindow._POSTHOG_JS_APPS = []
assignableWindow._POSTHOG_REMOTE_CONFIG = {
[token]: {
config: {},
siteApps: [],
},
} as any
siteAppsInstance.onRemoteConfig({
siteApps: [{ id: '1', url: '/site_app/1' }],
} as RemoteConfig)
Expand Down Expand Up @@ -289,26 +296,31 @@ describe('SiteApps', () => {

beforeEach(() => {
appConfigs = []
assignableWindow._POSTHOG_JS_APPS = [
{
id: '1',
init: jest.fn((config) => {
appConfigs.push(config)
return {
processEvent: jest.fn(),
}
}),
assignableWindow._POSTHOG_REMOTE_CONFIG = {
[token]: {
config: {},
siteApps: [
{
id: '1',
init: jest.fn((config) => {
appConfigs.push(config)
return {
processEvent: jest.fn(),
}
}),
},
{
id: '2',
init: jest.fn((config) => {
appConfigs.push(config)
return {
processEvent: jest.fn(),
}
}),
},
],
},
{
id: '2',
init: jest.fn((config) => {
appConfigs.push(config)
return {
processEvent: jest.fn(),
}
}),
},
]
} as any

siteAppsInstance.init()
})
Expand All @@ -319,7 +331,12 @@ describe('SiteApps', () => {
})

it('does not sets up the eventCaptured listener if no site apps', () => {
assignableWindow._POSTHOG_JS_APPS = []
assignableWindow._POSTHOG_REMOTE_CONFIG = {
[token]: {
config: {},
siteApps: [],
},
} as any
siteAppsInstance.onRemoteConfig({} as RemoteConfig)
expect(posthog.on).not.toHaveBeenCalled()
})
Expand Down
10 changes: 7 additions & 3 deletions src/site-apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class SiteApps {
}
}

get siteAppLoaders(): SiteAppLoader[] | undefined {
return assignableWindow._POSTHOG_REMOTE_CONFIG?.[this.instance.config.token]?.siteApps
}

init() {
if (this.isEnabled) {
const stop = this.instance._addCaptureHook(this.eventCollector.bind(this))
Expand Down Expand Up @@ -142,17 +146,17 @@ export class SiteApps {
}

onRemoteConfig(response: RemoteConfig): void {
if (isArray(assignableWindow._POSTHOG_JS_APPS)) {
if (this.siteAppLoaders) {
if (!this.isEnabled) {
logger.error(`PostHog site apps are disabled. Enable the "opt_in_site_apps" config to proceed.`)
return
}

for (const app of assignableWindow._POSTHOG_JS_APPS) {
for (const app of this.siteAppLoaders) {
this.setupSiteApp(app)
}

if (!assignableWindow._POSTHOG_JS_APPS.length) {
if (!this.siteAppLoaders.length) {
this.stopBuffering?.()
} else {
// NOTE: We could improve this to only fire if we actually have listeners for the event
Expand Down

0 comments on commit 1c119ed

Please sign in to comment.