Skip to content

Commit

Permalink
[PAY-2012] Add analytics for purchase start/success/failure (#6425)
Browse files Browse the repository at this point in the history
  • Loading branch information
schottra authored Oct 23, 2023
1 parent 8dc22a7 commit 428ba1a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
25 changes: 15 additions & 10 deletions packages/common/src/models/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1600,24 +1600,29 @@ type BuyUSDCRecoveryFailure = {
error: string
}

type PurchaseContentStarted = {
eventName: Name.PURCHASE_CONTENT_STARTED
extraAmount?: number
extraAmountPreset?: string
type ContentPurchaseMetadata = {
price: number
contentId: number
contentName: string
contentType: string
payExtraAmount: number
payExtraPreset?: string
artistHandle: string
isVerifiedArtist: boolean
}
type PurchaseContentSuccess = {

type PurchaseContentStarted = ContentPurchaseMetadata & {
eventName: Name.PURCHASE_CONTENT_STARTED
}
type PurchaseContentSuccess = ContentPurchaseMetadata & {
eventName: Name.PURCHASE_CONTENT_SUCCESS
contentId: number
contentType: string
}
type PurchaseContentFailure = {

type PurchaseContentFailure = ContentPurchaseMetadata & {
eventName: Name.PURCHASE_CONTENT_FAILURE
contentId: number
contentType: string
error: string
}

type PurchaseContentTwitterShare = {
eventName: Name.PURCHASE_CONTENT_TWITTER_SHARE
text: string
Expand Down
55 changes: 37 additions & 18 deletions packages/common/src/store/purchase-content/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ type GetPurchaseConfigArgs = {
contentType: ContentType
}

function* getUSDCPremiumConditions({
contentId,
contentType
}: GetPurchaseConfigArgs) {
function* getContentInfo({ contentId, contentType }: GetPurchaseConfigArgs) {
if (contentType !== ContentType.TRACK) {
throw new Error('Only tracks are supported')
}
Expand All @@ -62,7 +59,19 @@ function* getUSDCPremiumConditions({
) {
throw new Error('Content is missing premium conditions')
}
return trackInfo.premium_conditions.usdc_purchase
const artistInfo = yield* select(getUser, { id: trackInfo.owner_id })
if (!artistInfo) {
throw new Error('Failed to retrieve content owner')
}

const {
premium_conditions: {
usdc_purchase: { price }
},
title
} = trackInfo

return { price, title, artistInfo }
}

function* getPurchaseConfig({ contentId, contentType }: GetPurchaseConfigArgs) {
Expand Down Expand Up @@ -140,24 +149,32 @@ function* doStartPurchaseContentFlow({
const reportToSentry = yield* getContext('reportToSentry')
const { track, make } = yield* getContext('analytics')

const { price, title, artistInfo } = yield* call(getContentInfo, {
contentId,
contentType
})

const analyticsInfo = {
price: price / 100,
contentId,
contentType,
contentName: title,
artistHandle: artistInfo.handle,
isVerifiedArtist: artistInfo.is_verified,
payExtraAmount: extraAmount ? extraAmount / 100 : 0,
payExtraPreset: extraAmountPreset
}

// Record start
yield* call(
track,
make({
eventName: Name.PURCHASE_CONTENT_STARTED,
extraAmount,
extraAmountPreset,
contentId,
contentType
...analyticsInfo
})
)

try {
const { price } = yield* call(getUSDCPremiumConditions, {
contentId,
contentType
})

// get user bank
const userBank = yield* call(getUSDCUserBank)

Expand Down Expand Up @@ -245,7 +262,10 @@ function* doStartPurchaseContentFlow({

yield* call(
track,
make({ eventName: Name.PURCHASE_CONTENT_SUCCESS, contentId, contentType })
make({
eventName: Name.PURCHASE_CONTENT_SUCCESS,
...analyticsInfo
})
)
} catch (e: unknown) {
// If we get a known error, pipe it through directly. Otherwise make sure we
Expand All @@ -264,9 +284,8 @@ function* doStartPurchaseContentFlow({
track,
make({
eventName: Name.PURCHASE_CONTENT_FAILURE,
contentId,
contentType,
error: error.message
error: error.message,
...analyticsInfo
})
)
}
Expand Down

0 comments on commit 428ba1a

Please sign in to comment.