Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: use callback-modified page for custom events

Blue Mousea05ae5410f8b
diff --git a/packages/tracker-js/src/Lib.ts b/packages/tracker-js/src/Lib.ts
index c02c29789..e5bcd6687 100644
--- a/packages/tracker-js/src/Lib.ts
+++ b/packages/tracker-js/src/Lib.ts
@@ -1291,6 +1291,10 @@ export class Lib {
}
}
+ if (evokeCallback) {
+ this.activePage = pvPayload.pg || null
+ }
+
Object.assign(pvPayload, privateData)
this.sendRequest('', pvPayload)
diff --git a/packages/tracker-js/tests/events.test.ts b/packages/tracker-js/tests/events.test.ts
index 97ab15aab..f255e1575 100644
--- a/packages/tracker-js/tests/events.test.ts
+++ b/packages/tracker-js/tests/events.test.ts
@@ -1,4 +1,4 @@
-import { init, track } from '../src/index'
+import { init, track, trackViews } from '../src/index'
import { Lib } from '../src/Lib'
import { setLocation } from './testUtils'
@@ -88,6 +88,23 @@ describe('Custom Event Tracking', () => {
)
})
+ test('track function should use the page overridden by the pageview callback', async () => {
+ const page = '/articles/:slug'
+ const { stop } = await trackViews({
+ unique: true,
+ callback: () => ({ pg: page }),
+ })
+
+ await track({ ev: 'article_cta_click' })
+
+ expect((libInstance as any).sendRequest).toHaveBeenLastCalledWith(
+ 'custom',
+ expect.objectContaining({ pg: page }),
+ )
+
+ stop()
+ })
+
test('should not track when library is not initialized', async () => {
// Create a new module import to reset the LIB_INSTANCE
jest.resetModules()