Skip to content

fix: record the real post title in analytics instead of document.title#1941

Open
subodhr258 wants to merge 1 commit into
developfrom
fix/analytics-blog-post-title
Open

fix: record the real post title in analytics instead of document.title#1941
subodhr258 wants to merge 1 commit into
developfrom
fix/analytics-blog-post-title

Conversation

@subodhr258

Copy link
Copy Markdown
Collaborator

Problem

Addresses the P1 item "On blog post data ingestion - analytics payload sends wrong post title" from the analytics EPIC rtCamp/godam-analytics#171.

Two related defects in the analytics payload:

  1. visit_entry_action_name was sent as raw document.title. On WordPress that is the SEO/document title — "{Post Title} – {Site Name}" by default, or whatever an SEO plugin (Yoast/RankMath title templates) generates — not the post title. Every blog-post view was ingested with this polluted title. Verified on a live site: post "Hello world!" ingests as "Hello world! – subodh-multisite.rt.gw".

  2. postTitle was localized via get_the_title() with no post ID on wp_enqueue_scripts, which reads the loop global $post. On archives/home it records the first post of the loop as the post title for every view on that page (confirmed live: blog index sends postTitle: "Hello world!", postId: 1 while isPost: 0), and on singular pages it silently breaks when a theme/plugin runs a secondary query before wp_head without resetting postdata.

Fix

  • PHP (rtgodam_get_localize_array()): resolve the title from the queried object — is_singular() ? get_the_title( get_queried_object_id() ) : '' — immune to loop-global pollution; empty on non-singular pages where there is no single post to attribute the view to.
  • JS (analytics-helpers.js): visit_entry_action_name: postTitle || document.title — the clean WP post title on singular pages, document.title as fallback elsewhere (on archives the document title is the best available page name).

No schema or analytics-service change needed; both fields already exist in the ingest payload. GoDAM Central's client keeps sending document.title for its own (non-WP) pages, which remains correct there.

Verification

  • Runtime assertions against the modified buildAnalyticsRequestBody() (module executed with stubbed globals): singular → visit_entry_action_name === postTitle; archive → falls back to document.title with post_title: ''; missing postTitle key (stale cached localize) → falls back; unverified-token bail-out unchanged.
  • WP Playground run with the plugin mounted: single post localizes postTitle: "Hello world!", static page "Sample Page", blog index "" — while the post's <title> is Hello world! – My WordPress Website, demonstrating the pollution the fix removes.
  • phpcs and wp-scripts lint-js clean on the changed files; built godam-player-analytics.min.js confirmed to contain the new expression (build artifacts not committed, per release process).

Follow-up (separate repo)

godam-for-woo reel-pops analytics (assets/src/js/reel-pops/analytics/analytics.js) duplicates the visit_entry_action_name: document.title pattern and will need the same one-line change; its post_title benefits from this PR automatically since it reads the same localized videoAnalyticsParams.

visit_entry_action_name was sent as raw document.title, which on WordPress
is the SEO/document title ("Post Title – Site Name", or whatever an SEO
plugin emits) — so blog-post views were ingested with a polluted title.
Prefer the PHP-resolved post title and keep document.title only as the
fallback for non-singular pages.

postTitle itself was localized via get_the_title() with no ID on
wp_enqueue_scripts, which reads the loop global: on archives/home it
recorded the first post of the loop as the "post title" for every view
on the page, and on singular pages it broke if an earlier secondary
query moved the global. Resolve it from the queried object on singular
pages and send it empty elsewhere.
Copilot AI review requested due to automatic review settings June 11, 2026 04:57

Copilot AI 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.

Pull request overview

This PR fixes analytics payload titles for WordPress pages by using the actual queried post title (instead of the SEO-influenced document.title) when building analytics requests. It improves the correctness of visit_entry_action_name and stabilizes the localized postTitle against loop/global $post pollution.

Changes:

  • Update the analytics request body builder to prefer postTitle over document.title for visit_entry_action_name, with a safe fallback.
  • Update the PHP localization array to derive postTitle from the queried object ID (and set it empty on non-singular pages).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
assets/src/js/godam-player/analytics-helpers.js Uses localized postTitle for visit_entry_action_name with document.title fallback.
admin/godam-transcoder-functions.php Localizes postTitle using get_queried_object_id() on singular views to avoid loop-global pollution.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +629 to +632
$localize_array['endpoint'] = RTGODAM_ANALYTICS_BASE;
$localize_array['isPost'] = empty( is_single() ) ? 0 : is_single();
$localize_array['isPage'] = empty( is_page() ) ? 0 : is_page();
$localize_array['isArchive'] = empty( is_archive() ) ? 0 : is_archive();
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.

3 participants