From 605577f7d8ec793fbb42f8b538204331a5f7db91 Mon Sep 17 00:00:00 2001 From: Sebastian Kreft Date: Wed, 4 Sep 2024 17:41:13 -0400 Subject: [PATCH] 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. --- CHANGELOG.md | 7 +++++++ src/detector.purchases.test.ts | 11 +++++++++++ src/detector.ts | 3 ++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5614c4d..4a4df1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/detector.purchases.test.ts b/src/detector.purchases.test.ts index 7527c10..6c148ce 100644 --- a/src/detector.purchases.test.ts +++ b/src/detector.purchases.test.ts @@ -9,6 +9,7 @@ test("check purchase", async () => { }); document.body.innerHTML = `
+
`; await import("./detector"); @@ -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 }, + ], + }, ]); }); diff --git a/src/detector.ts b/src/detector.ts index bb319d8..498b4ac 100644 --- a/src/detector.ts +++ b/src/detector.ts @@ -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 {