Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions src/app/analytics/ga.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ describe('Testing GA Service', () => {

vi.mocked(localStorageService.get).mockImplementation((key) => {
const store: Record<string, string> = {
subscriptionId: 'sub_12345',
paymentIntentId: '',
priceId: 'price_yearly_2tb',
currency: 'EUR',
Expand All @@ -236,7 +235,7 @@ describe('Testing GA Service', () => {
expect(event).toMatchObject({
event: 'purchase',
ecommerce: {
transaction_id: 'sub_12345',
transaction_id: 'user_uuid_123',
currency: 'EUR',
value: 95.9,
items: [
Expand All @@ -260,7 +259,6 @@ describe('Testing GA Service', () => {
vi.mocked(localStorageService.getUser).mockReturnValue({ uuid: 'user_uuid' } as any);
vi.mocked(localStorageService.get).mockImplementation((key) => {
if (key === 'paymentIntentId') return 'pi_999';
if (key === 'subscriptionId') return 'sub_888';
if (key === 'amountPaid') return '100';
if (key === 'itemOriginalPrice') return '119.88';
if (key === 'checkout_item_data')
Expand All @@ -279,34 +277,10 @@ describe('Testing GA Service', () => {
expect(event.ecommerce.transaction_id).toBe('pi_999');
});

it('should use subscription ID when payment intent is not available', () => {
vi.mocked(localStorageService.getUser).mockReturnValue({ uuid: 'user_uuid' } as any);
vi.mocked(localStorageService.get).mockImplementation((key) => {
if (key === 'paymentIntentId') return null;
if (key === 'subscriptionId') return 'sub_888';
if (key === 'amountPaid') return '100';
if (key === 'itemOriginalPrice') return '119.88';
if (key === 'checkout_item_data')
return JSON.stringify({
item_name: '2TB Year Plan',
item_category: 'Individual',
item_variant: 'year',
discount: 0,
});
return '';
});

gaService.trackPurchase();

const event = globalThis.window.dataLayer[0] as any;
expect(event.ecommerce.transaction_id).toBe('sub_888');
});

it('should fallback to user UUID when neither payment intent nor subscription ID are available', () => {
it('should fallback to user UUID when payment intent is not available', () => {
vi.mocked(localStorageService.getUser).mockReturnValue({ uuid: 'user_fallback_uuid' } as any);
vi.mocked(localStorageService.get).mockImplementation((key) => {
if (key === 'paymentIntentId') return null;
if (key === 'subscriptionId') return null;
if (key === 'amountPaid') return '100';
if (key === 'itemOriginalPrice') return '119.88';
if (key === 'checkout_item_data')
Expand Down
3 changes: 1 addition & 2 deletions src/app/analytics/ga.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ function trackPurchase(): void {
return;
}

const subscriptionId = localStorageService.get('subscriptionId');
const paymentIntentId = localStorageService.get('paymentIntentId');
const priceId = localStorageService.get('priceId');
const currency = localStorageService.get('currency');
Expand All @@ -173,7 +172,7 @@ function trackPurchase(): void {
console.error('[GA Service] Error parsing checkout_item_data:', parseError);
}

const transactionId = paymentIntentId || subscriptionId || uuid;
const transactionId = paymentIntentId || uuid;
const currencyCode = currency ?? 'EUR';

const itemName = checkoutItemData?.item_name || 'Unknown Plan';
Expand Down
31 changes: 1 addition & 30 deletions src/app/analytics/impact.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ vi.mock('services/error.service', () => ({
},
}));

const subId = 'sub_123';
const paymentIntentId = 'py_123';
const mockedUserUuid = '00000000-0000-0000-0000-0000000000';
const mockImpactApiUrl = 'mock-impact-api-url';
Expand Down Expand Up @@ -93,7 +92,6 @@ beforeEach(() => {

vi.spyOn(localStorageService, 'get').mockImplementation((key) => {
if (key === 'paymentIntentId') return paymentIntentId;
if (key === 'subscriptionId') return subId;
if (key === 'productName') return planName;
if (key === 'priceId') return product.price.id;
if (key === 'currency') return product.price.currency;
Expand All @@ -110,7 +108,6 @@ describe('Testing Impact Service', () => {
const setToLocalStorageSpy = vi.spyOn(localStorageService, 'set');

savePaymentDataInLocalStorage({
subscriptionId: subId,
paymentIntentId,
selectedPlan: product as PriceWithTax,
users: 1,
Expand All @@ -121,32 +118,12 @@ describe('Testing Impact Service', () => {
expect(setToLocalStorageSpy).toHaveBeenCalledWith('amountPaid', expectedAmount);
});

it('When the plan is not lifetime, then it saves the subscription ID to localStorage', () => {
const setToLocalStorageSpy = vi.spyOn(localStorageService, 'set');

savePaymentDataInLocalStorage({
subscriptionId: subId,
paymentIntentId: undefined,
selectedPlan: product as PriceWithTax,
users: 1,
couponCodeData: promoCode,
isFirstPurchase: true,
});

expect(setToLocalStorageSpy).toHaveBeenCalledWith('subscriptionId', subId);
});

it('When the plan is lifetime, then it saves the payment intent ID to localStorage', () => {
const setToLocalStorageSpy = vi.spyOn(localStorageService, 'set');
const lifetimeProduct = {
...product,
price: { ...product.price, interval: 'lifetime' },
};

savePaymentDataInLocalStorage({
subscriptionId: undefined,
paymentIntentId,
selectedPlan: lifetimeProduct as PriceWithTax,
selectedPlan: product as PriceWithTax,
users: 1,
couponCodeData: promoCode,
isFirstPurchase: true,
Expand All @@ -159,7 +136,6 @@ describe('Testing Impact Service', () => {
const setToLocalStorageSpy = vi.spyOn(localStorageService, 'set');

savePaymentDataInLocalStorage({
subscriptionId: subId,
paymentIntentId,
selectedPlan: product as PriceWithTax,
users: 1,
Expand All @@ -176,7 +152,6 @@ describe('Testing Impact Service', () => {
const setToLocalStorageSpy = vi.spyOn(localStorageService, 'set');

savePaymentDataInLocalStorage({
subscriptionId: subId,
paymentIntentId,
selectedPlan: product as PriceWithTax,
users: 1,
Expand All @@ -191,7 +166,6 @@ describe('Testing Impact Service', () => {
const setToLocalStorageSpy = vi.spyOn(localStorageService, 'set');

savePaymentDataInLocalStorage({
subscriptionId: subId,
paymentIntentId,
selectedPlan: product as PriceWithTax,
users: 1,
Expand Down Expand Up @@ -287,7 +261,6 @@ describe('Testing Impact Service', () => {
timestamp: expect.any(String),
properties: expect.objectContaining({
impact_value: parseFloat(expectedAmount),
subscription_id: subId,
payment_intent: paymentIntentId,
order_promo_code: promoCode.codeName,
}),
Expand All @@ -301,7 +274,6 @@ describe('Testing Impact Service', () => {
it('When the amount paid is 0, then it uses 0.01 as the minimum impact value', async () => {
vi.spyOn(localStorageService, 'get').mockImplementation((key) => {
if (key === 'amountPaid') return '0';
if (key === 'subscriptionId') return subId;
if (key === 'couponCode') return promoCode.codeName;
if (key === 'isFirstPurchase') return 'true';
return null;
Expand Down Expand Up @@ -382,7 +354,6 @@ describe('Testing Impact Service', () => {
vi.spyOn(localStorageService, 'get').mockImplementation((key) => {
if (key === 'couponCode') return 'CNINTERNXT'; // In whitelist
if (key === 'amountPaid') return expectedAmount;
if (key === 'subscriptionId') return subId;
if (key === 'isFirstPurchase') return 'true';
return null;
});
Expand Down
18 changes: 7 additions & 11 deletions src/app/analytics/impact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { sendAddShoppersConversion } from './addShoppers.services';
*
*/
export interface SavePaymentDataParams {
subscriptionId: string | undefined;
paymentIntentId: string | undefined;
selectedPlan: PriceWithTax | undefined;
users: number;
Expand All @@ -38,18 +37,13 @@ export interface SavePaymentDataParams {
}

export function savePaymentDataInLocalStorage({
subscriptionId,
paymentIntentId,
selectedPlan,
users,
couponCodeData,
isFirstPurchase,
}: SavePaymentDataParams) {
if (subscriptionId && selectedPlan?.price.interval !== 'lifetime') {
localStorageService.set('subscriptionId', subscriptionId);
}

if (paymentIntentId && selectedPlan?.price.interval === 'lifetime') {
if (paymentIntentId) {
localStorageService.set('paymentIntentId', paymentIntentId);
}

Expand All @@ -76,12 +70,13 @@ export async function trackSignUp(uuid: string): Promise<void> {
const IMPACT_API = envService.getVariable('impactApiUrl');
const anonymousID = getCookie('impactAnonymousId');
const source = getCookie('impactSource');
const irclickid = getCookie('impactClickId');

if (globalThis.window.gtag) {
window.gtag('event', 'User Signup');
}

if (source && source !== 'direct') {
if ((source && source !== 'direct') || irclickid) {
await axios.post(IMPACT_API, {
anonymousId: anonymousID,
timestamp: dayjs().format('YYYY-MM-DDTHH:mm:ss.sssZ'),
Expand All @@ -90,6 +85,7 @@ export async function trackSignUp(uuid: string): Promise<void> {
type: 'track',
event: 'User Signup',
...(gclid && { gclid }),
...(irclickid && { properties: { irclickid } }),
});
}
} catch (error) {
Expand All @@ -107,7 +103,6 @@ export async function trackPaymentConversion(): Promise<void> {
}

const { uuid, email: userEmail } = userSettings;
const subscription = localStorageService.get('subscriptionId');
const paymentIntent = localStorageService.get('paymentIntentId');
const currency = localStorageService.get('currency');
const amountPaidStr = localStorageService.get('amountPaid');
Expand All @@ -130,20 +125,21 @@ export async function trackPaymentConversion(): Promise<void> {
const IMPACT_API = envService.getVariable('impactApiUrl');
const anonymousID = getCookie('impactAnonymousId') || uuidV4();
const source = getCookie('impactSource');
const irclickid = getCookie('impactClickId');

const IMPACT_COUPON_WHITELIST = ['CNINTERNXT', 'CNINTERNXTL', 'CLOUDOFF'];
const isImpactCoupon = couponCode && IMPACT_COUPON_WHITELIST.includes(couponCode.toUpperCase());

if (isFirstPurchase && ((source && source !== 'direct') || isImpactCoupon)) {
if (isFirstPurchase && ((source && source !== 'direct') || isImpactCoupon || irclickid)) {
try {
await axios.post(IMPACT_API, {
anonymousId: anonymousID,
timestamp: dayjs().format('YYYY-MM-DDTHH:mm:ss.sssZ'),
properties: {
impact_value: amount === 0 ? 0.01 : amount,
subscription_id: subscription,
payment_intent: paymentIntent,
...(couponCode && { order_promo_code: couponCode }),
...(irclickid && { irclickid }),
},
userId: uuid,
type: 'track',
Expand Down
2 changes: 0 additions & 2 deletions src/views/Checkout/hooks/useUserPayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ export const useUserPayment = () => {
});

savePaymentDataInLocalStorage({
subscriptionId: subscription.subscriptionId,
paymentIntentId: subscription.paymentIntentId,
selectedPlan: currentSelectedPlan,
users: 1,
Expand Down Expand Up @@ -221,7 +220,6 @@ export const useUserPayment = () => {
});

savePaymentDataInLocalStorage({
subscriptionId: undefined,
paymentIntentId,
selectedPlan: currentSelectedPlan,
users: 1,
Expand Down
1 change: 0 additions & 1 deletion src/views/Checkout/views/CheckoutSuccessView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import metaService from 'app/analytics/meta.service';
import { userStoragePolling } from 'utils/userStoragePolling.utils';

export function removePaymentsStorage() {
localStorageService.removeItem('subscriptionId');
localStorageService.removeItem('paymentIntentId');
localStorageService.removeItem('amountPaid');
localStorageService.removeItem('productName');
Expand Down
Loading