2021-06-10 04:04:30 -04:00
|
|
|
const AnalyticsManager = require('../Analytics/AnalyticsManager')
|
|
|
|
|
|
|
|
function sendRecurlyAnalyticsEvent(event, eventData) {
|
|
|
|
switch (event) {
|
|
|
|
case 'new_subscription_notification':
|
|
|
|
_sendSubscriptionStartedEvent(eventData)
|
|
|
|
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-09 10:45:41 -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-09 10:45:41 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-started', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
is_trial: isTrial,
|
|
|
|
})
|
2021-09-09 10:45:41 -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-09 10:45:41 -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-09 10:45:41 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-updated', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
})
|
2021-09-09 10:45:41 -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-09 10:45:41 -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-09 10:45:41 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-cancelled', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
is_trial: isTrial,
|
|
|
|
})
|
2021-09-09 10:45:41 -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-09 10:45:41 -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-09 10:45:41 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-expired', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
is_trial: isTrial,
|
|
|
|
})
|
2021-09-09 10:45:41 -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-09 10:45:41 -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-09 10:45:41 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-renewed', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
is_trial: isTrial,
|
|
|
|
})
|
2021-09-09 10:45:41 -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-09 10:45:41 -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-09 10:45:41 -04:00
|
|
|
AnalyticsManager.recordEventForUser(userId, 'subscription-reactivated', {
|
2021-06-10 04:04:30 -04:00
|
|
|
plan_code: planCode,
|
|
|
|
quantity,
|
|
|
|
})
|
2021-09-09 10:45:41 -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-09 10:45:41 -04:00
|
|
|
async function _sendInvoicePaidEvent(eventData) {
|
2021-06-10 04:04:30 -04:00
|
|
|
const userId = _getUserId(eventData)
|
2021-09-09 10:45:41 -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,
|
|
|
|
}
|