2021-06-10 04:04:30 -04:00
|
|
|
const AnalyticsManager = require('../Analytics/AnalyticsManager')
|
2022-01-24 05:59:30 -05:00
|
|
|
const SplitTestHandler = require('../SplitTests/SplitTestHandler')
|
2021-09-28 05:30:01 -04:00
|
|
|
const SubscriptionEmailHandler = require('./SubscriptionEmailHandler')
|
2021-06-10 04:04:30 -04:00
|
|
|
|
|
|
|
function sendRecurlyAnalyticsEvent(event, eventData) {
|
|
|
|
switch (event) {
|
|
|
|
case 'new_subscription_notification':
|
2021-09-28 05:30:01 -04:00
|
|
|
sendSubscriptionStartedEvent(eventData)
|
2021-06-10 04:04:30 -04:00
|
|
|
break
|
|
|
|
case 'updated_subscription_notification':
|
|
|
|
_sendSubscriptionUpdatedEvent(eventData)
|
|
|
|
break
|
|
|
|
case 'canceled_subscription_notification':
|
|
|
|
_sendSubscriptionCancelledEvent(eventData)
|
|
|
|
break
|
|
|
|
case 'expired_subscription_notification':
|
|
|
|
_sendSubscriptionExpiredEvent(eventData)
|
|
|
|
break
|
|
|
|
case 'renewed_subscription_notification':
|
|
|
|
_sendSubscriptionRenewedEvent(eventData)
|
|
|
|
break
|
|
|
|
case 'reactivated_account_notification':
|
|
|
|
_sendSubscriptionReactivatedEvent(eventData)
|
|
|
|
break
|
|
|
|
case 'paid_charge_invoice_notification':
|
2021-06-16 10:22:22 -04:00
|
|
|
if (
|
|
|
|
eventData.invoice.state === 'paid' &&
|
|
|
|
eventData.invoice.total_in_cents > 0
|
|
|
|
) {
|
2021-06-10 04:04:30 -04:00
|
|
|
_sendInvoicePaidEvent(eventData)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'closed_invoice_notification':
|
2021-06-16 10:22:22 -04:00
|
|
|
if (
|
|
|
|
eventData.invoice.state === 'collected' &&
|
|
|
|
eventData.invoice.total_in_cents > 0
|
|
|
|
) {
|
2021-06-10 04:04:30 -04:00
|
|
|
_sendInvoicePaidEvent(eventData)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:30:01 -04:00
|
|
|
async function sendSubscriptionStartedEvent(eventData) {
|
2021-06-10 04:04:30 -04:00
|
|
|
const userId = _getUserId(eventData)
|
|
|
|
const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData)
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-started', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
is_trial: isTrial,
|
|
|
|
})
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-plan-code',
|
|
|
|
planCode
|
|
|
|
)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(userId, 'subscription-state', state)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-is-trial',
|
|
|
|
isTrial
|
|
|
|
)
|
2021-09-28 05:30:01 -04:00
|
|
|
|
|
|
|
// send the trial onboarding email
|
|
|
|
if (isTrial) {
|
2022-01-24 05:59:30 -05:00
|
|
|
const assignment = await SplitTestHandler.promises.getAssignment(
|
2021-09-28 05:30:01 -04:00
|
|
|
userId,
|
|
|
|
'trial-onboarding-email'
|
|
|
|
)
|
|
|
|
|
|
|
|
if (assignment.variant === 'send-email') {
|
|
|
|
SubscriptionEmailHandler.sendTrialOnboardingEmail(userId)
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 04:04:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
async function _sendSubscriptionUpdatedEvent(eventData) {
|
2021-06-10 04:04:30 -04:00
|
|
|
const userId = _getUserId(eventData)
|
|
|
|
const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData)
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-updated', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
2021-09-16 04:48:01 -04:00
|
|
|
is_trial: isTrial,
|
2021-06-10 04:04:30 -04:00
|
|
|
})
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-plan-code',
|
|
|
|
planCode
|
|
|
|
)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(userId, 'subscription-state', state)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-is-trial',
|
|
|
|
isTrial
|
|
|
|
)
|
2021-06-10 04:04:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
async function _sendSubscriptionCancelledEvent(eventData) {
|
2021-06-10 04:04:30 -04:00
|
|
|
const userId = _getUserId(eventData)
|
|
|
|
const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData)
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-cancelled', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
is_trial: isTrial,
|
|
|
|
})
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(userId, 'subscription-state', state)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-is-trial',
|
|
|
|
isTrial
|
|
|
|
)
|
2021-06-10 04:04:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
async function _sendSubscriptionExpiredEvent(eventData) {
|
2021-06-10 04:04:30 -04:00
|
|
|
const userId = _getUserId(eventData)
|
|
|
|
const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData)
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-expired', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
is_trial: isTrial,
|
|
|
|
})
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-plan-code',
|
|
|
|
planCode
|
|
|
|
)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(userId, 'subscription-state', state)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-is-trial',
|
|
|
|
isTrial
|
|
|
|
)
|
2021-06-10 04:04:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
async function _sendSubscriptionRenewedEvent(eventData) {
|
2021-06-10 04:04:30 -04:00
|
|
|
const userId = _getUserId(eventData)
|
|
|
|
const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData)
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-renewed', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
is_trial: isTrial,
|
|
|
|
})
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-plan-code',
|
|
|
|
planCode
|
|
|
|
)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(userId, 'subscription-state', state)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-is-trial',
|
|
|
|
isTrial
|
|
|
|
)
|
2021-06-10 04:04:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
async function _sendSubscriptionReactivatedEvent(eventData) {
|
2021-06-10 04:04:30 -04:00
|
|
|
const userId = _getUserId(eventData)
|
|
|
|
const { planCode, quantity, state, isTrial } = _getSubscriptionData(eventData)
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-reactivated', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
})
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-plan-code',
|
|
|
|
planCode
|
|
|
|
)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(userId, 'subscription-state', state)
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-is-trial',
|
|
|
|
isTrial
|
|
|
|
)
|
2021-06-10 04:04:30 -04:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
async function _sendInvoicePaidEvent(eventData) {
|
2021-06-10 04:04:30 -04:00
|
|
|
const userId = _getUserId(eventData)
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-invoice-collected')
|
|
|
|
AnalyticsManager.setUserPropertyForUser(
|
|
|
|
userId,
|
|
|
|
'subscription-is-trial',
|
|
|
|
false
|
|
|
|
)
|
2021-06-10 04:04:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function _getUserId(eventData) {
|
|
|
|
let userId
|
|
|
|
if (eventData && eventData.account && eventData.account.account_code) {
|
|
|
|
userId = eventData.account.account_code
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
'account.account_code missing in event data to identity user ID'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return userId
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getSubscriptionData(eventData) {
|
|
|
|
const isTrial =
|
|
|
|
eventData.subscription.trial_started_at &&
|
|
|
|
eventData.subscription.current_period_started_at &&
|
|
|
|
eventData.subscription.trial_started_at.getTime() ===
|
|
|
|
eventData.subscription.current_period_started_at.getTime()
|
|
|
|
return {
|
|
|
|
planCode: eventData.subscription.plan.plan_code,
|
|
|
|
quantity: eventData.subscription.quantity,
|
|
|
|
state: eventData.subscription.state,
|
|
|
|
isTrial,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
sendRecurlyAnalyticsEvent,
|
2021-09-28 05:30:01 -04:00
|
|
|
sendSubscriptionStartedEvent,
|
2021-06-10 04:04:30 -04:00
|
|
|
}
|