Skip to content

Commit 3ca89f6

Browse files
authored
fix: use items in purchase ids (#283)
The id we were generating for purshases was alays the same as purchases do not have a bid id, meaning that if two purchases were reported in the same page, only one of them was being reported.
1 parent e72a8d9 commit 3ca89f6

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ We follow the format used by [Open Telemetry](https://github.com/open-telemetry/
1111
### Fixed
1212
- Fix truncation of `seenEvents`
1313
([#282](https://github.com/Topsort/analytics.js/pull/282))
14+
- Fix id of purchase events
15+
([#283](https://github.com/Topsort/analytics.js/pull/283))
1416

1517
## Version 2.3.1 (2024-04-11)
1618

src/detector.purchases.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test("check purchase", async () => {
99
});
1010
document.body.innerHTML = `
1111
<div data-ts-action="purchase" data-ts-items='[{"product":"product-id-purchase1", "price": "2399", "quantity": 1}, {"product":"product-id-purchase2", "price": "299", "quantity": 1}, {"product":"product-id-purchase3", "price": "399", "quantity": 4}]'></div>
12+
<div data-ts-action="purchase" data-ts-items='[{"product":"product-id-purchase-after", "price": "2199", "quantity": 1}]'></div>
1213
`;
1314
await import("./detector");
1415

@@ -25,5 +26,13 @@ test("check purchase", async () => {
2526
{ product: "product-id-purchase3", price: "399", quantity: 4 },
2627
],
2728
},
29+
{
30+
type: "Purchase",
31+
page: "/",
32+
product: undefined,
33+
bid: undefined,
34+
id: expect.stringMatching(/[\d.a-zA-Z-]+/),
35+
items: [{ product: "product-id-purchase-after", price: "2199", quantity: 1 }],
36+
},
2837
]);
2938
});

src/detector.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ function logEvent(info: ProductEvent, node: Node) {
192192
}
193193

194194
function getId(event: ProductEvent): string {
195-
return [event.page, event.type, event.product ?? event.additionalProduct, event.bid].join("-");
195+
const items = JSON.stringify(event.items || []);
196+
return [event.page, event.type, event.product ?? event.additionalProduct, event.bid, items].join(
197+
"-",
198+
);
196199
}
197200

198201
function getPage(): string {

0 commit comments

Comments
 (0)