Skip to content

Commit 605577f

Browse files
committed
fix: use items in purchase ids
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 5d62bf5 commit 605577f

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77
We follow the format used by [Open Telemetry](https://github.com/open-telemetry/opentelemetry-python/blob/main/CHANGELOG.md).
88

9+
## Unreleased
10+
11+
### Fixed
12+
13+
- Fix id of purchase events
14+
([#283](https://github.com/Topsort/analytics.js/pull/283))
15+
916
## Version 2.3.1 (2024-04-11)
1017

1118
Patch release to fix tags

src/detector.purchases.test.ts

Lines changed: 11 additions & 0 deletions
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,15 @@ 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: [
36+
{ product: "product-id-purchase-after", price: "2199", quantity: 1 },
37+
],
38+
},
2839
]);
2940
});

src/detector.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ function logEvent(info: ProductEvent, node: Node) {
197197
}
198198

199199
function getId(event: ProductEvent): string {
200-
return [event.page, event.type, event.product ?? event.additionalProduct, event.bid].join("-");
200+
const items = JSON.stringify(event.items || []);
201+
return [event.page, event.type, event.product ?? event.additionalProduct, event.bid, items].join("-");
201202
}
202203

203204
function getPage(): string {

0 commit comments

Comments
 (0)