Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/schemas/v1/page-hit-processed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const PageHitProcessedSchema = Type.Object({
member_status: Type.Union([Type.String({minLength: 1}), Type.Literal('undefined')]),
post_uuid: Type.Union([Type.String({format: 'uuid'}), Type.Literal('undefined')]),
post_type: Type.Union([Type.Literal('null'), Type.Literal('post'), Type.Literal('page')]),
gift_link: Type.Optional(Type.Union([Type.String(), Type.Null()])),
locale: Type.String({minLength: 1}),
location: Type.Union([Type.String({minLength: 1}), Type.Null()]),
pathname: Type.String({minLength: 1}),
Expand Down Expand Up @@ -161,6 +162,7 @@ export async function transformPageHitRawToProcessed(
member_status: pageHitRaw.payload.member_status,
post_uuid: pageHitRaw.payload.post_uuid,
post_type: pageHitRaw.payload.post_type,
gift_link: pageHitRaw.payload.gift_link,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Normalize missing raw gift_link to null here too.

PageHitRaw.payload.gift_link is optional upstream, so the batch/raw path can still hand you undefined. Copying it through directly breaks the PR’s “absent => null” contract for processed payloads. Use the same fallback as the request→raw transform.

Suggested fix
-            gift_link: pageHitRaw.payload.gift_link,
+            gift_link: pageHitRaw.payload.gift_link ?? null,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
gift_link: pageHitRaw.payload.gift_link,
gift_link: pageHitRaw.payload.gift_link ?? null,
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/schemas/v1/page-hit-processed.ts` at line 166, The processed payload
mapping in PageHitProcessed still forwards pageHitRaw.payload.gift_link
directly, which can leave it undefined instead of normalizing to null. Update
the page-hit processing transform to apply the same fallback used in the
request-to-raw path so that missing gift_link values become null consistently in
the processed output.

locale: pageHitRaw.payload.locale,
location: pageHitRaw.payload.location,
pathname: pageHitRaw.payload.pathname,
Expand Down
1 change: 1 addition & 0 deletions src/schemas/v1/page-hit-raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const PayloadSchema = Type.Object({
member_status: Type.Union([NonEmptyStringSchema, Type.Literal('undefined')]),
post_uuid: Type.Union([UUIDSchema, Type.Literal('undefined')]),
post_type: Type.Union([Type.Literal('null'), Type.Literal('post'), Type.Literal('page')]),
gift_link: Type.Optional(Type.Union([StringSchema, Type.Null()])),
locale: NonEmptyStringSchema,
location: Type.Union([NonEmptyStringSchema, Type.Null()]),
referrer: Type.Optional(Type.Union([StringSchema, Type.Null()])),
Expand Down
2 changes: 2 additions & 0 deletions src/schemas/v1/page-hit-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const PageHitRequestPayloadSchema = Type.Object({
site_uuid: UUIDSchema,
post_uuid: Type.Union([UUIDSchema, Type.Literal('undefined')]),
post_type: Type.Union([Type.Literal('null'), Type.Literal('post'), Type.Literal('page')]),
gift_link: Type.Optional(Type.Union([StringSchema, Type.Null()])),
member_uuid: Type.Union([UUIDSchema, Type.Literal('undefined')]),
member_status: Type.Union([NonEmptyStringSchema, Type.Literal('undefined')]),
utm_source: Type.Optional(Type.Union([StringSchema, Type.Null()])),
Expand Down Expand Up @@ -140,6 +141,7 @@ export const PageHitRequestPayloadDefaults = {
site_uuid: '',
post_uuid: 'undefined',
post_type: 'null',
gift_link: null,
member_uuid: 'undefined',
member_status: 'undefined',
utm_source: null,
Expand Down
1 change: 1 addition & 0 deletions src/transformations/page-hit-transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const pageHitRawPayloadFromRequest = (request: PageHitRequestType): PageH
member_status: request.body.payload.member_status,
post_uuid: request.body.payload.post_uuid,
post_type: request.body.payload.post_type,
gift_link: request.body.payload.gift_link ?? null,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Read the gift token from the producer field

When Ghost sends the new gift-link dimension as payload.gift (as described by this change and allowed by the request schema's additionalProperties), this transform only reads payload.gift_link, so gift_link becomes null and the raw/processed events never carry the token to Tinybird. For gift-link page hits this makes the new per-link analytics silently report as non-gift traffic unless the producer is changed to the different field name.

Useful? React with 👍 / 👎.

locale: request.body.payload.locale,
location: request.body.payload.location,
referrer: request.body.payload.referrer ?? null,
Expand Down
11 changes: 11 additions & 0 deletions test/unit/transformations/page-hit-transformations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('pageHitRawPayloadFromRequest', () => {
member_status: 'free',
post_uuid: 'post-uuid-456',
post_type: 'post',
gift_link: 'gift-token-789',
locale: 'en-US',
location: 'homepage',
referrer: 'https://google.com',
Expand Down Expand Up @@ -52,6 +53,7 @@ describe('pageHitRawPayloadFromRequest', () => {
member_status: 'free',
post_uuid: 'post-uuid-456',
post_type: 'post',
gift_link: 'gift-token-789',
locale: 'en-US',
location: 'homepage',
referrer: 'https://google.com',
Expand All @@ -77,6 +79,15 @@ describe('pageHitRawPayloadFromRequest', () => {
});
});

it('defaults gift_link to null when absent (non-gift hit)', () => {
const request = createPageHitRequest();
delete request.body.payload.gift_link;

const result = pageHitRawPayloadFromRequest(request);

expect(result.payload.gift_link).toBeNull();
});

describe('Event ID', () => {
const uuidMatcher = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

Expand Down