Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(ingest): events keep a client-sent __query under the URL's own (#594)

Carl-Gerhard Lindesvärded3061fdf9e2
diff --git a/packages/core/src/modules/ingest/src/incoming-event-handler.test.ts b/packages/core/src/modules/ingest/src/incoming-event-handler.test.ts
index 209e22151..550e0b836 100644
--- a/packages/core/src/modules/ingest/src/incoming-event-handler.test.ts
+++ b/packages/core/src/modules/ingest/src/incoming-event-handler.test.ts
@@ -303,6 +303,33 @@ describe('incomingEvent', () => {
expect(Object.keys(query)).toHaveLength(3);
});
+ test('keeps a client-sent __query and lets the URL query win over it', async () => {
+ const { deps, createEvent, ingest } = ctx;
+ ingest.mockResolvedValueOnce({ kind: 'extend', current: makeSession() });
+
+ await incomingEvent(
+ buildJobData({
+ event: {
+ name: 'screen_view',
+ timestamp: new Date().toISOString(),
+ isTimestampFromThePast: false,
+ properties: {
+ __path: 'https://example.com/?utm_source=google',
+ __query: { utm_source: 'meta', utm_campaign: 'first_touch' },
+ },
+ },
+ }),
+ deps
+ );
+
+ const event = createdEvents(createEvent).find(
+ (a) => a.name === 'screen_view'
+ );
+ expect(event!.properties).toMatchObject({
+ __query: { utm_source: 'google', utm_campaign: 'first_touch' },
+ });
+ });
+
test("keeps the event's own utm over the session's", async () => {
const { deps, createEvent, ingest } = ctx;
ingest.mockResolvedValueOnce({
diff --git a/packages/core/src/modules/ingest/src/incoming-event-handler.ts b/packages/core/src/modules/ingest/src/incoming-event-handler.ts
index 0c67f8c53..5e7b61299 100644
--- a/packages/core/src/modules/ingest/src/incoming-event-handler.ts
+++ b/packages/core/src/modules/ingest/src/incoming-event-handler.ts
@@ -298,12 +298,15 @@ async function ingestIncomingEvent(
? null
: parseReferrer(getProperty('__referrer'));
const utmReferrer = getReferrerWithQuery(query);
+ // A client may send its own `__query` (for example first-touch utm tags it
+ // persisted); the URL's query wins over it so a tagged landing is never lost.
+ const clientQuery = properties.__query as Record<string, unknown> | undefined;
// Typed as a plain record so `omit`'s key set is `string`, not the two
// literal keys the spread would otherwise infer.
const eventProperties: Record<string, unknown> = {
...properties,
__hash: hash,
- __query: query,
+ __query: clientQuery || query ? { ...clientQuery, ...query } : undefined,
};
const sdkName = headers['openpanel-sdk-name'];
const sdkVersion = headers['openpanel-sdk-version'];