Skip to content

Commit

Permalink
fix: use items in purchase ids
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sk- committed Sep 4, 2024
1 parent 5d62bf5 commit 605577f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
We follow the format used by [Open Telemetry](https://github.com/open-telemetry/opentelemetry-python/blob/main/CHANGELOG.md).

## Unreleased

### Fixed

- Fix id of purchase events
([#283](https://github.com/Topsort/analytics.js/pull/283))

## Version 2.3.1 (2024-04-11)

Patch release to fix tags
Expand Down
11 changes: 11 additions & 0 deletions src/detector.purchases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test("check purchase", async () => {
});
document.body.innerHTML = `
<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>
<div data-ts-action="purchase" data-ts-items='[{"product":"product-id-purchase-after", "price": "2199", "quantity": 1}]'></div>
`;
await import("./detector");

Expand All @@ -25,5 +26,15 @@ test("check purchase", async () => {
{ product: "product-id-purchase3", price: "399", quantity: 4 },
],
},
{
type: "Purchase",
page: "/",
product: undefined,
bid: undefined,
id: expect.stringMatching(/[\d.a-zA-Z-]+/),
items: [
{ product: "product-id-purchase-after", price: "2199", quantity: 1 },
],
},
]);
});
3 changes: 2 additions & 1 deletion src/detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ function logEvent(info: ProductEvent, node: Node) {
}

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

function getPage(): string {
Expand Down

0 comments on commit 605577f

Please sign in to comment.