Skip to content

Added gift dimension to the web analytics tracker on gift reads#28853

Open
jonatansberg wants to merge 1 commit into
mainfrom
ber-3728-gift-links-analytics-gift-id
Open

Added gift dimension to the web analytics tracker on gift reads#28853
jonatansberg wants to merge 1 commit into
mainfrom
ber-3728-gift-links-analytics-gift-id

Conversation

@jonatansberg

@jonatansberg jonatansberg commented Jun 24, 2026

Copy link
Copy Markdown
Member

ref https://linear.app/ghost/issue/BER-3746/integrate-gift-link-usage-tracking-with-analytics

Stacked on #28824 (the ?gift= reader path). Producer slice of moving gift-link usage tracking off the DB counter and onto the existing web-analytics pipeline (Tinybird), so gift numbers are measured the same way as every other page view.

What this does

Attaches a single new dimension — gift_link (the gift-link token) — to the page_hit payload on a verified gift read, so downstream analytics can:

  • segment "gift traffic" in the normal reports, and
  • count usage per individual link.

How

  • The reader path (entry/gift-links.ts, setGiftTemplateFlag) flags a verified gift render with a single internal _giftLink token — the token itself, not a boolean — set on res.locals so it merges onto the render context root. One flag, not two: it also drives the reader toast.
  • This renames the internal flag (_gift_giftLink, to match the gift_link dimension) and moves it off the @data frame onto res.locals, so both consumers read it from the render root like post_uuid/member. Touches ghost_foot and the reader’s gift-links controller as well as ghost_head.
  • ghost_head's tracker reads it as dataRoot._giftLink and emits tb_gift_link, via the existing ghost_headghost-stats courier path that already carries post_uuid, member_status, etc. ghost_foot reads the same root flag for the toast.
  • Always emitted, but empty (tb_gift_link="") on a normal read; only a verified gift render carries the token. The empty-vs-token distinction (not an "undefined" sentinel) is what lets the downstream gift_link != '' filter decide what counts as gift traffic.

Gated on labs.giftLinks.

This PR is producer-only

The two consumers are separate PRs so each can be reviewed independently:

  • Proxy (TryGhost/TrafficAnalytics) — thread gift_link through the page-hit transforms, else it's dropped before Tinybird.
  • Data files (Tinybird datafiles in this repo) — _mv_hits column, gift_link segment param, and a per-link endpoint.

Until the proxy lands, gift_link is sent but dropped — no behaviour change.

Out of scope

The pre-existing ?gift= token leak into ghost-stats' location.href (noted in #28824) is unchanged here. The token is sent deliberately as gift_link regardless; the href field is inert in reports.

Test

  • ghost-head.test.js (unit): tb_gift_link carries the token when _giftLink is set, empty otherwise.
  • e2e-frontend/gift-links.test.ts (integration): drives the real reader path with web analytics enabled and asserts the rendered tracker — a verified token reaches tb_gift_link, while a forged token, a token for another post, and a normal read all render an empty tb_gift_link. Proves an unverified token never enters analytics.

@nx-cloud

nx-cloud Bot commented Jun 24, 2026

Copy link
Copy Markdown

🤖 Nx Cloud AI Fix

Ensure the fix-ci command is configured to always run in your CI pipeline to get automatic fixes in future runs. For more information, please see https://nx.dev/ci/features/self-healing-ci


View your CI Pipeline Execution ↗ for commit 9c2bd8e

Command Status Duration Result
nx run ghost:test:ci:integration ✅ Succeeded 2m 41s View ↗
nx run ghost:test:integration ✅ Succeeded 2m 36s View ↗
nx run ghost:test:legacy ✅ Succeeded 2m 54s View ↗
nx run ghost:test:e2e ✅ Succeeded 2m 22s View ↗
nx run-many --target=build --projects=tag:publi... ✅ Succeeded 3s View ↗
nx run-many -t test:unit -p ghost ✅ Succeeded 28s View ↗
nx run-many -t lint -p ghost ✅ Succeeded 34s View ↗
nx run @tryghost/admin:build ✅ Succeeded 5s View ↗
Additional runs (2) ✅ Succeeded ... View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-06-30 07:21:22 UTC

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5ea83bb8-5e9e-4950-b414-d15b768efe44

📥 Commits

Reviewing files that changed from the base of the PR and between 4178ae4 and 9c2bd8e.

⛔ Files ignored due to path filters (1)
  • ghost/core/test/unit/frontend/helpers/__snapshots__/ghost-head.test.js.snap is excluded by !**/*.snap
📒 Files selected for processing (5)
  • ghost/core/core/frontend/helpers/ghost_foot.js
  • ghost/core/core/frontend/helpers/ghost_head.js
  • ghost/core/core/frontend/services/routing/controllers/entry/gift-links.ts
  • ghost/core/test/e2e-frontend/gift-links.test.ts
  • ghost/core/test/unit/frontend/helpers/ghost-head.test.js
🚧 Files skipped from review as they are similar to previous changes (4)
  • ghost/core/core/frontend/helpers/ghost_head.js
  • ghost/core/core/frontend/helpers/ghost_foot.js
  • ghost/core/test/unit/frontend/helpers/ghost-head.test.js
  • ghost/core/core/frontend/services/routing/controllers/entry/gift-links.ts

Walkthrough

Gift-link tokens are now stored on res.locals._giftLink and read from the root render context by ghost_foot and ghost_head. The Tinybird tracker payload includes gift_link, and unit plus e2e tests verify the rendered tb_gift_link output for gift and non-gift requests.

Possibly related PRs

  • TryGhost/Ghost#28824 — Implements the earlier gift-link reader flow that this PR updates for root-context consumption.
  • TryGhost/Ghost#28873 — Modifies the entry gift-link routing path in controllers/entry/gift-links.ts.
  • TryGhost/Ghost#28959 — Changes reader-side gift-toast behavior in ghost_foot.js.

Suggested reviewers

  • kevinansfield
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly reflects the new gift-link analytics dimension added for gift reads.
Description check ✅ Passed The description matches the changeset and explains the new gift_link tracker payload and reader flow.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ber-3728-gift-links-analytics-gift-id

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch 2 times, most recently from c82401c to 968d520 Compare June 24, 2026 10:47
@jonatansberg jonatansberg changed the title Added gift_id to the web analytics tracker on gift reads Added gift dimension to the web analytics tracker on gift reads Jun 24, 2026
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch 2 times, most recently from 3982351 to c8a43b1 Compare June 24, 2026 11:27
@kevinansfield kevinansfield force-pushed the ber-3728-gift-links-query-param branch 3 times, most recently from 65fe68f to c6850b0 Compare June 24, 2026 12:25
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch from c8a43b1 to 2dfdea2 Compare June 24, 2026 12:52
@kevinansfield kevinansfield force-pushed the ber-3728-gift-links-query-param branch 2 times, most recently from ab1ac81 to 6c5e42f Compare June 24, 2026 13:39
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch from 2dfdea2 to 96735e9 Compare June 24, 2026 13:44
@kevinansfield kevinansfield force-pushed the ber-3728-gift-links-query-param branch 2 times, most recently from e8eb78c to fb2eef1 Compare June 24, 2026 13:56
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch from 96735e9 to 305746d Compare June 24, 2026 14:22
@kevinansfield kevinansfield force-pushed the ber-3728-gift-links-query-param branch from fb2eef1 to 13d86a8 Compare June 24, 2026 14:23
Base automatically changed from ber-3728-gift-links-query-param to main June 24, 2026 14:38
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch from 305746d to 04cbb8f Compare June 25, 2026 14:52
@jonatansberg jonatansberg marked this pull request as ready for review June 25, 2026 14:53
@jonatansberg jonatansberg requested a review from cmraible June 25, 2026 14:53

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with 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.

Inline comments:
In `@ghost/core/core/frontend/helpers/ghost_head.js`:
- Around line 211-213: The tb_gift attribute value in ghost_head.js is being
interpolated from gift without escaping, which can break HTML attribute context;
update the tbParams construction to escape/sanitize gift before it is mapped
into the tb_gift string. Use the existing ghost_head helper logic around the
tbParams object and the final map/join expression to ensure gift is safe before
rendering.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 212f9967-c23f-47af-8cc7-47d3260f184c

📥 Commits

Reviewing files that changed from the base of the PR and between d23217d and 04cbb8f.

⛔ Files ignored due to path filters (1)
  • ghost/core/test/unit/frontend/helpers/__snapshots__/ghost-head.test.js.snap is excluded by !**/*.snap
📒 Files selected for processing (2)
  • ghost/core/core/frontend/helpers/ghost_head.js
  • ghost/core/test/unit/frontend/helpers/ghost-head.test.js

Comment thread ghost/core/core/frontend/helpers/ghost_head.js
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch from 04cbb8f to a4ecaed Compare June 29, 2026 09:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a4ecaed7bf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ghost/core/core/frontend/helpers/ghost_head.js Outdated
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch from a4ecaed to 8fa3a1f Compare June 29, 2026 09:47

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with 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.

Inline comments:
In `@ghost/core/core/frontend/services/routing/controllers/entry/gift-links.ts`:
- Around line 31-33: Keep the existing template-data contract in the gift link
flow: when building the data object in the gift-links controller, continue
populating the legacy _gift alias alongside _giftLink so overridden partials
like gift-toast can still read the token. Update the relevant data assembly in
the gift link rendering path to include both symbols in the same frame for
compatibility, preserving _gift until the compatibility window is over.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7864c690-bc4c-4133-a4b0-a96e78c970d9

📥 Commits

Reviewing files that changed from the base of the PR and between a4ecaed and 8fa3a1f.

⛔ Files ignored due to path filters (1)
  • ghost/core/test/unit/frontend/helpers/__snapshots__/ghost-head.test.js.snap is excluded by !**/*.snap
📒 Files selected for processing (4)
  • ghost/core/core/frontend/helpers/ghost_foot.js
  • ghost/core/core/frontend/helpers/ghost_head.js
  • ghost/core/core/frontend/services/routing/controllers/entry/gift-links.ts
  • ghost/core/test/unit/frontend/helpers/ghost-head.test.js
🚧 Files skipped from review as they are similar to previous changes (2)
  • ghost/core/core/frontend/helpers/ghost_head.js
  • ghost/core/test/unit/frontend/helpers/ghost-head.test.js

Comment thread ghost/core/core/frontend/services/routing/controllers/entry/gift-links.ts Outdated
Comment thread ghost/core/core/frontend/helpers/ghost_head.js Outdated
Comment thread ghost/core/test/unit/frontend/helpers/ghost-head.test.js
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch from 8fa3a1f to 4178ae4 Compare June 30, 2026 06:07

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with 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.

Inline comments:
In `@ghost/core/core/frontend/helpers/ghost_head.js`:
- Line 212: The gift_link field in ghost_head.js is currently forwarding
dataRoot._giftLink directly into analytics, which exposes a bearer token. Update
the logic in the ghost_head helper to emit only a non-secret stable identifier
instead, such as a gift-link record ID/UUID or a server-generated keyed hash,
and ensure the analytics payload never includes the raw token value.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 728cf068-10e9-46cc-8ac5-5f4469c4472a

📥 Commits

Reviewing files that changed from the base of the PR and between 8fa3a1f and 4178ae4.

⛔ Files ignored due to path filters (1)
  • ghost/core/test/unit/frontend/helpers/__snapshots__/ghost-head.test.js.snap is excluded by !**/*.snap
📒 Files selected for processing (4)
  • ghost/core/core/frontend/helpers/ghost_foot.js
  • ghost/core/core/frontend/helpers/ghost_head.js
  • ghost/core/core/frontend/services/routing/controllers/entry/gift-links.ts
  • ghost/core/test/unit/frontend/helpers/ghost-head.test.js
🚧 Files skipped from review as they are similar to previous changes (3)
  • ghost/core/test/unit/frontend/helpers/ghost-head.test.js
  • ghost/core/core/frontend/services/routing/controllers/entry/gift-links.ts
  • ghost/core/core/frontend/helpers/ghost_foot.js

Comment thread ghost/core/core/frontend/helpers/ghost_head.js
ref https://linear.app/ghost/issue/BER-3746/integrate-gift-link-usage-tracking-with-analytics

- gift-link usage is moving onto the existing analytics pipeline instead
  of the DB counter, so the numbers stay consistent with the rest of site
  analytics
- ghost_head's tracker emits the gift-link token as `tb_gift_link`, the same
  ghost_head -> ghost-stats courier path that already carries post_uuid,
  member_status, etc.
- the reader path sets the verified token on res.locals as `_giftLink`, so it
  merges onto the render context root where both the tracker (dataRoot._giftLink)
  and ghost_foot's toast read it. This renames the internal flag (`_gift` ->
  `_giftLink`, matching the `gift_link` dimension) and moves it off the @DaTa
  frame onto res.locals so it reads like post_uuid/member; internal flag, not a
  theme API
- always emitted, empty when not a gift, so the backend's `gift_link != ''`
  check decides what counts as gift traffic rather than the producer
  special-casing it
- e2e-frontend gift-links tests now assert the rendered tracker output: a
  verified token reaches `tb_gift_link`, while a forged token, a token for
  another post, and a normal read all render an empty `tb_gift_link` — proving
  an unverified token never enters analytics
@jonatansberg jonatansberg force-pushed the ber-3728-gift-links-analytics-gift-id branch from 4178ae4 to 9c2bd8e Compare June 30, 2026 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants