Merge pull request #36496 from overleaf/oa-annual-events

[web] Subscription event segmentation

GitOrigin-RevId: fdc25ac7bc7320ed82617b7c27c12a17eb31d723
This commit is contained in:
Olzhas Askar
2026-09-17 11:59:20 +00:00
committed by Copybot
parent ba4e6ef8ed
commit 6053781e57
14 changed files with 305 additions and 47 deletions
@@ -209,6 +209,10 @@ describe('RecurlyEventHandler', function () {
it('with updated_subscription_notification', async function (ctx) {
ctx.planCode = 'new-plan-code'
ctx.eventData.subscription.plan.plan_code = ctx.planCode
ctx.SubscriptionLocator.promises.getUsersSubscription.resolves({
_id: 'sub123',
planCode: 'collaborator',
})
await ctx.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
'updated_subscription_notification',
ctx.eventData
@@ -219,6 +223,7 @@ describe('RecurlyEventHandler', function () {
'subscription-updated',
{
plan_code: ctx.planCode,
previous_plan_code: 'collaborator',
quantity: 1,
is_trial: true,
has_ai_add_on: false,
@@ -246,6 +251,19 @@ describe('RecurlyEventHandler', function () {
)
})
it('with updated_subscription_notification and no subscription in mongo', async function (ctx) {
await ctx.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
'updated_subscription_notification',
ctx.eventData
)
sinon.assert.calledWith(
ctx.AnalyticsManager.recordEventForUserInBackground,
ctx.userId,
'subscription-updated',
sinon.match({ previous_plan_code: undefined })
)
})
it('with canceled_subscription_notification', async function (ctx) {
ctx.eventData.subscription.state = 'cancelled'
await ctx.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
@@ -7,6 +7,7 @@ import MockResponse from '../helpers/MockResponse.mjs'
import SubscriptionErrors from '../../../../app/src/Features/Subscription/Errors.mjs'
import { PaymentServiceResourceNotFoundError } from '../../../../modules/subscriptions/app/src/PaymentServiceErrors.mjs'
import SubscriptionHelper from '../../../../app/src/Features/Subscription/SubscriptionHelper.mjs'
import PlansLocator from '../../../../app/src/Features/Subscription/PlansLocator.mjs'
import { AI_ADD_ON_CODE } from '../../../../app/src/Features/Subscription/AiHelper.mjs'
const modulePath =
@@ -360,6 +361,7 @@ describe('SubscriptionController', function () {
vi.doMock('../../../../app/src/Features/Subscription/PlansLocator', () => ({
default: (ctx.PlansLocator = {
getPlanCadence: PlansLocator.getPlanCadence,
findLocalPlanInSettings: sinon.stub().returns({
annual: false,
}),
@@ -611,6 +613,79 @@ describe('SubscriptionController', function () {
})
})
describe('subscription-page-view event', function () {
const renderWithSubscription = async (ctx, personalSubscription) => {
ctx.AnalyticsManager.recordEventForSession.resetHistory()
ctx.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
{ personalSubscription, memberGroupSubscriptions: [] }
)
await new Promise((resolve, reject) => {
ctx.res.render = () => resolve()
ctx.SubscriptionController.userSubscriptionPage(
ctx.req,
ctx.res,
ctx.rejectOnError(reject)
)
})
return ctx.AnalyticsManager.recordEventForSession.lastCall.args[2]
}
it('should segment a monthly subscription', async function (ctx) {
const segmentation = await renderWithSubscription(ctx, {
planCode: 'collaborator',
plan: { planCode: 'collaborator' },
payment: { currency: 'USD', trialEndsAt: null },
})
expect(segmentation).to.deep.include({
plan_code: 'collaborator',
billing_cycle: 'monthly',
is_trial: false,
currency: 'USD',
})
})
it('should segment an annual subscription', async function (ctx) {
const segmentation = await renderWithSubscription(ctx, {
planCode: 'collaborator-annual',
plan: { planCode: 'collaborator-annual', annual: true },
payment: { currency: 'EUR', trialEndsAt: null },
})
expect(segmentation).to.deep.include({
plan_code: 'collaborator-annual',
billing_cycle: 'annual',
is_trial: false,
currency: 'EUR',
})
})
it('should segment a subscription in trial', async function (ctx) {
const segmentation = await renderWithSubscription(ctx, {
planCode: 'collaborator_free_trial_7_days',
plan: { planCode: 'collaborator_free_trial_7_days' },
payment: {
currency: 'USD',
trialEndsAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
},
})
expect(segmentation).to.deep.include({
plan_code: 'collaborator_free_trial_7_days',
billing_cycle: 'monthly',
is_trial: true,
currency: 'USD',
})
})
it('should omit the plan segmentation without a personal subscription', async function (ctx) {
const segmentation = await renderWithSubscription(ctx, undefined)
expect(segmentation).to.deep.include({
plan_code: undefined,
billing_cycle: null,
is_trial: false,
currency: undefined,
})
})
})
describe('when an unrecognized query param is present', function () {
beforeEach(async function (ctx) {
setReqValidationModeForTests('log')