@reservamos/browser-analytics 1.1.3 → 1.1.5-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +15 -0
- package/dist/browser-analytics.cjs +1 -1
- package/dist/browser-analytics.cjs.map +1 -1
- package/dist/browser-analytics.d.ts +22 -0
- package/dist/browser-analytics.esm.js +28 -16
- package/dist/browser-analytics.esm.js.map +1 -1
- package/dist/browser-analytics.iife.js +1 -1
- package/dist/browser-analytics.iife.js.map +1 -1
- package/package.json +5 -1
- package/src/events/purchaseConfirmation/index.ts +7 -0
- package/src/events/purchaseConfirmation/purchaseConfirmation.test.ts +102 -0
- package/src/events/purchaseConfirmation/purchaseConfirmationSchema.ts +15 -0
- package/src/events/purchaseConfirmation/trackPurchaseConfirmation.ts +19 -0
- package/src/index.ts +4 -0
- package/src/services/validator/validator.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reservamos/browser-analytics",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5-alpha.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/reservamos/reservamos-browser-analytics.git"
|
|
@@ -39,6 +39,10 @@
|
|
|
39
39
|
],
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"private": false,
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"registry": "https://registry.npmjs.org"
|
|
45
|
+
},
|
|
42
46
|
"scripts": {
|
|
43
47
|
"dev": "vite --host",
|
|
44
48
|
"prebuild": "rm -rf dist/*",
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PurchaseConfirmationProps } from './purchaseConfirmationSchema';
|
|
2
|
+
import purchaseConfirmationSchema from './purchaseConfirmationSchema';
|
|
3
|
+
import trackPurchaseConfirmation from './trackPurchaseConfirmation';
|
|
4
|
+
|
|
5
|
+
export type { PurchaseConfirmationProps };
|
|
6
|
+
export { purchaseConfirmationSchema };
|
|
7
|
+
export default trackPurchaseConfirmation;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import purchaseConfirmationSchema from './purchaseConfirmationSchema';
|
|
3
|
+
|
|
4
|
+
const trackEventMock = vi.fn();
|
|
5
|
+
|
|
6
|
+
vi.mock('@/track', () => ({
|
|
7
|
+
trackEvent: (...args: unknown[]) => trackEventMock(...args),
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
describe('purchaseConfirmationSchema', () => {
|
|
11
|
+
it('accepts a valid confirmed payload', () => {
|
|
12
|
+
const payload = {
|
|
13
|
+
order_number: 'MOB-123',
|
|
14
|
+
payment_status: 'confirmed',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
expect(purchaseConfirmationSchema.parse(payload)).toEqual(payload);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('accepts a valid pending_pix payload with optional fields', () => {
|
|
21
|
+
const payload = {
|
|
22
|
+
order_number: 'MOB-123',
|
|
23
|
+
payment_status: 'pending_pix',
|
|
24
|
+
payment_type: 'pix',
|
|
25
|
+
total: 199.9,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
expect(purchaseConfirmationSchema.parse(payload)).toEqual(payload);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('rejects a payload missing order_number', () => {
|
|
32
|
+
expect(() =>
|
|
33
|
+
purchaseConfirmationSchema.parse({ payment_status: 'confirmed' }),
|
|
34
|
+
).toThrow();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('rejects a payload missing payment_status', () => {
|
|
38
|
+
expect(() =>
|
|
39
|
+
purchaseConfirmationSchema.parse({ order_number: 'MOB-123' }),
|
|
40
|
+
).toThrow();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('rejects an invalid payment_status value', () => {
|
|
44
|
+
expect(() =>
|
|
45
|
+
purchaseConfirmationSchema.parse({
|
|
46
|
+
order_number: 'MOB-123',
|
|
47
|
+
payment_status: 'refunded',
|
|
48
|
+
}),
|
|
49
|
+
).toThrow();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('rejects unknown fields', () => {
|
|
53
|
+
expect(() =>
|
|
54
|
+
purchaseConfirmationSchema.parse({
|
|
55
|
+
order_number: 'MOB-123',
|
|
56
|
+
payment_status: 'confirmed',
|
|
57
|
+
extra_field: 'not allowed',
|
|
58
|
+
}),
|
|
59
|
+
).toThrow();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('trackPurchaseConfirmation', () => {
|
|
64
|
+
it('sends the Purchase Confirmation Viewed event with the given data and meta', async () => {
|
|
65
|
+
trackEventMock.mockClear();
|
|
66
|
+
const trackPurchaseConfirmation = (await import('./trackPurchaseConfirmation'))
|
|
67
|
+
.default;
|
|
68
|
+
|
|
69
|
+
const eventData = {
|
|
70
|
+
order_number: 'MOB-123',
|
|
71
|
+
payment_status: 'confirmed' as const,
|
|
72
|
+
};
|
|
73
|
+
const meta = { source: 'checkout' };
|
|
74
|
+
|
|
75
|
+
trackPurchaseConfirmation(eventData, meta);
|
|
76
|
+
|
|
77
|
+
expect(trackEventMock).toHaveBeenCalledWith(
|
|
78
|
+
'Purchase Confirmation Viewed',
|
|
79
|
+
eventData,
|
|
80
|
+
meta,
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('defaults meta to an empty object when not provided', async () => {
|
|
85
|
+
trackEventMock.mockClear();
|
|
86
|
+
const trackPurchaseConfirmation = (await import('./trackPurchaseConfirmation'))
|
|
87
|
+
.default;
|
|
88
|
+
|
|
89
|
+
const eventData = {
|
|
90
|
+
order_number: 'MOB-456',
|
|
91
|
+
payment_status: 'pending_pix' as const,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
trackPurchaseConfirmation(eventData);
|
|
95
|
+
|
|
96
|
+
expect(trackEventMock).toHaveBeenCalledWith(
|
|
97
|
+
'Purchase Confirmation Viewed',
|
|
98
|
+
eventData,
|
|
99
|
+
{},
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const purchaseConfirmationSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
order_number: z.string().min(1, 'order_number is required'),
|
|
6
|
+
payment_status: z.enum(['confirmed', 'pending_pix']),
|
|
7
|
+
payment_type: z.string().optional(),
|
|
8
|
+
total: z.number().optional(),
|
|
9
|
+
})
|
|
10
|
+
.strict();
|
|
11
|
+
|
|
12
|
+
type PurchaseConfirmationProps = z.infer<typeof purchaseConfirmationSchema>;
|
|
13
|
+
|
|
14
|
+
export type { PurchaseConfirmationProps };
|
|
15
|
+
export default purchaseConfirmationSchema;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { EventMetadata } from '@/types/eventData';
|
|
2
|
+
import type { PurchaseConfirmationProps } from './purchaseConfirmationSchema';
|
|
3
|
+
import { trackEvent } from '@/track';
|
|
4
|
+
|
|
5
|
+
const EVENT_NAME = 'Purchase Confirmation Viewed';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Tracks a Purchase Confirmation Viewed event.
|
|
9
|
+
* @param {PurchaseConfirmationProps} eventData - The data associated with the Purchase Confirmation Viewed event.
|
|
10
|
+
* @param {EventMetadata} meta - Additional metadata to include in the event.
|
|
11
|
+
*/
|
|
12
|
+
function trackPurchaseConfirmation(
|
|
13
|
+
eventData: PurchaseConfirmationProps,
|
|
14
|
+
meta: EventMetadata = {},
|
|
15
|
+
): void {
|
|
16
|
+
trackEvent(EVENT_NAME, eventData, meta);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default trackPurchaseConfirmation;
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { PaymentAttemptProps } from '@/events/paymentAttempt';
|
|
|
7
7
|
import type { PickedDepartureProps } from '@/events/pickedDeparture';
|
|
8
8
|
import type { PurchaseAttemptProps } from '@/events/purchaseAttempt';
|
|
9
9
|
import type { PurchaseCanceledProps } from '@/events/purchaseCanceled';
|
|
10
|
+
import type { PurchaseConfirmationProps } from '@/events/purchaseConfirmation';
|
|
10
11
|
import type { SearchProps } from '@/events/search';
|
|
11
12
|
import type { SeatChangeProps } from '@/events/seatChange';
|
|
12
13
|
import type { ViewResultsProps } from '@/events/viewResults';
|
|
@@ -27,6 +28,7 @@ import trackPaymentAttempt from '@/events/paymentAttempt';
|
|
|
27
28
|
import trackPickedDeparture from '@/events/pickedDeparture';
|
|
28
29
|
import trackPurchaseAttempt from '@/events/purchaseAttempt';
|
|
29
30
|
import trackPurchaseCanceled from '@/events/purchaseCanceled';
|
|
31
|
+
import trackPurchaseConfirmation from '@/events/purchaseConfirmation';
|
|
30
32
|
import trackSearch from '@/events/search';
|
|
31
33
|
import trackSeatChange from '@/events/seatChange';
|
|
32
34
|
import trackViewResults from '@/events/viewResults';
|
|
@@ -75,6 +77,7 @@ const analytics = {
|
|
|
75
77
|
customEvent: trackCustomEvent,
|
|
76
78
|
purchaseCanceled: trackPurchaseCanceled,
|
|
77
79
|
geolocationSelected: trackGeolocationSelected,
|
|
80
|
+
purchaseConfirmation: trackPurchaseConfirmation,
|
|
78
81
|
},
|
|
79
82
|
};
|
|
80
83
|
|
|
@@ -99,6 +102,7 @@ export type {
|
|
|
99
102
|
CreateFrequentPassengersSchemaProps,
|
|
100
103
|
DeleteFrequentPassengerProps,
|
|
101
104
|
GeolocationSelectedProps,
|
|
105
|
+
PurchaseConfirmationProps,
|
|
102
106
|
};
|
|
103
107
|
|
|
104
108
|
export default analytics;
|
|
@@ -8,6 +8,7 @@ import { passengersCreatedSchema } from '@/events/passengersCreated';
|
|
|
8
8
|
import { paymentAttemptSchema } from '@/events/paymentAttempt';
|
|
9
9
|
import { pickedDepartureSchema } from '@/events/pickedDeparture';
|
|
10
10
|
import { purchaseAttemptSchema } from '@/events/purchaseAttempt';
|
|
11
|
+
import { purchaseConfirmationSchema } from '@/events/purchaseConfirmation';
|
|
11
12
|
import { searchSchema } from '@/events/search';
|
|
12
13
|
import { seatChangeSchema } from '@/events/seatChange';
|
|
13
14
|
import { viewResultsSchema } from '@/events/viewResults';
|
|
@@ -101,6 +102,7 @@ const eventSchemas: Record<string, z.ZodSchema> = {
|
|
|
101
102
|
'Purchase Attempt': purchaseAttemptSchema,
|
|
102
103
|
'Picked Departure': pickedDepartureSchema,
|
|
103
104
|
'Geolocation Selected': geolocationSelectedSchema,
|
|
105
|
+
'Purchase Confirmation Viewed': purchaseConfirmationSchema,
|
|
104
106
|
};
|
|
105
107
|
|
|
106
108
|
type EventDataSchema = z.infer<
|