diff --git a/services/web/app/src/Features/Subscription/RecurlyEventHandler.js b/services/web/app/src/Features/Subscription/RecurlyEventHandler.js index 973438aed7..aed2671dac 100644 --- a/services/web/app/src/Features/Subscription/RecurlyEventHandler.js +++ b/services/web/app/src/Features/Subscription/RecurlyEventHandler.js @@ -2,6 +2,8 @@ const AnalyticsManager = require('../Analytics/AnalyticsManager') const SubscriptionEmailHandler = require('./SubscriptionEmailHandler') const { ObjectID } = require('mongodb') +const INVOICE_SUBSCRIPTION_LIMIT = 10 + async function sendRecurlyAnalyticsEvent(event, eventData) { const userId = _getUserId(eventData) if (!ObjectID.isValid(userId)) { @@ -47,11 +49,13 @@ async function sendRecurlyAnalyticsEvent(event, eventData) { } async function _sendSubscriptionStartedEvent(userId, eventData) { - const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData) + const { planCode, quantity, state, isTrial, subscriptionId } = + _getSubscriptionData(eventData) AnalyticsManager.recordEventForUser(userId, 'subscription-started', { plan_code: planCode, quantity, is_trial: isTrial, + subscriptionId, }) AnalyticsManager.setUserPropertyForUser( userId, @@ -71,11 +75,13 @@ async function _sendSubscriptionStartedEvent(userId, eventData) { } async function _sendSubscriptionUpdatedEvent(userId, eventData) { - const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData) + const { planCode, quantity, state, isTrial, subscriptionId } = + _getSubscriptionData(eventData) AnalyticsManager.recordEventForUser(userId, 'subscription-updated', { plan_code: planCode, quantity, is_trial: isTrial, + subscriptionId, }) AnalyticsManager.setUserPropertyForUser( userId, @@ -91,11 +97,13 @@ async function _sendSubscriptionUpdatedEvent(userId, eventData) { } async function _sendSubscriptionCancelledEvent(userId, eventData) { - const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData) + const { planCode, quantity, state, isTrial, subscriptionId } = + _getSubscriptionData(eventData) AnalyticsManager.recordEventForUser(userId, 'subscription-cancelled', { plan_code: planCode, quantity, is_trial: isTrial, + subscriptionId, }) AnalyticsManager.setUserPropertyForUser(userId, 'subscription-state', state) AnalyticsManager.setUserPropertyForUser( @@ -106,11 +114,13 @@ async function _sendSubscriptionCancelledEvent(userId, eventData) { } async function _sendSubscriptionExpiredEvent(userId, eventData) { - const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData) + const { planCode, quantity, state, isTrial, subscriptionId } = + _getSubscriptionData(eventData) AnalyticsManager.recordEventForUser(userId, 'subscription-expired', { plan_code: planCode, quantity, is_trial: isTrial, + subscriptionId, }) AnalyticsManager.setUserPropertyForUser( userId, @@ -126,11 +136,13 @@ async function _sendSubscriptionExpiredEvent(userId, eventData) { } async function _sendSubscriptionRenewedEvent(userId, eventData) { - const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData) + const { planCode, quantity, state, isTrial, subscriptionId } = + _getSubscriptionData(eventData) AnalyticsManager.recordEventForUser(userId, 'subscription-renewed', { plan_code: planCode, quantity, is_trial: isTrial, + subscriptionId, }) AnalyticsManager.setUserPropertyForUser( userId, @@ -146,10 +158,12 @@ async function _sendSubscriptionRenewedEvent(userId, eventData) { } async function _sendSubscriptionReactivatedEvent(userId, eventData) { - const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData) + const { planCode, quantity, state, isTrial, subscriptionId } = + _getSubscriptionData(eventData) AnalyticsManager.recordEventForUser(userId, 'subscription-reactivated', { plan_code: planCode, quantity, + subscriptionId, }) AnalyticsManager.setUserPropertyForUser( userId, @@ -165,7 +179,35 @@ async function _sendSubscriptionReactivatedEvent(userId, eventData) { } async function _sendInvoicePaidEvent(userId, eventData) { - AnalyticsManager.recordEventForUser(userId, 'subscription-invoice-collected') + const invoice = eventData.invoice + if (!invoice) { + return + } + const invoiceNumber = invoice.invoice_number + const currency = invoice.currency + const totalInCents = invoice.total_in_cents + const taxInCents = invoice.tax_in_cents + const country = invoice.address?.country + const collectionMethod = invoice.collection_method + const subscriptionIds = {} + invoice.subscription_ids?.forEach((e, idx) => { + if (idx < INVOICE_SUBSCRIPTION_LIMIT) { + subscriptionIds[`subscriptionId${idx + 1}`] = e + } + }) + AnalyticsManager.recordEventForUser( + userId, + 'subscription-invoice-collected', + { + invoiceNumber, + currency, + totalInCents, + taxInCents, + country, + collectionMethod, + ...subscriptionIds, + } + ) AnalyticsManager.setUserPropertyForUser( userId, 'subscription-is-trial', @@ -195,6 +237,7 @@ function _getSubscriptionData(eventData) { planCode: eventData.subscription.plan.plan_code, quantity: eventData.subscription.quantity, state: eventData.subscription.state, + subscriptionId: eventData.subscription.uuid, isTrial, } } diff --git a/services/web/app/src/Features/Subscription/SubscriptionController.js b/services/web/app/src/Features/Subscription/SubscriptionController.js index 42e36e6eb7..8d4c128bcb 100644 --- a/services/web/app/src/Features/Subscription/SubscriptionController.js +++ b/services/web/app/src/Features/Subscription/SubscriptionController.js @@ -69,7 +69,9 @@ async function plansPage(req, res) { usage: getDefault('usage', 'usage', 'enterprise'), } - AnalyticsManager.recordEventForSession(req.session, 'plans-page-view') + AnalyticsManager.recordEventForSession(req.session, 'plans-page-view', { + currency: recommendedCurrency, + }) res.render('subscriptions/plans-marketing-v2', { title: 'plans_and_pricing', diff --git a/services/web/frontend/js/features/subscription/context/payment-context.tsx b/services/web/frontend/js/features/subscription/context/payment-context.tsx index 8929f8204a..fa95995754 100644 --- a/services/web/frontend/js/features/subscription/context/payment-context.tsx +++ b/services/web/frontend/js/features/subscription/context/payment-context.tsx @@ -95,7 +95,10 @@ function usePayment({ publicKey }: RecurlyOptions) { return } - eventTracking.sendMB('payment-page-view', { plan: planCode }) + eventTracking.sendMB('payment-page-view', { + plan: planCode, + currency: currencyCode, + }) eventTracking.send( 'subscription-funnel', 'subscription-form-viewed', @@ -147,6 +150,7 @@ function usePayment({ publicKey }: RecurlyOptions) { initiallySelectedCurrencyCode, planCode, publicKey, + currencyCode, t, ]) diff --git a/services/web/frontend/js/main/new-subscription.js b/services/web/frontend/js/main/new-subscription.js index e17cc25e1f..558f04df06 100644 --- a/services/web/frontend/js/main/new-subscription.js +++ b/services/web/frontend/js/main/new-subscription.js @@ -76,7 +76,10 @@ export default App.controller( `&itm_referrer=${window.ITMReferrer}` } - eventTracking.sendMB('payment-page-view', { plan: window.plan_code }) + eventTracking.sendMB('payment-page-view', { + plan: window.plan_code, + currency: $scope.currencyCode, + }) eventTracking.send( 'subscription-funnel', 'subscription-form-viewed', diff --git a/services/web/test/unit/src/Subscription/RecurlyEventHandlerTests.js b/services/web/test/unit/src/Subscription/RecurlyEventHandlerTests.js index 293dabf4c7..101a5a15c0 100644 --- a/services/web/test/unit/src/Subscription/RecurlyEventHandlerTests.js +++ b/services/web/test/unit/src/Subscription/RecurlyEventHandlerTests.js @@ -12,6 +12,7 @@ describe('RecurlyEventHandler', function () { account_code: this.userId, }, subscription: { + uuid: '8435ad98c1ce45da99b07f6a6a2e780f', plan: { plan_code: 'collaborator-annual', }, @@ -50,6 +51,7 @@ describe('RecurlyEventHandler', function () { plan_code: this.planCode, quantity: 1, is_trial: true, + subscriptionId: this.eventData.subscription.uuid, } ) sinon.assert.calledWith( @@ -102,6 +104,7 @@ describe('RecurlyEventHandler', function () { plan_code: this.planCode, quantity: 3, is_trial: false, + subscriptionId: this.eventData.subscription.uuid, } ) sinon.assert.calledWith( @@ -133,6 +136,7 @@ describe('RecurlyEventHandler', function () { plan_code: this.planCode, quantity: 1, is_trial: true, + subscriptionId: this.eventData.subscription.uuid, } ) sinon.assert.calledWith( @@ -169,6 +173,7 @@ describe('RecurlyEventHandler', function () { plan_code: this.planCode, quantity: 1, is_trial: true, + subscriptionId: this.eventData.subscription.uuid, } ) sinon.assert.calledWith( @@ -199,6 +204,7 @@ describe('RecurlyEventHandler', function () { plan_code: this.planCode, quantity: 1, is_trial: true, + subscriptionId: this.eventData.subscription.uuid, } ) sinon.assert.calledWith( @@ -234,6 +240,7 @@ describe('RecurlyEventHandler', function () { plan_code: this.planCode, quantity: 1, is_trial: true, + subscriptionId: this.eventData.subscription.uuid, } ) }) @@ -250,27 +257,47 @@ describe('RecurlyEventHandler', function () { { plan_code: this.planCode, quantity: 1, + subscriptionId: this.eventData.subscription.uuid, } ) }) it('with paid_charge_invoice_notification', function () { + const invoice = { + invoice_number: 1234, + currency: 'USD', + state: 'paid', + total_in_cents: 720, + tax_in_cents: 12, + address: { + country: 'Liurnia', + }, + collection_method: 'automatic', + subscription_ids: ['abcd1234', 'defa3214'], + } this.RecurlyEventHandler.sendRecurlyAnalyticsEvent( 'paid_charge_invoice_notification', { account: { account_code: this.userId, }, - invoice: { - state: 'paid', - total_in_cents: 720, - }, + invoice, } ) sinon.assert.calledWith( this.AnalyticsManager.recordEventForUser, this.userId, - 'subscription-invoice-collected' + 'subscription-invoice-collected', + { + invoiceNumber: invoice.invoice_number, + currency: invoice.currency, + totalInCents: invoice.total_in_cents, + taxInCents: invoice.tax_in_cents, + country: invoice.address.country, + collectionMethod: invoice.collection_method, + subscriptionId1: invoice.subscription_ids[0], + subscriptionId2: invoice.subscription_ids[1], + } ) })