[MKT-748]:feat/implement tracking services for cancelled subscriptions#351
[MKT-748]:feat/implement tracking services for cancelled subscriptions#351jaaaaavier wants to merge 8 commits intomasterfrom
Conversation
xabg2
left a comment
There was a problem hiding this comment.
Add the tests for this new klaviyo service.
src/services/klaviyo.service.ts
Outdated
| }, | ||
| }); | ||
|
|
||
| console.log(`[Klaviyo] ${eventName} tracked for ${email}`); |
src/services/klaviyo.service.ts
Outdated
| console.log(`[Klaviyo] ${eventName} tracked for ${email}`); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : 'Unknown error'; | ||
| console.error(`[Klaviyo] ${eventName} failed for ${email}:`, message); |
| try { | ||
| await klaviyoService.trackSubscriptionCancelled(customer.email); | ||
| } catch (error) { | ||
| log.error(`[KLAVIYO] Failed to track cancellation for ${customerId}`); |
There was a problem hiding this comment.
Log the error here also, and it would be nice to use Logger we can remove log in the future.
There was a problem hiding this comment.
Export the class here directly, e.g.: const klaviyoService = new KlaviyoService(); so you can export it directly without instantiating it every time (you can use the api key directly).
src/services/klaviyo.service.ts
Outdated
| baseUrl: string | undefined = process.env.KLAVIYO_BASE_URL | ||
| ) { | ||
| if (!apiKey) { | ||
| throw new Error("Klaviyo API Key is required."); |
There was a problem hiding this comment.
Use throw new BadRequestError('...') instead.
| } | ||
|
|
||
| export class KlaviyoTrackingService { | ||
| private readonly apiKey: string; |
There was a problem hiding this comment.
You can assign here directly the variables if you want, no need to pass it as props. Also, use config file instead of process.env.
src/services/klaviyo.service.ts
Outdated
| async trackSubscriptionCancelled(email: string): Promise<void> { | ||
| await this.trackEvent({ | ||
| email, | ||
| eventName: 'Subscription Cancelled', |
There was a problem hiding this comment.
nit: you can extract this to an enum so you can do:
TRACK_ENENTS.SubscriptionCancelled or smth like that.
|
Check Sonarcloud and tests, they are failing @jaaaaavier |
I think they are falling because we need to add some environment variables |
| try { | ||
| await klaviyoService.trackSubscriptionCancelled(customer.email); | ||
| } catch (error) { | ||
| Logger.error(`[KLAVIYO] Failed to track cancellation for ${customerId}`, error); |
There was a problem hiding this comment.
Add the error inside the first string, I think it is not printed if it is outside. Or it works?
| jest.clearAllMocks(); | ||
| (config as any).KLAVIYO_API_KEY = mockApiKey; | ||
| (config as any).KLAVIYO_BASE_URL = mockBaseUrl; | ||
| service = new KlaviyoTrackingService(); |
There was a problem hiding this comment.
You can instantiate the service in the service-factory.
src/services/klaviyo.service.ts
Outdated
| Logger.info(`[Klaviyo] ${eventName} tracked for ${email}`); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : 'Unknown error'; | ||
| Logger.error(`[Klaviyo] ${eventName} failed for ${email}:`, message); |
There was a problem hiding this comment.
Add the error inside the first string, I think it is not printed if it is outside. Or it works?
|
| import config from '../../../src/config'; | ||
|
|
||
| jest.mock('axios'); | ||
| jest.mock('../../../src/Logger'); |
There was a problem hiding this comment.
I think the logger can be spied: jest.spyOn(Logger, 'error)`
|
It will be automatically closed in 7 days if no further updates are made. |



This task, which is linked to additional PRs in other repositories, aims to track users who cancel their plans and send that data to Klaviyo. While Klaviyo already integrates with Stripe, it lacks this specific functionality, which is why we created this solution.
Changes:
Comment
Need to add two Environment variables