Skip to content

Commit c57a4b3

Browse files
committed
feat: analytics event
1 parent 24b0081 commit c57a4b3

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/common/apple/purchase.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type {
44
ResponseBodyV2DecodedPayload,
55
} from '@apple/app-store-server-library';
66
import type { User } from '../../entity/user/User';
7-
import { getAppleTransactionType } from './utils';
7+
import {
8+
getAnalyticsEventFromAppleNotification,
9+
getAppleTransactionType,
10+
logAppleAnalyticsEvent,
11+
} from './utils';
812
import { AppleTransactionType } from './types';
913
import {
1014
UserTransaction,
@@ -24,6 +28,7 @@ import type { Block, KnownBlock } from '@slack/web-api';
2428
import { webhooks } from '../slack';
2529
import { checkUserCoresAccess } from '../user';
2630
import { CoresRole } from '../../types';
31+
import { convertCurrencyToUSD } from '../../integrations/openExchangeRates';
2732

2833
export const isCorePurchaseApple = ({
2934
transactionInfo,
@@ -143,6 +148,7 @@ export const notifyNewStoreKitPurchase = async ({
143148
export const handleCoresPurchase = async ({
144149
transactionInfo,
145150
user,
151+
notification,
146152
}: {
147153
transactionInfo: JWSTransactionDecodedPayload;
148154
user: Pick<User, 'id' | 'subscriptionFlags' | 'coresRole'>;
@@ -234,12 +240,32 @@ export const handleCoresPurchase = async ({
234240
return userTransaction;
235241
});
236242

243+
const currencyInUSD = await convertCurrencyToUSD(
244+
(transactionInfo.price || 0) / 1000,
245+
transactionInfo.currency || 'USD',
246+
);
247+
248+
const eventName = getAnalyticsEventFromAppleNotification(
249+
notification.notificationType,
250+
notification.subtype,
251+
);
252+
253+
if (eventName) {
254+
await logAppleAnalyticsEvent(
255+
transactionInfo,
256+
undefined,
257+
eventName,
258+
user,
259+
currencyInUSD,
260+
);
261+
}
262+
237263
if (transaction.status === UserTransactionStatus.Success) {
238264
await notifyNewStoreKitPurchase({
239265
data: transactionInfo,
240266
transaction,
241267
user,
242-
currencyInUSD: transaction.valueIncFees,
268+
currencyInUSD,
243269
});
244270
}
245271

src/common/apple/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { isNullOrUndefined } from '../object';
1313
import {
1414
AnalyticsEventName,
1515
sendAnalyticsEvent,
16+
TargetType,
1617
} from '../../integrations/analytics';
1718
import {
1819
AppleTransactionType,
@@ -21,6 +22,7 @@ import {
2122
} from './types';
2223
import { readFile } from 'fs/promises';
2324
import { isTest } from '../utils';
25+
import { isCorePurchaseApple } from './purchase';
2426

2527
export const verifyAndDecodeAppleSignedData = async ({
2628
notification,
@@ -56,7 +58,7 @@ export const verifyAndDecodeAppleSignedData = async ({
5658

5759
export const logAppleAnalyticsEvent = async (
5860
transactionInfo: JWSTransactionDecodedPayload,
59-
renewalInfo: JWSRenewalInfoDecodedPayload,
61+
renewalInfo: JWSRenewalInfoDecodedPayload | undefined,
6062
eventName: AnalyticsEventName,
6163
user: Pick<User, 'id' | 'subscriptionFlags' | 'coresRole'>,
6264
currencyInUSD: number,
@@ -93,6 +95,9 @@ export const logAppleAnalyticsEvent = async (
9395
app_platform: 'api',
9496
user_id: user.id,
9597
extra: JSON.stringify(extra),
98+
target_type: isCorePurchaseApple({ transactionInfo })
99+
? TargetType.Credits
100+
: TargetType.Plus,
96101
},
97102
]);
98103
};

src/integrations/analytics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ export async function sendExperimentAllocationEvent<
7070
{ retries: 3 },
7171
);
7272
}
73+
74+
export enum TargetType {
75+
Plus = 'plus',
76+
Credits = 'credits',
77+
}

0 commit comments

Comments
 (0)