Skip to content

feat: capture page title and canonical URL in page view events - #112

Open
alexs-mparticle wants to merge 1 commit into
developmentfrom
feat/page-view-title-canonical
Open

feat: capture page title and canonical URL in page view events#112
alexs-mparticle wants to merge 1 commit into
developmentfrom
feat/page-view-title-canonical

Conversation

@alexs-mparticle

Copy link
Copy Markdown
Collaborator

Summary

Extends the auto page-view capture feature (added in #109) to also record the page title and canonical URL on each page-view event.

Both fields are nested inside each page_events record — alongside the existing pageUrl, sourceMessageId, timestamp, etc. — and surface in the page_events attribute sent to selectPlacements. No new top-level attributes are introduced.

Changes

  • Add optional pageTitle and canonicalUrl to the PageEvent interface.
  • capturePageView() reads document.title and <link rel="canonical">.
  • New readCanonicalUrl() helper resolves the canonical href to absolute and sanitizes it through the same sanitizeUrl() used for pageUrl (query string stripped, hash fragment retained) — keeping canonical handling consistent with pageUrl.
  • buildPageEvents() carries both fields through to the transmitted payload.
  • Both fields are omitted when unavailable, matching how activeTimeOnSite is handled.

Example page_events record

{
  "pageUrl": "https://example.com/checkout#section",
  "sourceMessageId": "...",
  "timestamp": 1712345678000,
  "pageTitle": "Checkout",
  "canonicalUrl": "https://example.com/canonical#section",
  "activeTimeOnSite": 4200
}

Tests

Three new tests: captures title + canonical, omits both when absent, and carries them through selectPlacements. Full suite: 231/231 passing. Lint and build clean.

Note on sanitization

canonicalUrl reuses sanitizeUrl() for consistency with pageUrl, which strips the query string but retains the hash fragment (per the existing pageUrl behavior/tests). If we'd prefer to strip the fragment from canonical too, that's a small follow-up but would diverge from pageUrl handling.

🤖 Generated with Claude Code

Add optional pageTitle and canonicalUrl fields to captured page-view
records so they surface in the page_events attribute sent to
selectPlacements.

- pageTitle is read from document.title
- canonicalUrl is read from <link rel="canonical"> and sanitized
  through the same helper as pageUrl (query string stripped, hash
  fragment retained)

Both fields are omitted when unavailable, matching the existing
activeTimeOnSite handling.
Comment thread src/Rokt-Kit.ts
timestamp: event.Timestamp,
};

const pageTitle = document.title;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: Should this come from event since it shoudl be there already in the edge case small chance it's different from document.title?

Suggested change
const pageTitle = document.title;
const pageTitle = event.EventAttributes?.title || document.title;

@rmi22186 rmi22186 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Adding the pageTitle and canonicalUrl will only make that error we were seeing for quota limit in setting local storage worse. I wouldn't merge this without including the fix for resolving reducing the rate we see the error.

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