Skip to content

✨ [Story analytics] Add caption state analytics events#2

Closed
michaeldegori wants to merge 1 commit into
mainfrom
feat/story-captions-analytics-events
Closed

✨ [Story analytics] Add caption state analytics events#2
michaeldegori wants to merge 1 commit into
mainfrom
feat/story-captions-analytics-events

Conversation

@michaeldegori
Copy link
Copy Markdown
Owner

@michaeldegori michaeldegori commented May 13, 2026

✨ [Story analytics] Add caption state analytics events

Summary

Adds analytics dispatch for the existing StateProperty.CAPTIONS_STATE so publishers can track when users toggle captions on or off, mirroring the existing STORY_MUTED / STORY_UNMUTED events.

Why

Audio mute/unmute already emits analytics:

// extensions/amp-story/1.0/amp-story.js
this.storeService_.subscribe(
  StateProperty.MUTED_STATE,
  (isMuted) => {
    this.analyticsService_.triggerEvent(
      isMuted
        ? StoryAnalyticsEvent.STORY_MUTED
        : StoryAnalyticsEvent.STORY_UNMUTED
    );
  },
  false /** callToInitialize */
);

Captions are a parallel user-toggleable media state (.i-amphtml-story-captions-button paired with .i-amphtml-story-no-captions-button) but have no analytics surface today. Publishers can't measure caption engagement without it.

Changes

  • story-analytics.js — adds two values to the StoryAnalyticsEvent enum:
    • STORY_CAPTIONS_ON: 'story-captions-on'
    • STORY_CAPTIONS_OFF: 'story-captions-off'
  • amp-story.js — subscribes to StateProperty.CAPTIONS_STATE next to the existing mute-state analytics subscriber. Uses callToInitialize: false so the default-on initialization does not fire an event; only user toggles do.
  • amp-story-analytics.md — documents the two new triggers in the same voice as the existing "Mute trigger" / "Unmute trigger" entries.

Naming notes

  • Event-name verb: on / off matches the captions-on host attribute on the system layer (amp-story-system-layer.js) and reads more naturally than enabled / disabled. Audio uses muted / unmuted, captions uses on / off, so each follows its own native vocabulary.
  • Callback parameter captionsState matches every other CAPTIONS_STATE subscriber in the codebase (amp-story-page.js, amp-story-system-layer.js) and the store-service typedef.

Out of scope (intentional follow-up)

  • No AnalyticsVariable.STORY_HAS_CAPTIONS parallel to STORY_IS_MUTED. Publishers can distinguish state via the new event names; if reviewers prefer a variable for cross-event reporting parity I'm happy to add it in a follow-up.
  • No new test. The audio dispatch in amp-story.js is also untested (mirrored in test-analytics.js and test-amp-story.js). Happy to add coverage for both if preferred.

Test plan

  • npx eslint clean on touched files
  • amp unit --files=extensions/amp-story/1.0/test/test-analytics.js — all 4 existing tests pass
  • Manual smoke: toggle captions in a story with <amp-story-captions> and an amp-analytics listener on story-captions-on / story-captions-off

Semantics check

StateProperty.CAPTIONS_STATE defaults to true in the store. With callToInitialize: false, the first emission is the first user toggle, so an opening-tap that hides captions correctly fires STORY_CAPTIONS_OFF (true → false). This matches the audio precedent's semantics.

Mirrors the existing STORY_MUTED / STORY_UNMUTED analytics dispatch so
publishers can track caption toggles alongside audio mute toggles.

* Adds STORY_CAPTIONS_ON ('story-captions-on') and STORY_CAPTIONS_OFF
  ('story-captions-off') to StoryAnalyticsEvent.
* Subscribes to StateProperty.CAPTIONS_STATE in amp-story with
  callToInitialize: false so the first emission is a user toggle, not
  the default-on initialization.
* Documents both triggers in amp-story-analytics.md.
@michaeldegori
Copy link
Copy Markdown
Owner Author

Superseded by upstream PR ampproject#40505.

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.

1 participant