diff --git a/src/__tests__/site-apps.ts b/src/__tests__/site-apps.ts index 9a2ae8d57..554cdbcc3 100644 --- a/src/__tests__/site-apps.ts +++ b/src/__tests__/site-apps.ts @@ -15,8 +15,10 @@ describe('SiteApps', () => { let emitCaptureEvent: ((eventName: string, eventPayload: CaptureResult) => void) | undefined let removeCaptureHook = jest.fn() + const token = 'testtoken' + const defaultConfig: Partial = { - token: 'testtoken', + token: token, api_host: 'https://test.com', persistence: 'memory', } @@ -40,7 +42,7 @@ describe('SiteApps', () => { }), } - delete assignableWindow._POSTHOG_JS_APPS + delete assignableWindow._POSTHOG_REMOTE_CONFIG delete assignableWindow.POSTHOG_DEBUG removeCaptureHook = jest.fn() @@ -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) @@ -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() }) @@ -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() }) diff --git a/src/site-apps.ts b/src/site-apps.ts index c77fa54bd..e985dcd2c 100644 --- a/src/site-apps.ts +++ b/src/site-apps.ts @@ -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)) @@ -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