Skip to content

Commit

Permalink
chore(analytic): track stripe checkout journeyÏ
Browse files Browse the repository at this point in the history
  • Loading branch information
wzulfikar committed Mar 30, 2021
1 parent 5e797a2 commit a2485fa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
27 changes: 18 additions & 9 deletions src/components/_preview/containers/StripePreview/controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from 'next-stripe/client';
import { useAsync } from 'react-use';

import trackGoal from '@src/utils/trackGoal';
import formatAmountForStripe from '@src/lib/stripe/formatAmountForStripe';
import StripePreview from './elements';

Expand All @@ -31,13 +32,20 @@ export default function StripePreviewController({ prices }) {
.then((res) => res.json())
.then((paymentIntent) => {
console.log('paymentIntent:', paymentIntent);

if (paymentIntent.statusCode === 500) {
trackGoal('StripePreview_PaymentFailed');

alert('Payment failed: ' + paymentIntent.message);
return;
} else {
trackGoal(
'StripePreview_PaymentSuccessful',
paymentIntent.amount_total
);

alert('Payment status: ' + paymentIntent.payment_status);
localStorage.removeItem('_stripeCheckoutSession');
}

alert('Payment status: ' + paymentIntent.payment_status);
localStorage.removeItem('_stripeCheckoutSession');
});
}
}, []);
Expand Down Expand Up @@ -65,7 +73,9 @@ export default function StripePreviewController({ prices }) {
return 'cus_JCdi2hmQ4ggKhh';
}

async function onCheckout(priceId, mode: 'subscription' | 'payment') {
async function onCheckout(priceId, amount, mode: 'subscription' | 'payment') {
trackGoal('StripePreview_ClickCheckout', amount);

// Create stripe checkout session
const qty = 1; // Assume user will buy one item

Expand All @@ -75,10 +85,7 @@ export default function StripePreviewController({ prices }) {
const lineItem = isCustomAmount
? {
name: customPrice.id,
amount: formatAmountForStripe(
customPrice.unit_amount / 100,
customPrice.currency
),
amount: formatAmountForStripe(amount / 100, customPrice.currency),
currency: customPrice.currency,
quantity: qty,
}
Expand Down Expand Up @@ -109,6 +116,8 @@ export default function StripePreviewController({ prices }) {
}

async function onClickManageSubscription() {
trackGoal('StripePreview_ClickManageSubscription');

const session = await createBillingPortalSession({
customer: getCustomerId(),
return_url: window.location.href,
Expand Down
5 changes: 3 additions & 2 deletions src/components/_preview/containers/StripePreview/elements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ function HintStripeTestCards() {
function PriceList({ prices, onCheckout, recurringSuffix = null }) {
const [isCheckingOut, setIsCheckingOut] = useState(null);

async function _onCheckout(priceId, mode) {
async function _onCheckout(priceId, amount, mode) {
setIsCheckingOut(priceId);
await onCheckout(priceId, mode);
await onCheckout(priceId, amount, mode);
setIsCheckingOut(null);
}

Expand All @@ -46,6 +46,7 @@ function PriceList({ prices, onCheckout, recurringSuffix = null }) {
onClick={() =>
_onCheckout(
price.id,
price.unit_amount,
Boolean(recurringSuffix) ? 'subscription' : 'payment'
)
}
Expand Down
14 changes: 14 additions & 0 deletions src/config/analytic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ export const events = {
Preview_GridCell_ClickOpenInGithub: {
event: 'XDGII22Y',
},

// Track checkout process
StripePreview_ClickCheckout: {
event: 'P8KTQRJY',
},
StripePreview_PaymentSuccessful: {
event: 'DRFURCVI',
},
StripePreview_PaymentFailed: {
event: '33F0HCOC',
},
StripePreview_ClickManageSubscription: {
event: 'VKBNYD6J',
},
};

export default { events };
7 changes: 5 additions & 2 deletions src/utils/trackGoal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { events } from '@src/config/analytic';
import { trackGoal as _trackGoal } from '@src/lib/analytic';

export default function trackGoal(event: keyof typeof events) {
export default function trackGoal(
event: keyof typeof events,
centValue?: number
) {
if (process.env.NODE_ENV === 'production') {
_trackGoal(events[event]);
_trackGoal({ ...events[event], centValue });
} else {
console.log('[dev][analytic] trackGoal:', events[event]);
}
Expand Down

1 comment on commit a2485fa

@vercel
Copy link

@vercel vercel bot commented on a2485fa Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.