Skip to content

Free & Open-Source Invoice Generator - No Sign-Up, No Ads, Instant PDF Export, 100% In-Browser, and Fully Customizable Templates.

feat: add umami tracking for item discount type changes (#287)

Vlad Sazonau8e893645872c
diff --git a/src/app/(main)/(app)/components/__tests__/invoice-pdf-download-link.test.tsx b/src/app/(main)/(app)/components/__tests__/invoice-pdf-download-link.test.tsx
index 41d6736..0c21cd5 100644
--- a/src/app/(main)/(app)/components/__tests__/invoice-pdf-download-link.test.tsx
+++ b/src/app/(main)/(app)/components/__tests__/invoice-pdf-download-link.test.tsx
@@ -260,6 +260,7 @@ describe("InvoicePDFDownloadLink", () => {
expect(umamiTrackEvent).toHaveBeenCalledWith("download_invoice", {
data: {
invoice_template: "default",
+ has_item_discount: false,
},
});
expect(updateAppMetadata).toHaveBeenCalledTimes(1);
@@ -270,6 +271,28 @@ describe("InvoicePDFDownloadLink", () => {
expect(showCTAToast).toHaveBeenCalledTimes(1);
});
+ it("should report has_item_discount when an item has a discount", () => {
+ const initialInvoiceData = getInitialInvoiceData();
+ const invoiceData = {
+ ...initialInvoiceData,
+ items: [
+ ...initialInvoiceData.items,
+ { ...initialInvoiceData.items[0], discountAmount: 10 },
+ ],
+ };
+
+ renderInvoicePDFDownloadLink({ invoiceData });
+
+ fireEvent.click(getDownloadLink());
+
+ expect(umamiTrackEvent).toHaveBeenCalledWith("download_invoice", {
+ data: {
+ invoice_template: "default",
+ has_item_discount: true,
+ },
+ });
+ });
+
it("should not show the CTA toast again while the cooldown is active", async () => {
vi.useFakeTimers();
@@ -284,6 +307,7 @@ describe("InvoicePDFDownloadLink", () => {
expect(umamiTrackEvent).toHaveBeenCalledWith("download_invoice", {
data: {
invoice_template: "default",
+ has_item_discount: false,
},
});
diff --git a/src/app/(main)/(app)/components/invoice-form/sections/__tests__/invoice-items.test.tsx b/src/app/(main)/(app)/components/invoice-form/sections/__tests__/invoice-items.test.tsx
index e1e5614..e5b1ac8 100644
--- a/src/app/(main)/(app)/components/invoice-form/sections/__tests__/invoice-items.test.tsx
+++ b/src/app/(main)/(app)/components/invoice-form/sections/__tests__/invoice-items.test.tsx
@@ -595,6 +595,10 @@ describe("InvoiceItems read-only amounts", () => {
});
describe("InvoiceItems discount", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
afterEach(() => {
cleanup();
});
@@ -618,6 +622,9 @@ describe("InvoiceItems discount", () => {
expect(screen.getByLabelText("Discount value")).toHaveFocus();
expect(screen.getByRole("radio", { name: "Percentage" })).toBeChecked();
expect(screen.getByLabelText(/^Label/)).toHaveValue("");
+ expect(umamiTrackEvent).toHaveBeenCalledExactlyOnceWith(
+ "add_item_discount",
+ );
});
it("switches between a percentage and a fixed amount", async () => {
@@ -630,6 +637,9 @@ describe("InvoiceItems discount", () => {
expect(screen.getByRole("radio", { name: "Fixed amount" })).toBeChecked();
expect(screen.getByRole("radio", { name: "Percentage" })).not.toBeChecked();
+ expect(umamiTrackEvent).toHaveBeenCalledWith("change_item_discount_type", {
+ data: { discount_type: "amount" },
+ });
});
it("moves between discount types with the arrow keys, as one tab stop", async () => {
@@ -697,6 +707,8 @@ describe("InvoiceItems discount", () => {
await user.click(screen.getByRole("radio", { name: "Fixed amount" }));
expect(screen.getByLabelText("Discount value")).toHaveValue(50);
+ // re-picking the selected type is not a change
+ expect(umamiTrackEvent).toHaveBeenCalledTimes(2);
});
it("explains a percentage and a fixed amount before a value is entered", async () => {
@@ -770,6 +782,9 @@ describe("InvoiceItems discount", () => {
await vi.waitFor(() => {
expect(addButton).toHaveFocus();
});
+ expect(umamiTrackEvent).toHaveBeenCalledExactlyOnceWith(
+ "remove_item_discount",
+ );
// reopening shows the cleared fields, not the removed discount
await user.click(addButton);
diff --git a/src/app/(main)/(app)/components/invoice-form/sections/components/item-discount-fields.tsx b/src/app/(main)/(app)/components/invoice-form/sections/components/item-discount-fields.tsx
index 1a66d81..ae5ec42 100644
--- a/src/app/(main)/(app)/components/invoice-form/sections/components/item-discount-fields.tsx
+++ b/src/app/(main)/(app)/components/invoice-form/sections/components/item-discount-fields.tsx
@@ -27,6 +27,7 @@ import { Input } from "@/components/ui/input";
import { InputHelperMessage } from "@/components/ui/input-helper-message";
import { Label } from "@/components/ui/label";
import { MoneyInput } from "@/components/ui/money-input";
+import { umamiTrackEvent } from "@/lib/umami-analytics-track-event";
import { cn } from "@/lib/utils";
/**
@@ -65,6 +66,7 @@ export function ItemDiscountFields({
onClick={() => {
setIsOpen(true);
setShouldFocusValue(true);
+ umamiTrackEvent("add_item_discount");
}}
data-testid={`addItemDiscount${index}`}
>
@@ -93,6 +95,7 @@ export function ItemDiscountFields({
setValue(`items.${index}.discountLabel`, "");
setValue(`items.${index}.discountType`, "percentage");
setIsOpen(false);
+ umamiTrackEvent("remove_item_discount");
// the section unmounts with the focused button in it, so hand focus to the
// "Add discount" button that takes its place
@@ -197,6 +200,10 @@ function OpenItemDiscountFields({
if (nextType === field.value) return;
field.onChange(nextType);
+
+ umamiTrackEvent("change_item_discount_type", {
+ data: { discount_type: nextType },
+ });
// Validate only to clear an error the old value left behind. A
// validating `setValue` publishes new form errors, which re-renders the
// whole form in the same task as the click and makes the switch flicker.
diff --git a/src/app/(main)/(app)/components/invoice-pdf-download-link.tsx b/src/app/(main)/(app)/components/invoice-pdf-download-link.tsx
index 3139765..b26462c 100644
--- a/src/app/(main)/(app)/components/invoice-pdf-download-link.tsx
+++ b/src/app/(main)/(app)/components/invoice-pdf-download-link.tsx
@@ -119,6 +119,9 @@ export function InvoicePDFDownloadLink({
umamiTrackEvent("download_invoice", {
data: {
invoice_template: invoiceData.template,
+ has_item_discount: invoiceData.items.some((item) => {
+ return item.discountAmount > 0;
+ }),
},
});
@@ -152,6 +155,7 @@ export function InvoicePDFDownloadLink({
error,
isMobile,
invoiceData.template,
+ invoiceData.items,
hasTriggeredCTAAction,
markCTAActionTriggered,
],