Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { getActivePinia, setActivePinia } from 'pinia'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import {
markStoresPending,
markStoresReady
} from '@/platform/telemetry/storeReadiness'

const hoisted = vi.hoisted(() => {
const analytics = {
identify: vi.fn().mockResolvedValue(undefined),
Expand All @@ -13,6 +19,7 @@ const hoisted = vi.hoisted(() => {
const resolvedUserInfo = { value: null as { id: string } | null }
return {
analytics,
reportError: vi.fn(),
load: vi.fn(() => analytics),
inAppPlugin: vi.fn(() => ({ name: 'Customer.io In-App Plugin' })),
userEmail: { value: null as string | null },
Expand Down Expand Up @@ -40,6 +47,10 @@ const hoisted = vi.hoisted(() => {
}
})

vi.mock('@/platform/telemetry/reportError', () => ({
reportError: hoisted.reportError
}))

vi.mock('@customerio/cdp-analytics-browser', () => ({
AnalyticsBrowser: { load: hoisted.load },
InAppPlugin: hoisted.inAppPlugin
Expand Down Expand Up @@ -155,7 +166,6 @@ describe('CustomerIoTelemetryProvider', () => {
})

it('continues tracking events and page views when the in-app plugin fails to register', async () => {
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
const registrationError = new Error('in-app setup failed')
hoisted.analytics.register.mockRejectedValue(registrationError)
const provider = createProvider()
Expand All @@ -171,10 +181,9 @@ describe('CustomerIoTelemetryProvider', () => {
SOURCE
)
expect(hoisted.analytics.page).toHaveBeenCalledOnce()
expect(consoleError).toHaveBeenCalledWith(
'Failed to initialize Customer.io in-app plugin:',
registrationError
)
expect(hoisted.reportError).toHaveBeenCalledWith(registrationError, {
errorType: 'customerio_in_app_plugin_registration_failure'
})

provider.trackAddApiCreditButtonClicked()
await vi.waitFor(() =>
Expand Down Expand Up @@ -220,6 +229,26 @@ describe('CustomerIoTelemetryProvider', () => {
)
})

it('defers user identification until stores are ready', async () => {
const pinia = getActivePinia()
setActivePinia(undefined)
markStoresPending()
try {
createProvider()
await vi.dynamicImportSettled()
await vi.waitFor(() => expect(hoisted.load).toHaveBeenCalled())

expect(hoisted.onUserResolved).not.toHaveBeenCalled()

markStoresReady()

await vi.waitFor(() => expect(hoisted.onUserResolved).toHaveBeenCalled())
} finally {
markStoresReady()
setActivePinia(pinia)
}
})

it('updates the identified user when the active locale changes', async () => {
createProvider()
await vi.dynamicImportSettled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { watch } from 'vue'

import { useCurrentUser } from '@/composables/auth/useCurrentUser'
import { i18n } from '@/i18n'
import { reportError } from '@/platform/telemetry/reportError'
import { whenStoresReady } from '@/platform/telemetry/storeReadiness'
import type { AuthUserInfo } from '@/types/authTypes'

import { TelemetryEvents } from '../../types'
Expand Down Expand Up @@ -67,20 +69,27 @@ export class CustomerIoTelemetryProvider implements TelemetryProvider {
}

void import('@customerio/cdp-analytics-browser')
.then(({ AnalyticsBrowser, InAppPlugin }) => {
.then(async ({ AnalyticsBrowser, InAppPlugin }) => {
const analytics = AnalyticsBrowser.load({ writeKey })
const inAppRegistration = analytics.register(
InAppPlugin({
siteId,
events: null,
anonymousInApp: false,
_env: undefined,
_logging: undefined,
colorScheme: 'system'
const inAppRegistration = analytics
.register(
InAppPlugin({
siteId,
events: null,
anonymousInApp: false,
_env: undefined,
_logging: undefined,
colorScheme: 'system'
})
)
.catch((error) => {
reportError(error, {
errorType: 'customerio_in_app_plugin_registration_failure'
})
})
)
this.analytics = analytics

await whenStoresReady()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High β€” await whenStoresReady() was placed before this.analytics = analytics and void this.flushQueue(), so the entire event pipeline β€” not just user identification β€” is now gated on Pinia: until the gate opens every event goes to the uncapped eventQueue, and because the gate has no timeout and cannot reject, a boot failure between markStoresPending() and markStoresReady() buffers events for the whole session with no error ever surfaced. The Mixpanel change in this same diff gets this right β€” it flushes first and defers only onUserResolved behind the gate. Move the assignment and the flush above the await, and/or bound the wait.

Raised by 6 of 8 reviewers (gemini-3.1-pro adversarial, gemini-3.1-pro edge-case, claude-opus-5-thinking-max adversarial, claude-opus-5-thinking-max edge-case, kimi-k3-max adversarial, kimi-k3-max edge-case).

this.analytics = analytics
const currentUser = useCurrentUser()
const identifyResolvedUser = (user: AuthUserInfo) => {
const identity = {
Expand Down Expand Up @@ -117,17 +126,10 @@ export class CustomerIoTelemetryProvider implements TelemetryProvider {
})

void this.flushQueue()
void inAppRegistration
.catch((error) => {
console.error(
'Failed to initialize Customer.io in-app plugin:',
error
)
})
.finally(() => {
this.isPageViewTrackingReady = true
this.flushPageView()
})
void inAppRegistration.finally(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟒 Low β€” The .finally() that sets isPageViewTrackingReady and flushes the queued page view is now attached only after await whenStoresReady(), even though page-view flushing touches no store. That widens the window in which successive trackPageView() calls collapse into the single pageViewQueued boolean, and no page view is ever sent if the gate never opens; attaching the .finally() before the await preserves the previous behaviour.

Raised by 2 of 8 reviewers (claude-opus-5-thinking-max adversarial, claude-opus-5-thinking-max edge-case).

this.isPageViewTrackingReady = true
this.flushPageView()
})
})
.catch((error) => {
console.error('Failed to load Customer.io:', error)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟑 Medium β€” Making the .then callback async widens this .catch to cover everything after the await, so a throw from useCurrentUser() or the watch() registration now sets isEnabled = false and discards this.eventQueue under the misleading message "Failed to load Customer.io". This handler also stays on bare console.error while the far less severe in-app-plugin failure was upgraded to reportError in this same diff, leaving the path that disables the provider entirely invisible in Sentry/Datadog.

Raised by 3 of 8 reviewers (kimi-k3-max adversarial, kimi-k3-max edge-case, claude-opus-5-thinking-max edge-case).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { getActivePinia, setActivePinia } from 'pinia'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import {
markStoresPending,
markStoresReady
} from '@/platform/telemetry/storeReadiness'

const mockMixpanel = vi.hoisted(() => ({
init: vi.fn(),
track: vi.fn(),
Expand Down Expand Up @@ -256,14 +262,34 @@ describe('MixpanelTelemetryProvider β€” with configured token', () => {
new MixpanelTelemetryProvider()
await waitForMixpanelInit()

expect(mockOnUserResolved).toHaveBeenCalled()
await vi.waitFor(() => expect(mockOnUserResolved).toHaveBeenCalled())
const callback = mockOnUserResolved.mock.calls[0]?.[0] as (user: {
id?: string
}) => void
callback({ id: 'user-42' })

expect(mockMixpanel.identify).toHaveBeenCalledWith('user-42')
})

it('defers user identification until stores are ready', async () => {
const pinia = getActivePinia()
setActivePinia(undefined)
markStoresPending()
try {
new MixpanelTelemetryProvider()
await waitForMixpanelInit()
await Promise.resolve()

expect(mockOnUserResolved).not.toHaveBeenCalled()

markStoresReady()

await vi.waitFor(() => expect(mockOnUserResolved).toHaveBeenCalled())
} finally {
markStoresReady()
setActivePinia(pinia)
}
})
})

describe('MixpanelTelemetryProvider β€” direct event tracking methods', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { omit } from 'es-toolkit'
import { watch } from 'vue'

import { useCurrentUser } from '@/composables/auth/useCurrentUser'
import { reportError } from '@/platform/telemetry/reportError'
import { whenStoresReady } from '@/platform/telemetry/storeReadiness'

import type {
AuthMetadata,
Expand Down Expand Up @@ -114,11 +116,19 @@ export class MixpanelTelemetryProvider implements TelemetryProvider {
loaded: () => {
this.isInitialized = true
this.flushEventQueue() // flush events that were queued while initializing
useCurrentUser().onUserResolved((user) => {
if (this.mixpanel && user.id) {
this.mixpanel.identify(user.id)
}
})
void whenStoresReady()
.then(() => {
useCurrentUser().onUserResolved((user) => {
if (this.mixpanel && user.id) {
this.mixpanel.identify(user.id)
}
})
})
.catch((error) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟒 Low β€” whenStoresReady() has no timeout and never rejects, so this .catch can only fire on a synchronous useCurrentUser() throw β€” the very failure this deferral guards against (stores never becoming ready) leaves the .then pending forever, so signed-in users are silently never identify'd and every event stays on the anonymous distinct id with nothing reported. Racing the gate against a deadline and reporting on expiry would make a stalled boot visible instead of indistinguishable from a healthy session.

Raised by 4 of 8 reviewers (claude-opus-5-thinking-max adversarial, claude-opus-5-thinking-max edge-case, kimi-k3-max adversarial, kimi-k3-max edge-case).

reportError(error, {
errorType: 'mixpanel_user_identification_failure'
})
})
}
})
})
Expand Down
Loading